ruby on rails - RoR - where to put a automated process -
following situation (ror 3, ruby 1.9).
i've got model fields.
now want grab json feed (f.e. every hour) compare content stored model objects , put in new model object if there new item in json feed.
i'm not shure put automated action. don't think doing in models method right place, right?
where migration actions? in controller? not think.
thankful hints
ignore controllers this. models serve abstraction layer accessing data. in case, have locally persisted data in form of database, , have remote data you're retrieving json on http. have regular activerecord models, , have model json data.
let's assume have model called remotedata fetches json document , has methods cleanly data it. have storeddata model keeps retrieved content in database displayed later.
now, want automate process of calling remotedata.fetch('url')
, calling storeddata.create :params
on returned data. that, create "rake task". here's example:
# lib/tasks/fetch.rake desc "fetch remote data , persist it" task :fetch => :environment remotedata.fetch('url').each |json_data| sd = storeddata.create :url => 'url', :data => json_data puts "retrieved 'url' , saved data in record ##{sd.id} @ #{datetime.now}." end end
then set system crontab run frequent like. rake task handles business logic, , models handle interacting data. nice , clean.
Comments
Post a Comment