lib/pupa/processor/client.rb in pupa-0.2.2 vs lib/pupa/processor/client.rb in pupa-0.2.3

- old
+ new

@@ -12,10 +12,16 @@ require 'multi_xml' rescue LoadError # pass end +begin + require 'faraday-cookie_jar' +rescue LoadError + # pass +end + module Pupa class Processor # An HTTP client factory. class Client # Returns a configured Faraday HTTP client. @@ -30,13 +36,16 @@ # @param [Integer] value_max_bytes the maximum Memcached item size # @param [String] memcached_username the Memcached username # @param [String] memcached_password the Memcached password # @param [String] level the log level # @param [String,IO] logdev the log device + # @param [Hash] faraday_options Faraday initialization options # @return [Faraday::Connection] a configured Faraday HTTP client - def self.new(cache_dir: nil, expires_in: 86400, value_max_bytes: 1048576, memcached_username: nil, memcached_password: nil, level: 'INFO', logdev: STDOUT) # 1 day - Faraday.new do |connection| + def self.new(cache_dir: nil, expires_in: 86400, value_max_bytes: 1048576, memcached_username: nil, memcached_password: nil, level: 'INFO', logdev: STDOUT, faraday_options: {}) # 1 day + follow_redirects = faraday_options.delete(:follow_redirects) + + Faraday.new(faraday_options) do |connection| connection.request :url_encoded connection.use Middleware::Logger, Logger.new('faraday', level: level) connection.use Faraday::Response::RaiseError # @see http://tools.ietf.org/html/rfc4627 @@ -49,9 +58,17 @@ end # @see http://tools.ietf.org/html/rfc3023 if defined?(MultiXml) connection.use FaradayMiddleware::ParseXml, preserve_raw: true, content_type: /\bxml$/ + end + + if follow_redirects + connection.use FaradayMiddleware::FollowRedirects, follow_redirects + end + + if Faraday.const_defined?('CookieJar') + connection.use Faraday::CookieJar end # Must come after the parser middlewares. connection.use FaradayMiddleware::Gzip