Module: Amara::Connection

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#connection(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/amara/connection.rb', line 34

def connection(options={})
  opts = merge_default_options(options)
  Faraday.new(opts) do |connection|
    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
# File 'lib/amara/connection.rb', line 16

def merge_default_options(opts={})
  headers = opts.delete(:headers) || {}
  options = HashWithIndifferentAccess.new(
    {
      :headers => {
        'User-Agent'   => user_agent,
        'Accept'       => "application/json",
        'Content-Type' => "application/json"
      },
      :ssl => {:verify => false},
      :url => endpoint
    }        
  ).merge(opts)
  options[:headers] = options[:headers].merge(headers)
  Amara::HEADERS.each{|k,v| options[:headers][v] = options.delete(k) if options.key?(k)}
  options.slice(*ALLOWED_OPTIONS)      
end