module LifenFhir class Patient < Base attribute :uuid, String attribute :first_name, [String] attribute :last_name, String attribute :birth_date, Date attribute :address, Address def self.create(params) new(params).save end def save json = application_client.post("fhir/Patient", create_payload) self.uuid = json["id"] self end def self.find_by_uuid(uuid) patient = new(uuid:uuid) json = application_client.get("fhir/#{LifenFhir::Reference.new(patient)}") patient = new patient.attributes_from_json(json) patient end def fhir_payload { id: "patient", resourceType: "Patient", name: [ { family: last_name, given: first_name } ], birthDate: birth_date.to_s } end def attributes_from_json(json) self.uuid = json["id"] full_name = Array(json["name"]).first self.first_name = Array(full_name["given"]) self.last_name = full_name["family"] self.birth_date = json["birthDate"] address_json = json["address"].first self.address = Address.new.attributes_from_json(address_json) end private def create_payload filtered_params = {"resourceType" => "Patient"} filtered_params["name"] = [{ "family": last_name, "given": first_name }] if address filtered_params["address"] = [ address.create_payload ] end filtered_params["birthDate"] = birth_date.to_s filtered_params end def application_client @application_client ||= AppAuthenticatedClient.new end def self.application_client @application_client ||= AppAuthenticatedClient.new end end end