I am new to rails so I watched railscast #168 to get an understand of how feedjira works. I got an idea how it works now. I got the functionalities to work as well. Where I struggle is at the update section, I can't get the "15 min" update to work.
So I downloaded a gem called "Clockwork" but I can't figure out how to connect the clockwork gem with the updates from feed_entry.rb. I want the rss feed to update every 15 min and every 1h.
class FeedEntry < ActiveRecord::Base
def self.update_from_feed(feed_url)
feed = Feedjira::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries)
end
def self.update_from_feed_continuously(feed_url, delay_interval = 15.minutes)
feed = Feedjira::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries)
loop do
sleep delay_interval
feed = Feedjira::Feed.update(feed)
add_entries(feed.new_entries) if feed.updated?
end
end
private
def self.add_entries(entries)
entries.each do |entry|
unless exists? :guid => entry.id
create!(
:name => entry.title,
:summary => entry.summary,
:url => entry.url,
:published_at => entry.published,
:guid => entry.id
)
end
end
end
end
<div id="recent_episodes">
<h3>Recent Railscasts Episodes</h3>
<ul>
<% for entry in FeedEntry.all.limit(10).order("published_at DESC") %>
<li><%= link_to h(entry.name), entry.url %></li>
<i><%= entry.url %></li>
<% end %>
</ul>
</div>
Here is the code the documentation provides, https://github.com/tomykaira/clockwork.
I generated a model call Clock.
But I am not sure how to use it yet.
class Clock < ActiveRecord::Base
require 'clockwork'
module Clockwork
handler do |job|
puts "Running #{job}"
end
# handler receives the time when job is prepared to run in the 2nd argument
# handler do |job, time|
# puts "Running #{job}, at #{time}"
# end
every(10.seconds, 'frequent.job')
every(3.minutes, 'less.frequent.job')
every(1.hour, 'hourly.job')
every(1.day, 'midnight.job', :at => '00:00')
end
end
As far as I can see the update functionallity was removed from feedjira due to serious problems detecting new feeds.
See https://github.com/feedjira/feedjira/commit/6f56516934a9bdb8691f2bbe98be0f2b7c25b7ea
The website is updated now as well http://feedjira.com/
I am also interested in a library that is capable to detect feed updates without downloading all items. Any suggestions?