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

Defined in:
lib/core_ext/message.rb

Instance Method Summary collapse

Instance Method Details

#eventObject



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

def event
  msh = hash["message"]["content"]["MSH"]
  @msh ||= ::HL7::Message::Segment.from_hash("MSH", msh)
  @msh.to_hash["messageType"]["triggerEvent"]
end

#hashObject



104
105
106
# File 'lib/core_ext/message.rb', line 104

def hash 
  to_hash
end

#message_typeObject



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

def message_type
  msh = hash["message"]["content"]["MSH"]
  @msh ||= ::HL7::Message::Segment.from_hash("MSH", msh)
  @msh.to_hash["messageType"]["messageCode"]
end

#obr_listObject



84
85
86
87
# File 'lib/core_ext/message.rb', line 84

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

#orc_listObject



99
100
101
102
# File 'lib/core_ext/message.rb', line 99

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

#patientObject



89
90
91
92
# File 'lib/core_ext/message.rb', line 89

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

#patient_dobObject



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

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

#patient_full_nameObject



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

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

#patient_genderObject



80
81
82
# File 'lib/core_ext/message.rb', line 80

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

#patient_mrnObject



74
75
76
77
78
# File 'lib/core_ext/message.rb', line 74

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

#patient_visitObject



94
95
96
97
# File 'lib/core_ext/message.rb', line 94

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/core_ext/message.rb', line 12

def providers
  providers = []

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


  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.provider_hash("admitting","AD"), segment: self[:PV1]}
    providers << {hash: pv1.provider_hash("attending","AT"), segment: self[:PV1]}
    providers << {hash: pv1.provider_hash("consulting","CP"), segment: self[:PV1]}
    providers << {hash: pv1.provider_hash("referring","RP"), segment: self[:PV1]}
  end

  if self[:ROL]
    roles = self[:ROL]
    roles = [self[:ROL]] unless self[:ROL].is_a?Array
    roles.each do |rol| 
      providers << {hash: rol.person_hash, segment: rol}
    end
  end 
    
  providers
end

#segments_for(key) ⇒ Object



108
109
110
# File 'lib/core_ext/message.rb', line 108

def segments_for(key)
  self.select {|segment| segment.segment_name == key}
end

#sending_applicationObject



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

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

#to_hashObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/core_ext/message.rb', line 112

def to_hash
  begin
    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"]
        if last_segment == "ORC"
          @hash[:message][:content]["ORC"]["array"].last["OBR"] ||= {}
          @hash[:message][:content]["ORC"]["array"].last["OBR"]["array"] ||= []
          @hash[:message][:content]["ORC"]["array"].last["OBR"]["array"] << segment.to_hash
        else
          last_segment = segment_name
        end
      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 == "ORC"
        @hash[:message][:content]["ORC"] ||= {}
        @hash[:message][:content]["ORC"]["array"] ||= []
        @hash[:message][:content]["ORC"]["array"] << segment.to_hash
        last_segment = segment_name
      elsif segment_name == "NTE"
        if last_segment == "OBR"
          @hash[:message][:content]["OBR"]["array"].last["OBX"] ||= {}
          @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"] ||= []
          if @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"].length>0
            @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"].last["notes"] ||= [] 
            @hash[:message][:content]["OBR"]["array"].last["OBX"]["array"].last["notes"] << segment.to_hash
          end
        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
  rescue => e
    puts e.message
    pp e.backtrace[0..10]
  end
end

#to_jsonObject



167
168
169
170
# File 'lib/core_ext/message.rb', line 167

def to_json
  to_hash
  @hash.to_json
end