Module: DaVinciPASTestKit::FhirpathUtils

Included in:
ClaimEndpoint
Defined in:
lib/davinci_pas_test_kit/cross_suite/fhirpath_utils.rb

Overview

Utilities for evaluating FHIRPath expressions against FHIR resources using the FHIRPath evaluation service and for replacing {fhirpath} tokens in serialized resources with values pulled from a request.

Defined Under Namespace

Classes: FhirpathServiceError

Instance Method Summary collapse

Instance Method Details

#calculate_expression_string_value(request, expression) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/davinci_pas_test_kit/cross_suite/fhirpath_utils.rb', line 47

def calculate_expression_string_value(request, expression)
  JSON.parse(execute_fhirpath(request, expression).body)
    .map { |result| result['element'] }
    .map { |element| element.is_a?(Array) || element.is_a?(Hash) ? nil : element }
    .compact
    .join(',')
end

#execute_fhirpath(body, query) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/davinci_pas_test_kit/cross_suite/fhirpath_utils.rb', line 12

def execute_fhirpath(body, query)
  fhirpath_result = fhirpath_evaluator.call_fhirpath_service(body, query)
  return fhirpath_result if fhirpath_result.status.to_s.start_with?('2')

  raise FhirpathServiceError,
        "FHIRPath service returned #{fhirpath_result.status} for query '#{query}' " \
        "on resource #{body}: #{fhirpath_result.body}"
end

#fhirpath_evaluatorObject



8
9
10
# File 'lib/davinci_pas_test_kit/cross_suite/fhirpath_utils.rb', line 8

def fhirpath_evaluator
  @fhirpath_evaluator ||= Inferno::DSL::FhirpathEvaluation::Evaluator.new
end

#interpret_fhirpath_result_as_boolean(fhirpath_result) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/davinci_pas_test_kit/cross_suite/fhirpath_utils.rb', line 21

def interpret_fhirpath_result_as_boolean(fhirpath_result)
  results = JSON.parse(fhirpath_result.body)
  if results.empty? || results.size > 1
    false
  elsif results.first['type'] == 'boolean'
    results.first['element']
  else
    true
  end
rescue JSON::ParserError
  false
end

#replace_tokens_in_string(string, request) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/davinci_pas_test_kit/cross_suite/fhirpath_utils.rb', line 34

def replace_tokens_in_string(string, request)
  return string unless string.include?('{{')

  tokens_to_replace = string.scan(/\{\{([^}]+)\}\}/).flatten
  replacements = tokens_to_replace.each_with_object({}) do |expression, dictionary|
    next if dictionary.key?("{{#{expression}}}")

    dictionary["{{#{expression}}}"] = calculate_expression_string_value(request, expression)
  end

  string.gsub(/\{\{.*?\}\}/, replacements)
end