Class: DaVinciDTRTestKit::MockEHR::FHIRGetEndpoint

Inherits:
Inferno::DSL::SuiteEndpoint
  • Object
show all
Includes:
DaVinciDTRTestKit::MockEHR
Defined in:
lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb

Constant Summary

Constants included from DaVinciDTRTestKit::MockEHR

RESOURCE_SERVER_BASE, RESOURCE_SERVER_BEARER_TOKEN

Instance Method Summary collapse

Methods included from DaVinciDTRTestKit::MockEHR

metadata, resource_server_client

Instance Method Details

#ehr_input_bundle(test, test_result) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb', line 51

def ehr_input_bundle(test, test_result)
  input_name = "#{input_group_prefix(test)}_ehr_bundle"
  ehr_bundle_input = JSON.parse(test_result.input_json).find { |input| input['name'] == input_name }
  ehr_bundle_input_value = ehr_bundle_input_value = ehr_bundle_input['value'] if ehr_bundle_input.present?
  ehr_bundle = FHIR.from_contents(ehr_bundle_input_value) if ehr_bundle_input_value.present?
  ehr_bundle if ehr_bundle.is_a?(FHIR::Bundle)
rescue StandardError
  nil
end

#fhir_class_and_id_from_url(url) ⇒ Object

Pull resource type class and ID from url e.g. example.org/fhir/Patient/123 -> [FHIR::Patient, ‘123’]



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb', line 80

def fhir_class_and_id_from_url(url)
  path = url.split('?').first.split('/fhir/').second
  path.sub!(%r{/$}, '')
  resource_type, id = path.split('/')

  begin
    fhir_class = FHIR.const_get(resource_type)
  rescue NameError
    fhir_class = nil
  end

  [fhir_class, id]
end

#find_resource_in_bundle(bundle, fhir_class, id) ⇒ Object



71
72
73
74
75
# File 'lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb', line 71

def find_resource_in_bundle(bundle, fhir_class, id)
  bundle.entry&.find do |entry|
    entry.resource.is_a?(fhir_class) && entry.resource&.id == id
  end&.resource
end

#input_group_prefix(test) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb', line 61

def input_group_prefix(test)
  if test.id.include?('static')
    'static'
  elsif test.id.include?('adaptive')
    'adaptive'
  else
    'resp'
  end
end

#make_responseObject



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
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb', line 13

def make_response
  fhir_class, id = fhir_class_and_id_from_url(request.url)
  response.format = 'application/fhir+json'
  response.headers['Access-Control-Allow-Origin'] = '*'

  if fhir_class.nil?
    response.status = 400
    response.body = FHIR::OperationOutcome.new(
      issue: FHIR::OperationOutcome::Issue.new(severity: 'warning', code: 'not-supported',
                                               details: FHIR::CodeableConcept.new(
                                                 text: 'No recognized resource type in URL'
                                               ))
    ).to_json
    return
  end

  # Respond with user-inputted resource if there is one that matches the request
  ehr_bundle = ehr_input_bundle(test, result)
  if id.present? && ehr_bundle.present?
    matching_resource = find_resource_in_bundle(ehr_bundle, fhir_class, id)
    if matching_resource.present?
      response.status = 200
      response.body = matching_resource.to_json
      return
    end
  end

  # Proxy resource request to the reference server
  proxy_response = if id.present?
                     resource_server_client.read(fhir_class, id)
                   else
                     resource_server_client.search(fhir_class,
                                                   search: { parameters: request.env['rack.request.query_hash'] })
                   end
  response.status = proxy_response.code
  response.body = proxy_response.body
end

#test_run_identifierObject



9
10
11
# File 'lib/davinci_dtr_test_kit/endpoints/mock_ehr/fhir_get_endpoint.rb', line 9

def test_run_identifier
  MockAuthorization.extract_client_id_from_bearer_token(request)
end