Class: Faraday::Adapter::Manticore

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/manticore.rb

Defined Under Namespace

Classes: ParallelManager

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, connection_options = {}) ⇒ Manticore

Returns a new instance of Manticore.



23
24
25
26
# File 'lib/faraday/adapter/manticore.rb', line 23

def initialize(app, connection_options = {})
  @connection_options = connection_options
  super(app)
end

Class Method Details

.setup_parallel_manager(options = {}) ⇒ Object



19
20
21
# File 'lib/faraday/adapter/manticore.rb', line 19

def self.setup_parallel_manager(options = {})
  ParallelManager.new
end

Instance Method Details

#call(env) ⇒ Object



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
94
95
96
97
98
99
# File 'lib/faraday/adapter/manticore.rb', line 47

def call(env)
  super

  opts = {}
  if env.key? :request_headers
    opts[:headers] = env[:request_headers]
    opts[:headers].reject! {|k, _| k.downcase == "content-length" }  # Manticore computes Content-Length
  end
  body = read_body(env)
  opts[:body] = body if body

  if req = env[:request]
    opts[:request_timeout] = opts[:connect_timeout] = opts[:socket_timeout] = req[:timeout] if req.key?(:timeout)
    opts[:connect_timeout] = opts[:socket_timeout] = req[:open_timeout] if req.key?(:open_timeout)
    if prx = req[:proxy]
      opts[:proxy] = {
        :url      => prx[:uri].to_s,
        :user     => prx[:user],
        :password => prx[:password]
      }
    end
  end

  cl = client(env)
  if parallel?(env)
    env[:parallel_manager].client = cl
    cl = cl.async
  end

  last_exception = nil

  req = cl.send(env[:method].to_s.downcase, env[:url].to_s, opts)
  req.on_success do |response|
    save_response(env, response.code, response.body || "", response.headers)
    env[:response].finish(env) if parallel?(env)
  end

  req.on_failure do |err|
    case err
    when ::Manticore::Timeout
      raise TimeoutError, err
    when ::Manticore::SocketException, ::Java::JavaUtilConcurrent::ExecutionException
      raise ConnectionFailed, err
    when ::Manticore::ClientProtocolException
      raise Faraday::ClientError, err
    else
      raise err
    end
  end

  req.call unless parallel?(env)
  @app.call env
end

#client(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/faraday/adapter/manticore.rb', line 28

def client(env)
  @client ||= begin
    opts = {}
    if ssl = env[:ssl].to_hash
      opts[:ssl] = {}
      opts[:ssl][:verify]      = :disable if ssl[:verify] == false
      opts[:ssl][:ca_file]     = ssl[:ca_file]
      opts[:ssl][:client_cert] = ssl[:client_cert]
      opts[:ssl][:client_key]  = ssl[:client_key]
    end
    conn_opts = @connection_options.dup
    if conn_opts.key?(:ssl)
      (opts[:ssl] ||= {}).merge! conn_opts.delete(:ssl)
    end
    opts.merge! conn_opts
    ::Manticore::Client.new(opts)
  end
end

#parallel?(env) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/faraday/adapter/manticore.rb', line 101

def parallel?(env)
  !env[:parallel_manager].nil?
end

#read_body(env) ⇒ Object



105
106
107
# File 'lib/faraday/adapter/manticore.rb', line 105

def read_body(env)
  env[:body].respond_to?(:read) ? env[:body].read : env[:body]
end