Module: Speechmatics::Connection

Included in:
API
Defined in:
lib/speechmatics/connection.rb

Constant Summary collapse

ALLOWED_OPTIONS =
[
  :headers,
  :url,
  :params,
  :request,
  :ssl
].freeze

Instance Method Summary collapse

Instance Method Details

#connection(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/speechmatics/connection.rb', line 36

def connection(options={})
  opts = merge_default_options(options)

  @conn ||= Faraday::Connection.new(opts) do |connection|
    connection.request  :multipart
    connection.request  :url_encoded

    connection.response :mashify
    connection.response :logger if ENV['DEBUG']
    connection.response :json

    connection.adapter(*adapter)
  end

end

#merge_default_options(opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/speechmatics/connection.rb', line 16

def merge_default_options(opts={})
  opts = opts.slice(*ALLOWED_OPTIONS)
  headers = opts.delete(:headers) || {}
  params = opts.delete(:params) || {}
  options = HashWithIndifferentAccess.new(
    {
      headers: {
        'User-Agent' => user_agent,
        'Accept'     => "application/json"
      },
      ssl: { verify: false },
      url: endpoint,
      params: { auth_token: auth_token }
    }        
  ).merge(opts)
  options[:headers] = options[:headers].merge(headers)
  options[:params] = options[:params].merge(params)
  options
end