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

Included in:
Faraday::Adapter::EMHttp
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



93
94
95
96
97
98
# File 'lib/faraday/adapter/em_http.rb', line 93

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



53
54
55
56
57
58
59
60
61
62
# File 'lib/faraday/adapter/em_http.rb', line 53

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



65
66
67
68
69
70
71
72
73
# File 'lib/faraday/adapter/em_http.rb', line 65

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



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

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



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

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)


27
28
29
30
31
32
33
34
# File 'lib/faraday/adapter/em_http.rb', line 27

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



47
48
49
50
# File 'lib/faraday/adapter/em_http.rb', line 47

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

#request_config(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/faraday/adapter/em_http.rb', line 36

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



100
101
102
# File 'lib/faraday/adapter/em_http.rb', line 100

def request_options(env)
  env[:request]
end