Skip to content

Instantly share code, notes, and snippets.

@bradylove
Created March 9, 2012 03:48
Show Gist options
  • Select an option

  • Save bradylove/2004915 to your computer and use it in GitHub Desktop.

Select an option

Save bradylove/2004915 to your computer and use it in GitHub Desktop.
class Feed < ActiveRecord::Base
validates :uri, presence: true,
format: { with: /(^$)|([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix }
def load_feed
@cache ||= Redis.new
if @cache.exists("Feed:#{self.uri}")
return Feedzirra::Feed.parse(@cache.get "Feed:#{self.uri}")
else
cache_feed
return Feedzirra::Feed.parse(@cache.get "Feed:#{self.uri}")
end
end
def cache_feed
@cache.set "Feed:#{self.uri}", Feedzirra::Feed.fetch_raw(self.uri)
@cache.expire "Feed:#{self.uri}", 1
self.updated_at = Time.now
self.save
end
end
it "should update if feed cache is older than 15 minutes" do
Timecop.freeze
feed = valid_feed.load_feed
time_one = valid_feed.updated_at
Timecop.travel(16.minutes)
feed = valid_feed.load_feed
time_two = valid_feed.updated_at
time_one.should_not == time_two
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment