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

Defined in:
lib/core_ext/message.rb

Instance Method Summary collapse

Instance Method Details

#account_numberObject



182
183
184
# File 'lib/core_ext/message.rb', line 182

def 
  pv1.value_for_field("19.1")
end

#dg1Object



142
143
144
# File 'lib/core_ext/message.rb', line 142

def dg1
  @dg1 ||= segments_for(:DG1).first
end

#drfObject



150
151
152
# File 'lib/core_ext/message.rb', line 150

def drf
  @drf ||= segments_for(:DRF).first
end

#eventObject



170
171
172
# File 'lib/core_ext/message.rb', line 170

def event
  msh.value_for_field("9.2")
end

#evnObject



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

def evn
  @evn ||= segments_for(:EVN).first
end

#hashObject



186
187
188
# File 'lib/core_ext/message.rb', line 186

def hash 
  to_hash
end

#in1Object



138
139
140
# File 'lib/core_ext/message.rb', line 138

def in1
  @in1 ||= segments_for(:IN1).first
end

#message_typeObject



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

def message_type
  msh.value_for_field("9.1")
end

#mrgObject



146
147
148
# File 'lib/core_ext/message.rb', line 146

def mrg
  @mrg ||= segments_for(:MRG).first
end

#mshObject



118
119
120
# File 'lib/core_ext/message.rb', line 118

def msh
  @msh ||= segments_for(:MSH).first
end

#notesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/core_ext/message.rb', line 91

def notes
  notes_str = ""
  
  if self[:NTE]
    notes = self[:NTE]
    notes = [notes] unless notes.is_a?Array
    notes.each do |nte|
      note = nte.value_for_field("3").gsub("\n","") rescue nil
      if note
        if note.include?"^"
          notes_str << "#{nte.value_for_field("3.2")}: #{nte.value_for_field("3.4")}"
        else
          notes_str << note
        end
      else
      end
      notes_str << "\n"
    end            
  end
  
  notes_str
end

#obr_listObject



154
155
156
157
158
# File 'lib/core_ext/message.rb', line 154

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

#orc_listObject



160
161
162
163
164
# File 'lib/core_ext/message.rb', line 160

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

#patient_mrnObject



178
179
180
# File 'lib/core_ext/message.rb', line 178

def patient_mrn
  pid.value_for_field("3.1")
end

#pd1Object



134
135
136
# File 'lib/core_ext/message.rb', line 134

def pd1
  @pd1 ||= segments_for(:PD1).first
end

#pidObject



122
123
124
# File 'lib/core_ext/message.rb', line 122

def pid
  @pid ||= segments_for(:PID).first
end

#providersObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/core_ext/message.rb', line 25

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]
    pv1 = [pv1] unless pv1.is_a?Array
    pv1.each do |pv|
      providers << {hash: pv.provider_hash("admitting","AD"), segment: pv}
      providers << {hash: pv.provider_hash("attending","AT"), segment: pv}
      providers << {hash: pv.provider_hash("consulting","CP"), segment: pv}
      providers << {hash: pv.provider_hash("referring","RP"), segment: pv}
      providers << {hash: pv.provider_hash("otherHealthcare","OTHER"), segment: pv}
    end
  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 
  
  if self[:PD1]
    pd1 = self[:PD1]
    pd1 = [pd1] unless pd1.is_a?Array
    pd1.each do |pd|
      providers << {hash: pd.provider_hash("primaryCare","PP"), segment: pd}
    end
  end
  
  if self[:ZDRF]
    zdrf = self[:ZDRF]
    zdrf = [zdrf] unless zdrf.is_a?Array
    zdrf.each do |z|
      providers << {hash: z.provider_hash("zdrfProvider","ZDRF"), segment: z}
    end
  end
  
  if self[:ZNP]
    znp = self[:ZNP]
    znp = [znp] unless znp.is_a?Array
    znp.each do |z|
      providers << {hash: z.provider_hash("znpProvider","ZNP"), segment: z}
    end
  end

  retval = []
  providers.inject(retval) {|a,provider| a << provider unless provider[:hash].empty?; a}
  retval
end

#pv1Object



126
127
128
# File 'lib/core_ext/message.rb', line 126

def pv1
  @pv1 ||= segments_for(:PV1).first
end

#pv2Object



130
131
132
# File 'lib/core_ext/message.rb', line 130

def pv2
  @pv2 ||= segments_for(:PV2).first
end

#reason_for_visit(add_newline = true) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/core_ext/message.rb', line 16

def reason_for_visit(add_newline = true)
  retval = ""
  if pv2
    retval << (add_newline ? "\n" : "")
    retval << "Reason for Visit: #{@pv2.admit_reason}"
  end
  retval
end

#segments_for(key) ⇒ Object



12
13
14
# File 'lib/core_ext/message.rb', line 12

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

#sending_applicationObject



174
175
176
# File 'lib/core_ext/message.rb', line 174

def sending_application
  msh.value_for_field("3.1")
end

#to_hashObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/core_ext/message.rb', line 190

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]["segment_array"] ||= []
        @hash[:message][:content][segment_name.to_sym]["segment_array"] << segment

        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
          @hash[:message][:content]["ORC"]["array"].last["OBR"]["segment_array"] ||= []
          @hash[:message][:content]["ORC"]["array"].last["OBR"]["segment_array"] << segment                  
        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
          @hash[:message][:content]["OBR"]["array"].last[segment_name]["segment_array"] ||= []                  
          @hash[:message][:content]["OBR"]["array"].last[segment_name]["segment_array"] << segment                  
        end
      elsif segment_name == "ORC"
        @hash[:message][:content]["ORC"] ||= {}
        @hash[:message][:content]["ORC"]["array"] ||= []
        @hash[:message][:content]["ORC"]["array"] << segment.to_hash
        @hash[:message][:content]["ORC"]["segment_array"] ||= []
        @hash[:message][:content]["ORC"]["segment_array"] << segment                
        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
      elsif segment_name == "TQ1"

      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



254
255
256
257
# File 'lib/core_ext/message.rb', line 254

def to_json
  to_hash
  @hash.to_json
end