# Rake tasks

**URL:** <https://www.ruby-forum.com/t/rake-tasks/175814>\
**Category:** Rails\
**Created:** [September 20, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/rake-tasks/175814 "2009-09-20T14:21:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 20, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/rake-tasks/175814/1 "2009-09-20T14:21:02Z")

</div>

I’m not really creating a rails app, but may integrate this with rails  
down the road.

I’ve adapted rss2mysql.rb from:

Practical Ruby Gems  
[http://www.apress.com/book/view/9781590598115](http://www.apress.com/book/view/9781590598115)

Chapter 10, parsing feeds

but am tweaking everything. Currently I have a rakefile, but am a bit  
unclear about what sort of tasks go in there. For instance, grabbing  
rss  
data from the interwebs could be a task, so maybe it should go in the  
rake file?

Also, the rake file should have provisions to add/drop tables?

I started to put some of the db interactions into the rakefile, and then  
thought that maybe everything in this file (script?) belongs in a  
rakefile. Where do you draw the line?

thufir@ARRAKIS:~/rb$  
thufir@ARRAKIS:~/rb$ cat rss2mysql.rb  
require ‘rubygems’  
require ‘active\_record’  
require ‘feed\_tools’  
require ‘yaml’

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”])  
class Item \< ActiveRecord::Base  
end

unless Item.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

puts “connected”

#feed = FeedTools::Feed.open(‘[Slashdot](http://www.slashdot.org/index.rss)’)  
feed = FeedTools::Feed.open(‘[www.amazon.com/rss/tag/blu-ray/new](http://www.amazon.com/rss/tag/blu-ray/new)’)

feed.items.each do |feed\_item|  
if not (Item.find\_by\_title(feed\_item.title)   
or Item.find\_by\_url(feed\_item.link)   
or Item.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

thufir@ARRAKIS:~/rb$

Also, what sort of IDE is popular for ruby? Eclipse?

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 20, 2009, 6:55pm UTC](https://www.ruby-forum.com/t/rake-tasks/175814/2 "2009-09-20T18:55:34Z")

</div>

On Sun, 20 Sep 2009 12:17:05 +0000, Thufir wrote:

> I’m not really creating a rails app, but may integrate this with rails  
> down the road.
> 
> I’ve adapted rss2mysql.rb from:
> 
> Practical Ruby Gems  
> [http://www.apress.com/book/view/9781590598115](http://www.apress.com/book/view/9781590598115)
> 
> Chapter 10, parsing feeds

I broke that code into three rake tasks:

thufir@ARRAKIS:~/rb$  
thufir@ARRAKIS:~/rb$ rake --tasks  
(in /home/thufir/rb)  
rake connect # connects to db  
rake exists # creates table unless it already exists  
rake populate # gets rss data  
thufir@ARRAKIS:~/rb$

so that exists and populate depend upon the connect task. Is this  
reasonably compatible with RoR?

thanks,

Thufir
