Reputation: 519
I'm creating an App where I would have some kind of scheduled (cron) methods (I guess it would be invoked by rake) to fetch and parse some remote data (mostly HTML) and then store it in my models.
I created a method in my model to fetch the remote data, and another method in the same model to parse and save this data. I feel it's wrong, and that's my question:
Where should be my fetch and parsing methods? Should I create a module? Should I create a Lib?
I think there should be a "rails way" to do it, but I haven't figured it out yet. Can you give me some tips?
Upvotes: 1
Views: 235
Reputation: 176412
My suggestion is to keep inside the model only the methods strictly associated with the persistence scope of the model.
Your new code is not strictly persistence related. It can be considered as a data importer. You can easily create a new class/module only for that.
For example, assuming your library will download some HTML, parse it and save the content as Model instances.
This file should live in /lib
, for example /lib/crawler.rb
.
class Crawler
def run(url)
# ...
fetch( ... )
parse( ... )
import( ... )
end
def fetch(...)
# ...
end
def parse(...)
# ...
end
def import(...)
# ...
result.each do |result|
Model.create( ... )
end
end
end
This ensures your Model
class does not quickly become full of methods belonging to several different features.
Moreover, you will be able to test this library in isolation from the Model
.
In your rake task or worker, simply create a new instance of the crawler (again, this is just an example) and run it.
tast :crawler => [:environment] do
craweler = Crawler.new
craweler.run( ... )
end
Upvotes: 2
Reputation: 4453
There are a couple things you could do, depending on how many of your models will be using your new model.
You could add them to your model 'helper' or you can create a new module and place it in your lib directory. You can then include the lib/module in your model. Nice; clean code.
Upvotes: 0
Reputation: 1242
You may want to watch Ryan Bates railscast on cron/scheduling/polling
http://railscasts.com/episodes/164-cron-in-ruby-revised http://railscasts.com/episodes/271-resque
Very good guides.
Upvotes: 1