Class: SMARTAppLaunch::EchoingFHIRResponderEndpoint

Inherits:
Inferno::DSL::SuiteEndpoint
  • Object
show all
Defined in:
lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb

Instance Method Summary collapse

Instance Method Details

#ehr_input_bundleObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 79

def ehr_input_bundle
  ehr_bundle_input = 
    JSON.parse(result.input_json).find { |input| input['name'] == 'fhir_read_resources_bundle' }&.dig('value')
  ehr_bundle = FHIR.from_contents(ehr_bundle_input) if ehr_bundle_input.present?
  return ehr_bundle if ehr_bundle.is_a?(FHIR::Bundle)
  
  nil
rescue StandardError
  nil
end

#find_resource_in_bundle(bundle, resource_type_class, id) ⇒ Object



90
91
92
93
94
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 90

def find_resource_in_bundle(bundle, resource_type_class, id)
  bundle.entry&.find do |entry|
    entry.resource.is_a?(resource_type_class) && entry.resource.id == id
  end&.resource
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
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 13

def make_response
  return if response.status == 401 # set in update_result (expired token handling there)

  response.content_type = 'application/fhir+json'
  response.headers['Access-Control-Allow-Origin'] = '*'
  response.status = 200

  # look for read of provided resources
  read_response = tester_provided_read_response_body
  if read_response.present?
    response.body = read_response.to_json
    return
  end

  # If the tester provided a response, echo it
  # otherwise, operation outcome
  echo_response = JSON.parse(result.input_json)
    .find { |input| input['name'].include?('echoed_fhir_response') }
    &.dig('value')
  if echo_response.present?
    response.body = echo_response
    return
  end
  
  response.status = 400
  response.body = FHIR::OperationOutcome.new(
    issue: FHIR::OperationOutcome::Issue.new(
      severity: 'fatal', code: 'required',
      details: FHIR::CodeableConcept.new(text: 'No response provided to echo.')
    )
  ).to_json  
end

#tagsObject



55
56
57
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 55

def tags
  [ACCESS_TAG]
end

#test_run_identifierObject



9
10
11
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 9

def test_run_identifier
  MockSMARTServer.issued_token_to_client_id(request.headers['authorization']&.delete_prefix('Bearer '))
end

#tester_provided_read_response_bodyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 59

def tester_provided_read_response_body
  resource_type = request.params[:one]
  id = request.params[:two]

  return unless resource_type.present? && id.present?
  
  resource_type_class = 
    begin
      FHIR.const_get(resource_type)
    rescue NameError
      nil
    end
  return unless resource_type_class.present?

  resource_bundle = ehr_input_bundle
  return unless resource_bundle.present?

  find_resource_in_bundle(resource_bundle, resource_type_class, id)
end

#update_resultObject



46
47
48
49
50
51
52
53
# File 'lib/smart_app_launch/endpoints/echoing_fhir_responder_endpoint.rb', line 46

def update_result
  if MockSMARTServer.request_has_expired_token?(request)
    MockSMARTServer.update_response_for_expired_token(response, 'Bearer token')
    return
  end

  nil # never update for now
end