Sha256: f6f5154d6f243fe4cd53e42a7b79cfc83accf1b2ccfeb1d022a3c94bc5116d9b

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module LifenFhir
  class Communication < Element

    attribute :sender, Practitioner
    attribute :recipient, Element
    attribute :medium, Medium
    attribute :binary, Binary

    attribute :sent_at, DateTime
    attribute :received_at, DateTime

    attribute :status, String

    STATUS = ["preparation", "in-progress", "suspended", "aborted", "completed" , "entered-in-error"]

    def self.find_by_uuid(uuid)
      json = application_client.get("fhir/Communication/#{uuid}")

      communication = new
      communication.attributes_from_json(json)

      communication
    end

    def attributes_from_json(json)
      self.uuid = json["id"]

      self.status = json.fetch("status")

      self.sent_at = json.fetch("sent") { "unknown" }

      self.received_at = json["received"]

      self.binary = Binary.new.attributes_from_json(Array(json["payload"]).first)

      self.sender = Practitioner.new.attributes_from_json(json["sender"])

      self.recipient = patient_or_practitioner(json["recipient"])

      self.medium = Medium.new.attributes_from_json(extract_json_for_medium(json["medium"]))

      raise Error.new(message: "Invalid Communication: status must be in the authorized values") if !valid?

      self
    end

    def method_missing(method, *arguments, &block)
      attribute_name = method.to_s.gsub!('is_', '')
      if STATUS.include? attribute_name
        self.status == attribute_name
      else
        super
      end
    end

  private

      def patient_or_practitioner(recipient_reference)
        m = Array(recipient_reference).first["reference"].match(/(.*)\/(.*)/)

        case m[1]
          when "Patient"
            Patient.new(uuid: m[2])
          when "Practitioner"
            Practitioner.new(uuid: m[2])
        end

      end

      def extract_json_for_medium(json)
        json_coding = Array(json).first
        json_medium = Array(json_coding["coding"]).first

        json_medium
      end

      def valid?
        STATUS.include? self.status
      end


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lifen_fhir-0.7.1 lib/lifen_fhir/communication.rb