Module: Extensions::HL7::Message::InstanceMethods

Defined in:
lib/core_ext/message.rb

Instance Method Summary collapse

Instance Method Details

#eventObject



33
34
35
# File 'lib/core_ext/message.rb', line 33

def event
  hash[:message][:content]["MSH"]["messageEvent"]
end

#hashObject



70
71
72
# File 'lib/core_ext/message.rb', line 70

def hash 
  to_hash
end

#obr_listObject



60
61
62
63
# File 'lib/core_ext/message.rb', line 60

def obr_list
  a = hash["message"]["content"]["OBR"]["array"].collect {|obr| ::HL7::Message::Segment.from_hash("OBR", obr)}
  a.to_enum(:each)
end

#patient_dobObject



46
47
48
# File 'lib/core_ext/message.rb', line 46

def patient_dob
  Date.parse(hash["message"]["content"]["PID"]["dateTimeBirth"]).strftime("%B %d, %Y")
end

#patient_full_nameObject



41
42
43
44
# File 'lib/core_ext/message.rb', line 41

def patient_full_name
  name = hash["message"]["content"]["PID"]["patientName"]
  "#{name["lastName"]}, #{name["firstName"]}#{name["middleInitOrName"].blank? ? "" : " #{name["middleInitOrName"]}"}"
end

#patient_genderObject



56
57
58
# File 'lib/core_ext/message.rb', line 56

def patient_gender
  hash["message"]["content"]["PID"]["sex"]
end

#patient_mrnObject



50
51
52
53
54
# File 'lib/core_ext/message.rb', line 50

def patient_mrn
  pid = hash["message"]["content"]["PID"]
  @pid ||= ::HL7::Message::Segment.from_hash("PID", pid)
  @pid.mrn
end

#patient_visitObject



65
66
67
68
# File 'lib/core_ext/message.rb', line 65

def patient_visit
  patient_visit = hash["message"]["content"]["PV1"]
  ::HL7::Message::Segment.from_hash("PV1", patient_visit)
end

#providersObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/core_ext/message.rb', line 12

def providers
  providers = []

  if self[:OBR]
    obrs = self[:OBR].is_a?(Array) ? self[:OBR] : [self[:OBR]]
    obrs.each do |obr|
      providers << {hash: obr.ordering_provider_hash, segment: obr}
    end
  end

  if self[:PV1]
    pv1 = self[:PV1]
    providers << {hash: pv1.admitting_provider_hash, segment: self[:PV1]}
    providers << {hash: pv1.attending_provider_hash, segment: self[:PV1]}
    providers << {hash: pv1.consulting_provider_hash, segment: self[:PV1]}
    providers << {hash: pv1.referring_provider_hash, segment: self[:PV1]}
  end

  providers
end

#sending_applicationObject



37
38
39
# File 'lib/core_ext/message.rb', line 37

def sending_application
  hash[:message][:content]["MSH"]["sendingApplication"] rescue nil
end

#to_hashObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/core_ext/message.rb', line 74

def to_hash
  return @hash unless @hash.blank?
  @hash ||= HashWithIndifferentAccess.new
  @hash[:message] = {content: {}}

  last_segment = nil
  self.each do |segment|
    segment_name = segment.segment_name
    if segment_name == "OBR"
      @hash[:message][:content][segment_name.to_sym] ||= {}
      @hash[:message][:content][segment_name.to_sym]["array"] ||= []
      @hash[:message][:content][segment_name.to_sym]["array"] << segment.to_hash

      @hash[:message][:content][segment_name.to_sym]["array"]

      last_segment = segment_name
    elsif segment_name == "OBX"
      if last_segment == "OBR"
        @hash[:message][:content]["OBR"]["array"].last[segment_name] ||= {}
        @hash[:message][:content]["OBR"]["array"].last[segment_name]["array"] ||= []
        @hash[:message][:content]["OBR"]["array"].last[segment_name]["array"] << segment.to_hash
      end
    elsif segment_name == "NTE"
      if last_segment == "OBR"
        @hash[:message][:content]["OBR"]["array"].last["OBX"] ||= {}
        @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"] ||= []
        @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"].last["notes"] ||= [] if @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"].length>0
        @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"].last["notes"] << segment.to_hash
      end
    else
      @hash[:message][:content][segment_name.to_sym] = segment.to_hash
    end
    @hash[:message][:content]["EVN"] = @hash[:message][:content]["MSH"]["messageEvent"] if segment_name == "MSH"
  end

  @hash
end

#to_jsonObject



112
113
114
115
# File 'lib/core_ext/message.rb', line 112

def to_json
  to_hash
  @hash.to_json
end