Created
March 9, 2012 03:48
-
-
Save bradylove/2004915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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