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

Defined in:
lib/core_ext/message.rb

Instance Method Summary collapse

Instance Method Details

#account_number ⇒ Object



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

def 
  pv1.value_for_field("19.1")
end

#event ⇒ Object



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

def event
  msh.value_for_field("8.2")
end

#hash ⇒ Object



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

def hash 
  to_hash
end

#message_type ⇒ Object



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

def message_type
  msh.value_for_field("8.1")
end

#msh ⇒ Object



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

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

#obr_list ⇒ Object



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

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

#orc_list ⇒ Object



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

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

#patient_dob ⇒ Object



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

def patient_dob
  Date.parse(pid.patient_dob).strftime("%B %d, %Y") if pid.patient_dob
end

#patient_full_name ⇒ Object



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

def patient_full_name
  last_name = pid.value_for_field("5.1")
  first_name = pid.value_for_field("5.2")
  middle_initial_or_name = pid.value_for_field("5.3")

  "#{last_name}, #{first_name}#{middle_initial_or_name.blank? ? "" : " #{middle_initial_or_name}"}"
end

#patient_gender ⇒ Object



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

def patient_gender
  pid.value_for_field("8")
end

#patient_mrn ⇒ Object



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

def patient_mrn
  pid.value_for_field("3.1")
end

#pid ⇒ Object



68
69
70
# File 'lib/core_ext/message.rb', line 68

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

#providers ⇒ Object



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
48
49
50
# File 'lib/core_ext/message.rb', line 16

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

#pv1 ⇒ Object



92
93
94
# File 'lib/core_ext/message.rb', line 92

def pv1
  @pv1 ||= segments_for(:PV1).first
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_application ⇒ Object



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

def sending_application
  msh.value_for_field("3.1")
end

#to_hash ⇒ Object



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
166
167
168
169
# File 'lib/core_ext/message.rb', line 116

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_json ⇒ Object



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

def to_json
  to_hash
  @hash.to_json
end