Class: Lighthouse::VeteransHealth::Client

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/lighthouse/veterans_health/client.rb

Overview

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initialize(icn) ⇒ Lighthouse::VeteransHealth::Client

Initializes the Veterans Health client.

Lighthouse::VeteransHealth::Client.new(‘12345’)

Parameters:

  • icn (String)

    The ICN of the veteran filing the 526 claim for increase

Raises:

  • (ArgumentError)


28
29
30
31
# File 'lib/lighthouse/veterans_health/client.rb', line 28

def initialize(icn)
  @icn = icn
  raise ArgumentError, 'no ICN passed in for LH API request.' if icn.blank?
end

Instance Method Details

#authenticate(params) ⇒ Object (private)



129
130
131
132
133
134
135
136
# File 'lib/lighthouse/veterans_health/client.rb', line 129

def authenticate(params)
  perform(
    :post,
    'oauth2/health/system/v1/token',
    URI.encode_www_form(params),
    { 'Content-Type': 'application/x-www-form-urlencoded' }
  )
end

#authenticate_as_system(json_web_token) ⇒ Object (private)



154
155
156
# File 'lib/lighthouse/veterans_health/client.rb', line 154

def authenticate_as_system(json_web_token)
  authenticate(payload(json_web_token)).body['access_token']
end

#base64_icnObject (private)



138
139
140
# File 'lib/lighthouse/veterans_health/client.rb', line 138

def base64_icn
  @base64_icn ||= Base64.encode64 JSON.generate({ patient: @icn.to_s }, space: ' ')
end

#bearer_tokenObject (private)



142
143
144
# File 'lib/lighthouse/veterans_health/client.rb', line 142

def bearer_token
  @bearer_token ||= retrieve_bearer_token
end

#collect_all_entries(next_page, collection) ⇒ Object (private)



115
116
117
118
119
120
121
122
123
# File 'lib/lighthouse/veterans_health/client.rb', line 115

def collect_all_entries(next_page, collection)
  while next_page.present?
    next_response = perform_get(URI(next_page).path + "?#{URI(next_page).query}")
    collection.concat(next_response.body['entry'])
    next_page = next_response.body['link'].find { |link| link['relation'] == 'next' }&.[]('url')
  end

  collection
end

#get_allergy_intolerance(id) ⇒ Object



59
60
61
# File 'lib/lighthouse/veterans_health/client.rb', line 59

def get_allergy_intolerance(id)
  perform_get("services/fhir/v0/r4/AllergyIntolerance/#{id}")
end

#get_list(first_response) ⇒ Object (private)

Returns Faraday::Env response with all the pages of data.

Returns:

  • Faraday::Env response with all the pages of data



107
108
109
110
111
112
113
# File 'lib/lighthouse/veterans_health/client.rb', line 107

def get_list(first_response)
  next_page = first_response.body['link'].find { |link| link['relation'] == 'next' }&.[]('url')
  return first_response unless next_page

  first_response.body['entry'] = collect_all_entries(next_page, first_response.body['entry'])
  first_response
end

#get_observation(id) ⇒ Object



91
92
93
# File 'lib/lighthouse/veterans_health/client.rb', line 91

def get_observation(id)
  perform_get("services/fhir/v0/r4/Observation/#{id}")
end

#headers_hashObject (private)



146
147
148
# File 'lib/lighthouse/veterans_health/client.rb', line 146

def headers_hash
  @headers_hash ||= Configuration.base_request_headers.merge({ Authorization: "Bearer #{bearer_token}" })
end

#list_allergy_intolerancesObject



49
50
51
52
53
54
55
56
57
# File 'lib/lighthouse/veterans_health/client.rb', line 49

def list_allergy_intolerances
  params = {
    patient: @icn,
    _count: 100
  }

  first_response = perform_get('services/fhir/v0/r4/AllergyIntolerance', **params.compact)
  get_list(first_response)
end

#list_bp_observationsObject



63
64
65
66
67
68
69
# File 'lib/lighthouse/veterans_health/client.rb', line 63

def list_bp_observations
  params = {
    category: 'vital-signs',
    code: '85354-9'
  }
  list_observations(params)
end

#list_conditionsObject



71
72
73
74
75
76
77
78
79
# File 'lib/lighthouse/veterans_health/client.rb', line 71

def list_conditions
  params = {
    patient: @icn,
    _count: 100
  }

  first_response = perform_get('services/fhir/v0/r4/Condition', **params)
  get_list(first_response)
end

#list_diagnostic_reports(params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lighthouse/veterans_health/client.rb', line 33

def list_diagnostic_reports(params = {})
  params = {
    patient: @icn,
    category: params[:category],
    code: params[:code],
    date: params[:date],
    status: params[:status],
    _lastUpdated: params[:lastUpdated],
    page: params[:page],
    _count: params[:count]
  }

  first_response = perform_get('services/fhir/v0/r4/DiagnosticReport', **params.compact)
  get_list(first_response)
end

#list_medication_requestsObject



95
96
97
98
99
100
101
102
# File 'lib/lighthouse/veterans_health/client.rb', line 95

def list_medication_requests
  params = {
    patient: @icn,
    _count: 100
  }
  first_response = perform_get('services/fhir/v0/r4/MedicationRequest', **params)
  get_list(first_response)
end

#list_observations(params_override = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/lighthouse/veterans_health/client.rb', line 81

def list_observations(params_override = {})
  params = {
    patient: @icn,
    _count: 100
  }.merge(params_override)

  first_response = perform_get('services/fhir/v0/r4/Observation', **params)
  get_list(first_response)
end

#payload(json_web_token) ⇒ Object (private)



158
159
160
161
162
163
164
165
166
# File 'lib/lighthouse/veterans_health/client.rb', line 158

def payload(json_web_token)
  {
    grant_type: 'client_credentials',
    client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
    client_assertion: json_web_token,
    scope: Settings.lighthouse.veterans_health.fast_tracker.api_scope.join(' '),
    launch: base64_icn
  }.as_json
end

#perform_get(uri_path, **params) ⇒ Object (private)



125
126
127
# File 'lib/lighthouse/veterans_health/client.rb', line 125

def perform_get(uri_path, **params)
  perform(:get, uri_path, params, headers_hash)
end

#retrieve_bearer_tokenObject (private)



150
151
152
# File 'lib/lighthouse/veterans_health/client.rb', line 150

def retrieve_bearer_token
  authenticate_as_system(JwtWrapper.new.token)
end