Interestingly, the text book
Practical Ruby Gems
http://www.apress.com/book/view/9781590598115
listing 5-1
uses ActiveRecord::Base wheras ActiveRecord::Migration would give more
flexibility, right?
How would I implement this change, pls?
[email protected]:~/projects/aggregator$
[email protected]:~/projects/aggregator$ cat rss2mysql.rb
require ‘rubygems’
require ‘active_record’
require ‘feed_tools’
require ‘yaml’
require ‘items’
def all
feed_url = ‘http://www.slashdot.org/index.rss’
feed=FeedTools::Feed.open(feed_url)
feed.items.each do |feed_item|
unless (Items.find_by_title(feed_item.title)
or Items.find_by_url(feed_item.link)
or Items.find_by_guid(feed_item.guid))
puts “processing item ‘#{feed_item.title}’ - new”
Items.new do |newitem|
newitem.title=feed_item.title.gsub(/<[^>]*>/, '')
newitem.guid=feed_item.guid
if feed_item.publisher.name
newitem.source=feed_item.publisher.name
end
newitem.url=feed_item.link
newitem.content=feed_item.description
newitem.timestamp=feed_item.published
newitem.save
end
else
puts “processing item ‘#{feed_item.title}’ - old”
end
end
end
Items.connect
all
[email protected]:~/projects/aggregator$
[email protected]:~/projects/aggregator$ cat items.rb
class Items < ActiveRecord::Base
def Items.connect
db = YAML.load_file("database.yml")
ActiveRecord::Base.establish_connection(
:adapter => db["adapter"],
:host => db["host"],
:username => db["username"],
:password => db["password"],
:database => db["database"])
If the table doesn’t exist, we’ll create it.
unless Items.table_exists?
ActiveRecord::Schema.define do
create_table :items do |t|
t.column :title, :string
t.column :content, :string
t.column :source, :string
t.column :url, :string
t.column :timestamp, :timestamp
t.column :keyword_id, :integer
t.column :guid, :string
t.column :html, :string
end
end
end
end #connect
end
[email protected]:~/projects/aggregator$
[email protected]:~/projects/aggregator$ cat items2.rb
class Items2 < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.column :title, :string
t.column :content, :string
t.column :source, :string
t.column :url, :string
t.column :timestamp, :timestamp
t.column :keyword_id, :integer
t.column :guid, :string
t.column :html, :string
end
end
def self.down
drop_table :items
end
end
[email protected]:~/projects/aggregator$
[email protected]:~/projects/aggregator$
thanks,
Thufir