Class: Codat::FaradayCodatAuth

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/codat/faraday_codat_auth.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, api_key = '', options = {}) ⇒ FaradayCodatAuth

Returns a new instance of FaradayCodatAuth.



9
10
11
12
13
14
15
# File 'lib/codat/faraday_codat_auth.rb', line 9

def initialize(app, api_key = '', options = {})
  super()

  @app = app
  @api_key = api_key
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/codat/faraday_codat_auth.rb', line 17

def call(env)
  unless @api_key&.size&.positive?
    raise APIKeyError, 'Missing api_key! Use a Codat.configure block to add your key.'
  end

  env[:request_headers]['Authorization'] = "Basic #{Base64.encode64(@api_key)}"
  env[:request_headers]['Content-Type'] = 'application/json'
  env[:request_headers]['User-Agent'] = "finpoint/#{Codat::VERSION}"

  @app.call(env)
end