Class: Inferno::DSL::FhirpathEvaluation::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno/dsl/fhirpath_evaluation.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Evaluator

Returns a new instance of Evaluator.



37
38
39
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 37

def initialize(url = nil)
  url(url)
end

Instance Method Details

#call_fhirpath_service(fhir_resource, fhirpath_expression) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 96

def call_fhirpath_service(fhir_resource, fhirpath_expression)
  Faraday.new(
    url,
    request: { timeout: 600 }
  ).post(
    "evaluate?path=#{fhirpath_expression}",
    fhir_resource.to_json,
    content_type: 'application/json'
  )
end

#default_fhirpath_urlObject



42
43
44
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 42

def default_fhirpath_url
  ENV.fetch('FHIRPATH_URL')
end

#evaluate_fhirpath(fhir_resource, fhirpath_expression, runnable) ⇒ Array<Hash>

Note:

the ‘element` field can either be a primitive value (string, boolean, etc.) or a FHIR::Model.

Evaluates a fhirpath expression for a given FHIR resource

Parameters:

  • fhir_resource (FHIR::Model)

    the root FHIR resource to use when evaluating the fhirpath expression.

  • fhirpath_expression (String)

    The FHIRPath expression to evaluate.

  • runnable (Inferno::Test)

    to add any error message that occurs.

Returns:

  • (Array<Hash>)

    An array hashes representing the result of evaluating the given expression against the given root resource. Each “result” in the returned array will be in the form ‘{ “type”: “[FHIR datatype of the result]”, “element”: “[result value of the FHIRPath expression]” }`.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 63

def evaluate_fhirpath(fhir_resource, fhirpath_expression, runnable)
  begin
    response = call_fhirpath_service(fhir_resource, fhirpath_expression)
  rescue StandardError => e
    # This could be a complete failure to connect (fhirpath service isn't running)
    # or a timeout (fhirpath service took too long to respond).
    runnable.add_message('error', e.message)
    raise Inferno::Exceptions::ErrorInFhirpathException, "Unable to connect to FHIRPath service at #{url}."
  end

  sanitized_body = remove_invalid_characters(response.body)
  return transform_fhirpath_results(JSON.parse(sanitized_body)) if response.status.to_s.start_with? '2'

  runnable.add_message('error', "FHIRPath service Response: HTTP #{response.status}\n#{sanitized_body}")
  raise Inferno::Exceptions::ErrorInFhirpathException,
        'FHIRPath service call failed. Review Messages tab for more information.'
rescue JSON::ParserError
  runnable.add_message('error', "Invalid FHIRPath service response format:\n#{sanitized_body}")
  raise Inferno::Exceptions::ErrorInFhirpathException,
        'Error occurred in the FHIRPath service. Review Messages tab for more information.'
end

#remove_invalid_characters(string) ⇒ Object



108
109
110
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 108

def remove_invalid_characters(string)
  string.gsub(/[^[:print:]\r\n]+/, '')
end

#transform_fhirpath_results(fhirpath_results) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 86

def transform_fhirpath_results(fhirpath_results)
  fhirpath_results.each do |result|
    klass = FHIR.const_get(result['type'])
    result['element'] = klass.new(result['element'])
  rescue NameError
    next
  end
  fhirpath_results
end

#url(fhirpath_url = nil) ⇒ String

Set/Get the url of the fhirpath service

Parameters:

  • fhirpath_url (String) (defaults to: nil)

Returns:

  • (String)


50
51
52
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 50

def url(fhirpath_url = nil)
  @url ||= fhirpath_url || default_fhirpath_url
end