Skip to content

Commit

Permalink
wrote the Feed.update logic and an initial test spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldix committed Jan 31, 2009
1 parent 90e6339 commit ed90bf6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
31 changes: 31 additions & 0 deletions lib/feedzirra/feed.rb
Expand Up @@ -81,6 +81,37 @@ def self.fetch_and_parse(urls, options = {})
return responses.size == 1 ? responses.values.first : responses
end

def self.update(feeds, options = {})
feeds = [*feeds]
multi = Curl::Multi.new
responses = {}
feeds.each do |feed|
easy = Curl::Easy.new(feed.feed_url) do |curl|
curl.headers["User-Agent"] = (options[:user_agent] || USER_AGENT)
curl.headers["If-Modified-Since"] = feed.last_modified
curl.headers["If-None-Match"] = feed.etag if feed.etag
curl.follow_location = true
curl.on_success do |c|
updated_feed = Feed.parse(c.body_str)
updated_feed.feed_url ||= c.last_effective_url
updated_feed.etag = etag_from_header(c.header_str)
updated_feed.last_modified = last_modified_from_header(c.header_str)
feed.update_from_feed(updated_feed)
responses[feed.feed_url] = feed
options[:on_success].call(feed) if options.has_key?(:on_success)
end
curl.on_failure do |c|
responses[url] = c.response_code
options[:on_failure].call(feed, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure)
end
end
multi.add(easy)
end

multi.perform
return responses.size == 1 ? responses.values.first : responses.values
end

def self.etag_from_header(header)
header =~ /.*ETag:\s(.*)\r/
$1
Expand Down
6 changes: 5 additions & 1 deletion lib/feedzirra/feed_utilities.rb
@@ -1,8 +1,12 @@
module Feedzirra
module FeedUtilities
attr_writer :last_modified, :new_entries, :updated
attr_writer :new_entries, :updated
attr_accessor :etag

def last_modified=(value)
@last_modified = value if value
end

def last_modified
@last_modified ||= entries.inject(Time.now - 10.years) {|last_time, entry| entry.published > last_time ? entry.published : last_time}
end
Expand Down
15 changes: 15 additions & 0 deletions spec/feedzirra/feed_spec.rb
Expand Up @@ -176,5 +176,20 @@ def self.able_to_parse?(val)
# Feedzirra::Feed.fetch_and_parse(@paul_feed_url).last_modified.should.class == Time
# end
end

describe "#update" do
it "should update and return a single feed object" do
feed = Feedzirra::Feed.fetch_and_parse(@paul_feed_url)
feed.entries.delete_at(0)
feed.last_modified = nil
feed.etag = nil
updated_feed = Feedzirra::Feed.update(feed)
updated_feed.new_entries.size.should == 1
updated_feed.should have_new_entries
end

it "should update a collection of feed objects"
it "should return the feed objects even when not updated"
end
end
end

0 comments on commit ed90bf6

Please sign in to comment.