我是 Rails 新手,所以我观看了 railscast #168 以了解 feedjira 的工作原理。我知道它现在是如何工作的。我也得到了工作的功能。我在更新部分苦苦挣扎,我无法让“15 分钟”更新工作。
所以我下载了一个名为“Clockwork”的gem,但我不知道如何将clockwork gem 与feed_entry.rb 的更新联系起来。我希望 rss 提要每 15 分钟和每 1 小时更新一次。
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>
这是文档提供的代码,https://github.com/tomykaira/clockwork。
我生成了一个模型调用时钟。
但我还不确定如何使用它。
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