Class: Inferno::DSL::FhirpathEvaluation::Evaluator
- Inherits:
-
Object
- Object
- Inferno::DSL::FhirpathEvaluation::Evaluator
- Defined in:
- lib/inferno/dsl/fhirpath_evaluation.rb
Instance Method Summary collapse
- #call_fhirpath_service(fhir_resource, fhirpath_expression) ⇒ Object
- #default_fhirpath_url ⇒ Object
-
#evaluate_fhirpath(fhir_resource, fhirpath_expression, runnable) ⇒ Array<Hash>
Evaluates a fhirpath expression for a given FHIR resource.
-
#initialize(url = nil) ⇒ Evaluator
constructor
A new instance of Evaluator.
- #remove_invalid_characters(string) ⇒ Object
- #transform_fhirpath_results(fhirpath_results) ⇒ Object
-
#url(fhirpath_url = nil) ⇒ String
Set/Get the url of the fhirpath service.
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_url ⇒ Object
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
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.('error', e.) 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.('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.('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
50 51 52 |
# File 'lib/inferno/dsl/fhirpath_evaluation.rb', line 50 def url(fhirpath_url = nil) @url ||= fhirpath_url || default_fhirpath_url end |