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::Connection

Returns a configured Faraday HTTP client.

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

Memcached support depends on the dalli gem.



36
37
38
39
40
41
42
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
# File 'lib/pupa/processor/client.rb', line 36

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|
    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

    # Must come after the parser middlewares.
    if FaradayMiddleware.const_defined?(:Gzip)
      connection.use FaradayMiddleware::Gzip
    end

    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