Class: Pupa::Processor::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pupa/processor/client.rb

Overview

An HTTP client factory.

Class Method Summary collapse

Class Method Details

.new(cache_dir: nil, expires_in: 86400, value_max_bytes: 1048576, memcached_username: nil, memcached_password: nil, level: 'INFO', logdev: STDOUT, faraday_options: {}) ⇒ Faraday::Connection

Returns a configured Faraday HTTP client.

To automatically parse XML responses, you must require 'multi_xml'.

Memcached support depends on the dalli gem.

Parameters:

  • (defaults to: nil)

    a directory or a Memcached address (e.g. memcached://localhost:11211) in which to cache requests

  • (defaults to: 86400)

    the cache's expiration time in seconds

  • (defaults to: 1048576)

    the maximum Memcached item size

  • (defaults to: nil)

    the Memcached username

  • (defaults to: nil)

    the Memcached password

  • (defaults to: 'INFO')

    the log level

  • (defaults to: STDOUT)

    the log device

  • (defaults to: {})

    Faraday initialization options

Returns:

  • a configured Faraday HTTP client



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pupa/processor/client.rb', line 43

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
    connection.use Middleware::ParseJson, preserve_raw: true, content_type: /\bjson$/

    # @see http://tools.ietf.org/html/rfc2854
    # @see http://tools.ietf.org/html/rfc3236
    if defined?(Nokogiri)
      connection.use Middleware::ParseHtml, preserve_raw: true, content_type: %w(text/html application/xhtml+xml)
    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

    if cache_dir
      connection.response :caching do
        address = cache_dir[%r{\Amemcached://(.+)\z}, 1]
        if address
          ActiveSupport::Cache::MemCacheStore.new(address, expires_in: expires_in, value_max_bytes: Integer(value_max_bytes), username: memcached_username, password: memcached_password)
        else
          ActiveSupport::Cache::FileStore.new(cache_dir, expires_in: expires_in)
        end
      end
    end

    if defined?(Typhoeus)
      connection.adapter :typhoeus
    else
      connection.adapter Faraday.default_adapter # must be last
    end
  end
end