Permalink
Browse files

On failure callbacks get curl and error as args

Reduces the signature while maintaining or even increasing the
usefulness of arguments
  • Loading branch information...
1 parent 260a979 commit d5524960e832d9d353f3066661328b35d0adb2b6 @jonallured jonallured committed Dec 29, 2013
Showing with 25 additions and 25 deletions.
  1. +1 −1 README.md
  2. +13 −13 lib/feedzirra/feed.rb
  3. +11 −11 spec/feedzirra/feed_spec.rb
View
@@ -102,7 +102,7 @@ updated_feeds = Feedzirra::Feed.update(feeds.values)
# defining custom behavior on failure or success. note that a return status of 304 (not updated) will call the on_success handler
feed = Feedzirra::Feed.fetch_and_parse("http://feeds.feedburner.com/PaulDixExplainsNothing",
:on_success => lambda {|url, feed| puts feed.title },
- :on_failure => lambda {|url, response_code, response_header, response_body, error| puts response_body })
+ :on_failure => lambda {|curl, error| puts error })
# if a collection was passed into fetch_and_parse, the handlers will be called for each one
View
@@ -320,10 +320,10 @@ def self.add_url_to_multi(multi, url, url_queue, responses, options)
responses[url] = feed
options[:on_success].call(url, feed) if options.has_key?(:on_success)
rescue Exception => e
- call_on_failure(url, c, e, options[:on_failure])
+ call_on_failure(c, e, options[:on_failure])
end
else
- call_on_failure(url, c, "Can't determine a parser", options[:on_failure])
+ call_on_failure(c, "Can't determine a parser", options[:on_failure])
end
end
@@ -343,13 +343,13 @@ def self.add_url_to_multi(multi, url, url_queue, responses, options)
curl.on_missing do |c|
if c.response_code == 404 && options.has_key?(:on_failure)
- call_on_failure(url, c, 'Server returned a 404', options[:on_failure])
+ call_on_failure(c, 'Server returned a 404', options[:on_failure])
end
end
curl.on_failure do |c, err|
responses[url] = c.response_code
- call_on_failure(url, c, err, options[:on_failure])
+ call_on_failure(c, err, options[:on_failure])
end
end
multi.add(easy)
@@ -387,21 +387,21 @@ def self.add_feed_to_multi(multi, feed, feed_queue, responses, options)
responses[feed.feed_url] = feed
options[:on_success].call(feed) if options.has_key?(:on_success)
rescue Exception => e
- call_on_failure(feed.feed_url, c, e, options[:on_failure])
+ call_on_failure(c, e, options[:on_failure])
end
end
curl.on_failure do |c, err| # response code 50X
responses[feed.feed_url] = c.response_code
- call_on_failure(feed.feed_url, c, 'Server returned a 404', options[:on_failure])
+ call_on_failure(c, 'Server returned a 404', options[:on_failure])
end
curl.on_redirect do |c, err| # response code 30X
if c.response_code == 304
options[:on_success].call(feed) if options.has_key?(:on_success)
else
responses[feed.feed_url] = c.response_code
- call_on_failure(feed.feed_url, c, err, options[:on_failure])
+ call_on_failure(c, err, options[:on_failure])
end
end
@@ -442,15 +442,15 @@ def on_parser_failure(url)
Proc.new { |message| raise "Error while parsing [#{url}] #{message}" }
end
- def call_on_failure(url, c, error, on_failure)
+ def call_on_failure(c, error, on_failure)
if on_failure
- if on_failure.arity == 5
- on_failure.call(url, c.response_code, c.header_str, c.body_str, error)
- elsif on_failure.arity == 4
+ if on_failure.arity == 4
warn 'on_failure proc with deprecated arity 4 should include a fifth parameter containing the error'
- on_failure.call(url, c.response_code, c.header_str, c.body_str)
+ on_failure.call(c.url, c.response_code, c.header_str, c.body_str)
+ elsif on_failure.arity == 2
+ on_failure.call(c, error)
else
- warn "on_failure proc with invalid parameters number #{on_failure.arity} instead of 5, ignoring it"
+ warn "on_failure proc with invalid parameters number #{on_failure.arity} instead of 2, ignoring it"
end
end
end
@@ -458,8 +458,8 @@ def self.able_to_parse?(val)
describe 'when the parser raises an exception' do
it 'invokes the on_failure callback with that exception' do
- failure = double 'Failure callback', arity: 5
- failure.should_receive(:call).with(@paul_feed[:url], 0, nil, nil, an_instance_of(Hell))
+ failure = double 'Failure callback', arity: 2
+ failure.should_receive(:call).with(@easy_curl, an_instance_of(Hell))
Feedzirra::Parser::AtomFeedBurner.should_receive(:parse).and_raise Hell
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { on_failure: failure })
@@ -474,8 +474,8 @@ def self.able_to_parse?(val)
end
it 'invokes the on_failure callback' do
- failure = double 'Failure callback', arity: 5
- failure.should_receive(:call).with(@paul_feed[:url], 0, nil, nil, an_instance_of(RuntimeError))
+ failure = double 'Failure callback', arity: 2
+ failure.should_receive(:call).with(@easy_curl, an_instance_of(RuntimeError))
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { on_failure: failure })
@easy_curl.on_success.call(@easy_curl)
@@ -485,8 +485,8 @@ def self.able_to_parse?(val)
describe 'when no compatible xml parser class is found' do
it 'invokes the on_failure callback' do
- failure = double 'Failure callback', arity: 5
- failure.should_receive(:call).with(@paul_feed[:url], 0, nil, nil, "Can't determine a parser")
+ failure = double 'Failure callback', arity: 2
+ failure.should_receive(:call).with(@easy_curl, "Can't determine a parser")
Feedzirra::Feed.should_receive(:determine_feed_parser_for_xml).and_return nil
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { on_failure: failure })
@@ -507,8 +507,8 @@ def self.able_to_parse?(val)
end
it 'should call proc if :on_failure option is passed' do
- failure = double 'Failure callback', arity: 5
- failure.should_receive(:call).with(@paul_feed[:url], 500, @headers, @body, nil)
+ failure = double 'Failure callback', arity: 2
+ failure.should_receive(:call).with(@easy_curl, nil)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => failure })
@easy_curl.on_failure.call(@easy_curl)
end
@@ -534,8 +534,8 @@ def self.able_to_parse?(val)
end
it 'should call proc if :on_failure option is passed' do
- complete = double 'Failure callback', arity: 5
- complete.should_receive(:call).with(@paul_feed[:url], 404, @headers, @body, 'Server returned a 404')
+ complete = double 'Failure callback', arity: 2
+ complete.should_receive(:call).with(@easy_curl, 'Server returned a 404')
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => complete })
@easy_curl.on_missing.call(@easy_curl)
end
@@ -660,7 +660,7 @@ def self.able_to_parse?(val)
end
it 'invokes the on_failure callback' do
- failure = double 'Failure callback', arity: 5
+ failure = double 'Failure callback', arity: 2
failure.should_receive(:call)
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, { on_failure: failure })

0 comments on commit d552496

Please sign in to comment.