Class: OpenehrRails::FhirController

Inherits:
ApplicationController show all
Defined in:
app/controllers/openehr_rails/fhir_controller.rb

Overview

HL7 FHIR R5 facade. Reads/searches/creates entry-level resources (Observation, ...) by dispatching on the archetype code through ResourceRegistry. Created resources are converted to openEHR RM and stored canonically by the backing Storable model.

Mounted under the admin engine, so the FHIR base URL is /fhir (e.g. /openehr/fhir).

Constant Summary collapse

FHIR_CONTENT_TYPE =
'application/fhir+json'

Instance Method Summary collapse

Instance Method Details

#createObject

POST /fhir/Observation



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 56

def create
  resource = parse_body
  code = resource.dig('code', 'coding', 0, 'code')
  entry = OpenehrRails::Fhir::ResourceRegistry.find_by_code(code)
  raise OpenehrRails::Fhir::Deserializer::UnmappedResource, "unknown code #{code.inspect}" unless entry

  attrs = OpenehrRails::Fhir::Deserializer.new(entry.model, resource).attributes
  record = entry.model.create!(attrs)
  observation = OpenehrRails::Fhir::Serializer.new(record).observations
                                              .find { |o| o[:code][:coding].first[:code] == code }

  response.headers['Location'] = "#{request.path}/#{observation[:id]}"
  render_fhir(observation, status: :created)
end

#metadataObject



22
23
24
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 22

def 
  render_fhir OpenehrRails::Fhir::CapabilityStatement.build(base_url: request.base_url)
end

#openehr_access_scopeObject



18
19
20
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 18

def openehr_access_scope
  :fhir
end

#searchObject

GET /fhir/Observation?code=<archetype_id>&subject=



34
35
36
37
38
39
40
41
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 34

def search
  entries = lookup_entries(params[:code])
  records = entries.flat_map { |entry| scoped_records(entry) }.uniq
  observations = records.flat_map { |record| OpenehrRails::Fhir::Serializer.new(record).observations }
  observations = filter_by_code(observations, params[:code])

  render_fhir(searchset_bundle(observations))
end

#showObject

GET /fhir/Observation/:id (id = "-")



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 44

def show
  record, entry = resolve(params[:id])
  return render_not_found('Observation', params[:id]) unless record

  observation = OpenehrRails::Fhir::Serializer.new(record).observations
                                              .find { |o| o[:id] == params[:id] }
  return render_not_found('Observation', params[:id]) unless observation

  render_fhir observation
end

#structure_definitionObject



26
27
28
29
30
31
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 26

def structure_definition
  profile = OpenehrRails::Fhir::ProfileRepository.find(params[:id])
  return render_not_found('StructureDefinition', params[:id]) unless profile

  render_fhir profile
end