Module: Fixer::Connection

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#connection(options = {}) ⇒ Object



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

def connection(options={})
  Faraday::Connection.new(process_options(options)) do |connection|
    connection.request :authorization, 'Bearer', get_token(options).token
    connection.request :json
    connection.response :mashify
    connection.response :logger if options[:debug]
    connection.response :json
    connection.adapter adapter
  end
end

#get_token(opts) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/fixer/connection.rb', line 45

def get_token(opts)
  opts = process_options(options)
  opts[:site] = opts.delete(:url)
  @token ||= OAuth2::Client.new(client_id, client_secret, opts) do |connection|
    connection.request :url_encoded
    connection.response :logger if options[:debug]
    connection.adapter adapter
  end.client_credentials.get_token
end

#process_options(opts = {}) ⇒ Object



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

def process_options(opts={})
  headers = opts.delete(:headers) || {}
  options = {
    headers: {
      # generic http headers
      'User-Agent' => user_agent,
      'Accept'     => 'application/json;charset=utf-8'
    },
    ssl: { verify: false },
    url: endpoint
  }.merge(opts)
  options[:headers] = options[:headers].merge(headers)

  options.select{|k,v| ALLOWED_OPTIONS.include?(k.to_sym)}
end