lib/feedzirra/feed.rb in feedzirra-0.6.0 vs lib/feedzirra/feed.rb in feedzirra-0.7.0

- old
+ new

@@ -318,14 +318,14 @@ feed.etag = etag_from_header(c.header_str) feed.last_modified = last_modified_from_header(c.header_str) responses[url] = feed options[:on_success].call(url, feed) if options.has_key?(:on_success) rescue Exception => e - options[:on_failure].call(url, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure) + call_on_failure(c, e, options[:on_failure]) end else - options[:on_failure].call(url, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure) + call_on_failure(c, "Can't determine a parser", options[:on_failure]) end end # # trigger on_failure for 404s @@ -341,17 +341,17 @@ end end curl.on_missing do |c| if c.response_code == 404 && options.has_key?(:on_failure) - options[:on_failure].call(url, c.response_code, c.header_str, c.body_str) + call_on_failure(c, 'Server returned a 404', options[:on_failure]) end end curl.on_failure do |c, err| responses[url] = c.response_code - options[:on_failure].call(url, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure) + call_on_failure(c, err, options[:on_failure]) end end multi.add(easy) end @@ -385,22 +385,25 @@ 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) rescue Exception => e - options[:on_failure].call(feed, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure) + call_on_failure(c, e, options[:on_failure]) end end curl.on_failure do |c, err| # response code 50X - responses[feed.url] = c.response_code - options[:on_failure].call(feed, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure) + responses[feed.feed_url] = c.response_code + 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(c, err, options[:on_failure]) end end curl.on_complete do |c| add_feed_to_multi(multi, feed_queue.shift, feed_queue, responses, options) unless feed_queue.empty? @@ -435,9 +438,22 @@ class << self private def on_parser_failure(url) Proc.new { |message| raise "Error while parsing [#{url}] #{message}" } + end + + def call_on_failure(c, error, on_failure) + if on_failure + if on_failure.arity == 4 + warn 'on_failure proc with deprecated arity 4 should include a fifth parameter containing the error' + 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 2, ignoring it" + end + end end end end end