Module: Cortex::Connection

Included in:
Client
Defined in:
lib/cortex/connection.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cortex/connection.rb', line 10

def connection
  options = {
    :headers => {
        :user_agent => "cortex-client-ruby - #{Cortex::VERSION}"
    },
    :url => base_url
  }

  if access_token.is_a?(OAuth2::AccessToken) && access_token.expired?
    @access_token = get_cc_token
  end

  Faraday::Utils.default_uri_parser = Addressable::URI
  Faraday.new options do |conn|
    ## Request middleware first:
    conn.use Cortex::FaradayMiddleware::NormalizeURIPath
    conn.request :oauth2, access_token.is_a?(OAuth2::AccessToken) ? access_token.token : access_token, token_type: 'param'
    conn.request :json

    ## Response middleware second:
    conn.response :mashify
    conn.use Cortex::FaradayMiddleware::ResponseFailures
    conn.response :json, :content_type => /\bjson$/

    ## Adapter always last:
    conn.adapter Faraday.default_adapter
  end
end