Class: Faraday::Adapter::Excon

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

Overview

Excon adapter.

Instance Method Summary collapse

Instance Method Details

#build_connection(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/faraday/adapter/excon.rb', line 7

def build_connection(env)
  if @connection_options[:persistent] && defined?(@connection)
    return @connection
  end

  opts = opts_from_env(env)

  # remove path & query when creating connection
  # because if it is persistent, it can re-use the conn
  url = env[:url].dup
  url.path = ''
  url.query = nil

  @connection = ::Excon.new(url.to_s, opts.merge(@connection_options))
end

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/faraday/adapter/excon.rb', line 23

def call(env)
  super

  req_opts = {
    path: env[:url].path,
    query: env[:url].query,
    method: env[:method].to_s.upcase,
    headers: env[:request_headers],
    body: read_body(env)
  }

  req = env[:request]
  if req&.stream_response?
    total = 0
    req_opts[:response_block] = lambda do |chunk, _remain, _total|
      req.on_data.call(chunk, total += chunk.size)
    end
  end

  resp = connect_and_request(env, req_opts)
  save_response(env, resp.status.to_i, resp.body, resp.headers,
                resp.reason_phrase)

  @app.call(env)
end

#read_body(env) ⇒ Object

TODO: support streaming requests



50
51
52
# File 'lib/faraday/adapter/excon.rb', line 50

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