class AthenaHealth::Connection

Constants

API_VERSION
PREVIEW_BASE_URL
PRODUCTION_BASE_URL

Public Class Methods

new(client_id:, secret:, token: nil, production: ) click to toggle source
# File lib/athena_health/connection.rb, line 9
def initialize(client_id:, secret:, token: nil, production: )
  @client_id = client_id
  @secret = secret
  @token = token
  @production = production
end

Public Instance Methods

authenticate() click to toggle source
# File lib/athena_health/connection.rb, line 16
def authenticate
  response = Typhoeus.post(
    "#{base_url}/oauth2/#{API_VERSION}/token",
    userpwd: "#{@client_id}:#{@secret}",
    body: { 
      grant_type: 'client_credentials', 
      scope: 'athena/service/Athenanet.MDP.*' 
    }
  ).response_body

  @token = JSON.parse(response)['access_token']
end
call(endpoint:, method:, params: {}, body: {}, second_call: false) click to toggle source
# File lib/athena_health/connection.rb, line 29
def call(endpoint:, method:, params: {}, body: {}, second_call: false)
  authenticate if @token.nil?

  response = Typhoeus::Request.new(
    "#{base_url}/#{API_VERSION}/#{endpoint}",
    method: method,
    headers: { "Authorization" => "Bearer #{@token}"},
    params: params,
    body: body
  ).run

  if response.response_code == 401 && !second_call
    authenticate
    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

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

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

  json_response(body)
end

Private Instance Methods

base_url() click to toggle source
# File lib/athena_health/connection.rb, line 68
def base_url
  @base_url ||= @production ? PRODUCTION_BASE_URL : PREVIEW_BASE_URL
end
json_response(body) click to toggle source
# File lib/athena_health/connection.rb, line 64
def json_response(body)
  JSON.parse(body)
end