lib/lifen_fhir/patient.rb in lifen_fhir-0.1.1 vs lib/lifen_fhir/patient.rb in lifen_fhir-0.2.0

- old
+ new

@@ -1,12 +1,37 @@ module LifenFhir class Patient < Base - attribute :first_name, String + attribute :uuid, String + + attribute :first_name, [String] attribute :last_name, String - attribute :birthdate, Date + 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) + json = application_client.get("fhir/Patient/#{uuid}") + + patient = new + patient.attributes_from_json(json) + + patient + end + def fhir_payload { id: "patient", resourceType: "Patient", name: [ @@ -17,11 +42,56 @@ given: [ first_name ] } ], - birthDate: birthdate.to_s + 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 \ No newline at end of file +end