Class: AthenaHealth::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/athena_health/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_version:, token:) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
# File 'lib/athena_health/connection.rb', line 5

def initialize(base_url:, api_version:, token:)
  @token = token
  @api_version = api_version
  @base_url = base_url
end

Instance Method Details

#call(endpoint:, method:, params: {}, body: {}, second_call: false) ⇒ Object



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
# File 'lib/athena_health/connection.rb', line 11

def call(endpoint:, method:, params: {}, body: {}, second_call: false)
  response = Typhoeus::Request.new(
    "#{@base_url}/#{@api_version}/#{endpoint}",
    method: method,
    headers: @token.auth_header,
    params: params,
    body: body
  ).run

  if response.response_code == 401 && !second_call
    @token.reset!
    return call(endpoint: endpoint, method: method, second_call: true, body: body, params: params)
  end

  if response.response_code == 403 && !second_call
    return call(endpoint: endpoint, method: method, second_call: true, body: body, params: params)
  end

  body = response.response_body

  raise AthenaHealth::ValidationError, json_response(body) if [400, 409].include? response.response_code

  AthenaHealth::Error.new(code: response.response_code).render if response.response_code != 200

  json_response(body)
end