Skip to content
Jason Walonoski edited this page Apr 13, 2020 · 4 revisions
CRUD == Create, Read, Update, Delete

The fhir_client library supports two modes of operation, using the models, or using the client directly.

Using the Models

Associate the client with the model:

FHIR::Model.client = client

The FHIR models can now be used to directly interact with a FHIR server.

# read an existing patient with an ID of 'example'
patient = FHIR::Patient.read('example')

# update a patient
patient.gender = 'female'
patient.update # saves the patient

# create a patient
patient = FHIR::Patient.create(name: {given: 'John', family: 'Doe'})

# create a patient with specific headers
patient = FHIR::Patient.new(name: {given: 'John', family: 'Doe'}).create({Prefer: "return=representation"})

# search patients
results = FHIR::Patient.search(given: 'John', family: 'Doe')
results.count # results in an enumeration

# delete the recently created patient
patient.destroy

Using the Client Directly

# read an existing patient with id "example"
patient = client.read(FHIR::Patient, "example").resource

# read specifying Formats
patient = client.read(FHIR::Patient, "example", FHIR::Formats::FeedFormat::FEED_JSON).resource

# create a patient
patient = FHIR::Patient.new
patient_id = client.create(patient).id

# update the patient
patient.gender = 'female'
client.update(patient, patient_id)

# conditional update
p_identifier = FHIR::Identifier.new
p_identifier.use = 'official'
p_identifier.system = 'http://projectcrucible.org'
p_identifier.value = '123'
patient.identifier = [ p_identifier ]
searchParams = { :identifier => 'http://projectcrucible.org|123' }
client.conditional_update(patient, patient_id, searchParams)

# conditional create
ifNoneExist = { :identifier => 'http://projectcrucible.org|123' }
client.conditional_create(patient, ifNoneExist)

# destroy the patient
client.destroy(FHIR::Patient, patient_id)