Module: Faraday::Adapter::EMHttp::Options

Included in:
Faraday::Adapter::EMHttp, Faraday::Adapter::EMSynchrony
Defined in:
lib/faraday/adapter/em_http.rb

Overview

Options is a module containing helpers to convert the Faraday env object into options hashes for EMHTTP method calls.

Instance Method Summary collapse

Instance Method Details

#configure_compression(options, env) ⇒ Object

Reads out compression header settings from env into options


79
80
81
82
83
84
# File 'lib/faraday/adapter/em_http.rb', line 79

def configure_compression(options, env)
  return unless (env[:method] == :get) &&
                !options[:head].key?('accept-encoding')

  options[:head]['accept-encoding'] = 'gzip, compressed'
end

#configure_proxy(options, env) ⇒ Object

Reads out proxy settings from env into options


39
40
41
42
43
44
45
46
47
48
# File 'lib/faraday/adapter/em_http.rb', line 39

def configure_proxy(options, env)
  proxy = request_options(env)[:proxy]
  return unless proxy

  options[:proxy] = {
    host: proxy[:uri].host,
    port: proxy[:uri].port,
    authorization: [proxy[:user], proxy[:password]]
  }
end

#configure_socket(options, env) ⇒ Object

Reads out host and port settings from env into options


51
52
53
54
55
56
57
58
59
# File 'lib/faraday/adapter/em_http.rb', line 51

def configure_socket(options, env)
  bind = request_options(env)[:bind]
  return unless bind

  options[:bind] = {
    host: bind[:host],
    port: bind[:port]
  }
end

#configure_ssl(options, env) ⇒ Object

Reads out SSL certificate settings from env into options


62
63
64
65
66
67
68
69
# File 'lib/faraday/adapter/em_http.rb', line 62

def configure_ssl(options, env)
  return unless env[:url].scheme == 'https' && env[:ssl]

  options[:ssl] = {
    cert_chain_file: env[:ssl][:ca_file],
    verify_peer: env[:ssl].fetch(:verify, true)
  }
end

#configure_timeout(options, env) ⇒ Object

Reads out timeout settings from env into options


72
73
74
75
76
# File 'lib/faraday/adapter/em_http.rb', line 72

def configure_timeout(options, env)
  req = request_options(env)
  options[:inactivity_timeout] = request_timeout(:read, req)
  options[:connect_timeout] = request_timeout(:open, req)
end

#connection_config(env) ⇒ Hash

Returns:

  • (Hash)

13
14
15
16
17
18
19
20
# File 'lib/faraday/adapter/em_http.rb', line 13

def connection_config(env)
  options = {}
  configure_proxy(options, env)
  configure_timeout(options, env)
  configure_socket(options, env)
  configure_ssl(options, env)
  options
end

#read_body(env) ⇒ Object


33
34
35
36
# File 'lib/faraday/adapter/em_http.rb', line 33

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

#request_config(env) ⇒ Object


22
23
24
25
26
27
28
29
30
31
# File 'lib/faraday/adapter/em_http.rb', line 22

def request_config(env)
  options = {
    body: read_body(env),
    head: env[:request_headers]
    # keepalive: true,
    # file: 'path/to/file', # stream data off disk
  }
  configure_compression(options, env)
  options
end

#request_options(env) ⇒ Object


86
87
88
# File 'lib/faraday/adapter/em_http.rb', line 86

def request_options(env)
  env[:request]
end