Sha256: 30a47f2f65d9c57141cabcd143d245723a25d7945af32ff5be4c6629cf58e6d5

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

module LifenFhir
  class Patient < Element

    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/#{patient.reference}")

      patient.attributes_from_json(json)

      patient
    end

    def fhir_payload
      fhir_payload = {
        id: "patient",
        resourceType: "Patient",
        name: [
          {
            family: last_name,
            given: first_name
          }
        ]
      }
      fhir_payload[:birthDate] = birth_date.to_s if birth_date
      fhir_payload
    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.fhir_payload
          ]
        end

        if birth_date
          filtered_params["birthDate"] = birth_date.to_s
        end

        filtered_params
      end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lifen_fhir-0.7.1 lib/lifen_fhir/patient.rb
lifen_fhir-0.7.0 lib/lifen_fhir/patient.rb
lifen_fhir-0.6.1 lib/lifen_fhir/patient.rb
lifen_fhir-0.6.0 lib/lifen_fhir/patient.rb
lifen_fhir-0.5.0 lib/lifen_fhir/patient.rb
lifen_fhir-0.4.2 lib/lifen_fhir/patient.rb