Class: HL7::Message

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby-hl7.rb

Overview

Ruby Object representation of an hl7 2.x message the message object is actually a “smart” collection of hl7 segments

Examples

Creating a new HL7 message

# create a message
msg = HL7::Message.new

# create a MSH segment for our new message
msh = HL7::Message::Segment::MSH.new
msh.recv_app = "ruby hl7"
msh.recv_facility = "my office"
msh.processing_id = rand(10000).to_s

msg << msh # add the MSH segment to the message

puts msg.to_s # readable version of the message

puts msg.to_hl7 # hl7 version of the message (as a string)

puts msg.to_mllp # mllp version of the message (as a string)

Parse an existing HL7 message

raw_input = open( "my_hl7_msg.txt" ).readlines
msg = HL7::Message.new( raw_input )

puts "message type: %s" % msg[:MSH].message_type

Defined Under Namespace

Classes: Segment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_msg = nil) ⇒ Message

setup a new hl7 message

raw_msg

is an optional object containing an hl7 message it can either be a string or an Enumerable object



83
84
85
86
87
88
89
90
91
# File 'lib/ruby-hl7.rb', line 83

def initialize( raw_msg=nil )
  @segments = []
  @segments_by_name = {}
  @item_delim = "^"
  @element_delim = '|' 
  @segment_delim = "\r"

  parse( raw_msg ) if raw_msg
end

Instance Attribute Details

#element_delimObject (readonly)

Returns the value of attribute element_delim.



76
77
78
# File 'lib/ruby-hl7.rb', line 76

def element_delim
  @element_delim
end

#item_delimObject (readonly)

Returns the value of attribute item_delim.



77
78
79
# File 'lib/ruby-hl7.rb', line 77

def item_delim
  @item_delim
end

#segment_delimObject (readonly)

Returns the value of attribute segment_delim.



78
79
80
# File 'lib/ruby-hl7.rb', line 78

def segment_delim
  @segment_delim
end

Class Method Details

.parse(inobj) ⇒ Object

parse a String or Enumerable object into an HL7::Message if possible

  • returns a new HL7::Message if successful



151
152
153
154
155
# File 'lib/ruby-hl7.rb', line 151

def self.parse( inobj )
  ret = HL7::Message.new
  ret.parse( inobj )
  ret
end

Instance Method Details

#<<(value) ⇒ Object

add a segment to the message

  • will force auto set_id sequencing for segments containing set_id’s



138
139
140
141
142
143
144
145
146
147
# File 'lib/ruby-hl7.rb', line 138

def <<( value )
  unless ( value && value.kind_of?(HL7::Message::Segment) )
    raise HL7::Exception.new( "attempting to append something other than an HL7 Segment" ) 
  end

  (@segments ||= []) << value
  name = value.class.to_s.gsub("HL7::Message::Segment::", "").to_sym
  (@segments_by_name[ name ] ||= []) << value
  sequence_segments # let's auto-set the set-id as we go
end

#[](index) ⇒ Object

access a segment of the message

index

can be a Range, Fixnum or anything that responds to to_sym



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby-hl7.rb', line 96

def []( index )
  ret = nil

  if index.kind_of?(Range) || index.kind_of?(Fixnum)
    ret = @segments[ index ]
  elsif (index.respond_to? :to_sym)
    ret = @segments_by_name[ index.to_sym ]
    ret = ret.first if ret.length == 1
  end

  ret
end

#[]=(index, value) ⇒ Object

modify a segment of the message

index

can be a Range, Fixnum or anything that responds to to_sym

value

an HL7::Message::Segment object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby-hl7.rb', line 113

def []=( index, value )
  unless ( value && value.kind_of?(HL7::Message::Segment) )
    raise HL7::Exception.new( "attempting to assign something other than an HL7 Segment" ) 
  end

  if index.kind_of?(Range) || index.kind_of?(Fixnum)
    @segments[ index ] = value
  else
    (@segments_by_name[ index.to_sym ] ||= []) << value
  end
end

#eachObject

yield each segment in the message



171
172
173
174
# File 'lib/ruby-hl7.rb', line 171

def each # :yeilds: segment
  return unless @segments
  @segments.each { |s| yield s }
end

#index(value) ⇒ Object

return the index of the value if it exists, nil otherwise

value

is expected to be a string



127
128
129
130
131
132
133
134
# File 'lib/ruby-hl7.rb', line 127

def index( value )
  return nil unless (value && value.respond_to?(:to_sym))
  
  segs = @segments_by_name[ value.to_sym ]
  return nil unless segs

  @segments.index( segs.to_a.first )
end

#parse(inobj) ⇒ Object

parse the provided String or Enumerable object into this message



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby-hl7.rb', line 158

def parse( inobj )
  unless inobj.kind_of?(String) || inobj.respond_to?(:each)
    raise HL7::ParseError.new
  end

  if inobj.kind_of?(String)
      parse_string( inobj )
  elsif inobj.respond_to?(:each)
      parse_enumerable( inobj )
  end
end

#sequence_segments(base = nil) ⇒ Object

auto-set the set_id fields of any message segments that provide it and have more than one instance in the message



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ruby-hl7.rb', line 194

def sequence_segments(base=nil)
  last = nil
  segs = @segments
  segs = base.children if base

  segs.each do |s|
    if s.kind_of?( last.class ) && s.respond_to?( :set_id )
      if (last.set_id == "" || last.set_id == nil)
        last.set_id = 1
      end
      s.set_id = last.set_id.to_i + 1
    end

    if s.respond_to?(:children)
      sequence_segments( s )
    end

    last = s
  end
end

#to_hl7Object

provide a HL7 spec version of the message



182
183
184
# File 'lib/ruby-hl7.rb', line 182

def to_hl7
  @segments.join( @segment_delim )
end

#to_mllpObject

provide the HL7 spec version of the message wrapped in MLLP



187
188
189
190
# File 'lib/ruby-hl7.rb', line 187

def to_mllp
  pre_mllp = to_hl7
  "\x0b" + pre_mllp + "\x1c\r"
end

#to_sObject

provide a screen-readable version of the message



177
178
179
# File 'lib/ruby-hl7.rb', line 177

def to_s                         
  @segments.join( '\n' )
end