Created
August 31, 2009 20:22
-
-
Save handcrafted/178687 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 | |
attr_accessor :feed | |
def initialize(url) | |
Feedzirra::Feed.fetch_and_parse(url, {:on_success => lambda {|feed| @feed = feed[1] }, :on_failure => lambda {|url, response_code, response_header, response_body| raise "Feed failed to parse: http response code #{response_code}"}, :timeout => 4800}) | |
end | |
def sanitize! | |
@feed.sanitize_entries! | |
end | |
def tweetable_entries(time_span = nil) | |
time_span ||= Time.now - 86400 | |
@feed.entries.select { |entry| (entry.published || Time.now) >= time_span} | |
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
require File.dirname(__FILE__) + '/spec_helper' | |
describe Feed do | |
it "should fetch and parse the feed" do | |
Feedzirra::Feed.should_receive(:fetch_and_parse).with("http://www.test.com/test.atom", kind_of(Hash)).and_return(load_sample("jotly.atom")) | |
Feed.new("http://www.test.com/test.atom") | |
end | |
describe "already created" do | |
before do | |
Feedzirra::Feed.stub!(:fetch_and_parse).and_return(parsed_atom_feed) | |
@feed = Feed.new("http://www.test.com/test.atom") | |
end | |
it "should find tweetable entries" do | |
puts @feed.inspect | |
entries = @feed.tweetable_entries(Time.utc(2008, 11, 1)) | |
entries.size.should == 1 | |
end | |
it "should find no tweetable entries when there are none" do | |
entries = @feed.tweetable_entries(Time.now) | |
entries.should be_blank | |
end | |
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
def load_sample(filename) | |
File.read("#{File.dirname(__FILE__)}/feeds/#{filename}") | |
end | |
def sample_atom_feed | |
load_sample("jotly.atom") | |
end | |
def parsed_atom_feed | |
Feedzirra::Feed.parse(sample_atom_feed) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment