module LifenFhir class CommunicationRequest < Element attribute :number_communications, Integer attribute :sender, LifenFhir::Sender attribute :recipients, [LifenFhir::Recipient] attribute :category, LifenFhir::Category, default: LifenFhir::Category.new attribute :medium, [LifenFhir::Medium] attribute :patient, LifenFhir::Patient attribute :attachment, LifenFhir::Attachment attribute :binary, LifenFhir::Binary attribute :content_string, LifenFhir::ContentString attribute :status, String def send json = application_client.post("fhir/CommunicationRequest", fhir_payload) self.uuid = json["id"] self.number_communications = Array(json["issue"]).length self end def self.find_by_uuid(uuid) json = application_client.get("fhir/CommunicationRequest/#{uuid}") communication_request = new communication_request.attributes_from_json(json) communication_request end def attributes_from_json(json) self.uuid = json["id"] self.status = json.fetch("status") { "unknown" } if has_payload?(json) self.binary = Binary.new.attributes_from_json(Array(json["payload"]).first) end if has_category?(json) self.category = Category.new.attributes_from_json(Array(json["category"]).first) end if has_sender?(json) self.sender = Practitioner.new.attributes_from_json(json["sender"]) end if has_recipient?(json) self.recipients = Array(json["recipient"]).map do |recipient_json| Practitioner.new.attributes_from_json(recipient_json) end end end private def fhir_payload payload = { resourceType: "CommunicationRequest", sender: [ sender.fhir_payload_as_reference ], recipient: recipients.map(&:fhir_payload_as_reference), contained: [], medium: medium.map(&:fhir_payload) , category: [ category.fhir_payload], payload: [ document_content ] } if patient payload[:contained] << patient.fhir_payload payload[:subject] = [ {reference: "patient"} ] end if content_string payload[:payload] << content_string.fhir_payload end payload end def document_content if !attachment.nil? attachment.fhir_payload else binary.fhir_payload end end def has_category?(json) json.key?("category") end def has_sender?(json) json.key?("sender") end def has_recipient?(json) json.key?("recipient") end def has_payload?(json) json.key?("payload") end end end