Class: SimpleHL7::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_hl7/message.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Message

Constructor

Parameters:

  • options (Hash) (defaults to: nil)

    Options for the Message, keys are symbols, accepted values are: default_msh [boolean] True to create a default MSH segment, false

    to create the message with no segments. Defaults to true.
    

    segment_separator [String] The string to place between segments when

    generating HL7, defaults to "\r".
    


10
11
12
13
14
15
16
# File 'lib/simple_hl7/message.rb', line 10

def initialize(options = nil)
  default_opts = {default_msh: true, segment_separator: "\r"}
  opts = default_opts.merge(options || {})
  @segment_separator = opts[:segment_separator]
  @segments = []
  @segments << MSHSegment.new if opts[:default_msh]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_hl7/message.rb', line 18

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^[a-zA-Z][a-zA-Z0-9]{2}$/
    get_named_segment(meth)
  elsif meth.to_s =~ /^[a-zA-Z][a-zA-Z0-9]{2}_all$/
    seg_name = meth[0..3]
    all_segments(seg_name)
  else
    super
  end
end

Instance Method Details

#add_segment(name) ⇒ Segment

Add a segment to the message

Parameters:

  • name (String)

    The 3 letter segment name, case insensitive.

Returns:

  • (Segment)

    The segment that was just created



71
72
73
74
75
# File 'lib/simple_hl7/message.rb', line 71

def add_segment(name)
  segment = Segment.new(name)
  @segments << segment
  segment
end

#append_segment(segment) ⇒ Segment

Appends a segment to the message

Parameters:

  • segment (Segment)

    The segment to append.

Returns:

  • (Segment)

    The segment just appended



81
82
83
84
# File 'lib/simple_hl7/message.rb', line 81

def append_segment(segment)
  @segments << segment
  segment
end

#segment(name, index = 1) ⇒ Object

Get the specified segment

Parameters:

  • name (String)

    The 3 letter segment name, case insensitive

  • index (Integer) (defaults to: 1)

    The segment number to get if there are multiple in the message. This index starts at 1 because that’s what most HL7 specs do.



60
61
62
63
64
65
# File 'lib/simple_hl7/message.rb', line 60

def segment(name, index=1)
  all = all_segments(name)
  seg = nil
  seg = all[index - 1] if all.size >= index
  seg
end

#to_aArray

Get an array representation of the HL7 message. This can be useful to help debug problems.

Returns:

  • (Array)

    The HL7 message as an array where each subcomposites is also represented as an array.



50
51
52
# File 'lib/simple_hl7/message.rb', line 50

def to_a
  @segments.reduce([]) {|a, s| a << s.to_a}
end

#to_hl7String

Generate a HL7 string from this message

Returns:

  • (String)

    The generated HL7 string



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

def to_hl7
  separator_chars = get_named_segment('MSH').separator_chars
  @segments.map {|s| s.to_hl7(separator_chars)}.join(@segment_separator)
end

#to_llpString

Generate a LLP string from this message Commonly used for transmitting HL7 messages via TCP/IP

Returns:

  • (String)

    The generated LLP string



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

def to_llp
  "\x0b#{to_hl7}\x1c\r"
end