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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 52

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



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

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

#searchObject

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



30
31
32
33
34
35
36
37
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 30

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 = "-")



40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/openehr_rails/fhir_controller.rb', line 40

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



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

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

  render_fhir profile
end