ex_hl7 v1.0.0 HL7.Message

Module used to read, write and retrieve segments from an HL7 message.

Each message is represented as a list of HL7 segments in the order in which they appeared in the original message.

Link to this section Summary

Functions

Deletes the given repetition (0-based) of a segment in a message

Inserts a segment or group of segments after the first repetition of an existing segment in a message.

Inserts a segment or group of segments after the given repetition of an existing segment in a message.

Inserts a segment or group of segments before the first repetition of an existing segment in a message.

Inserts a segment or group of segments before the given repetition of an existing segment in a message.

Return the nth (0-based) grouping of segments with the specified segment IDs.

Reads a binary containing an HL7 message converting it to a list of segments.

Reads a binary containing an HL7 message converting it to a list of segments.

It skips over the first repetition groups of paired segment and invokes fun for each subsequent group of paired segments in the message. It passes the following arguments to fun on each call

Replaces the first repetition of an existing segment in a message.

Replaces the given repetition of an existing segment in a message.

Return the nth repetition (0-based) of a segment within a message.

Return the number of segments with a specified segment ID in an HL7 message.

Writes a list of HL7 segments into an iolist.

Link to this section Types

Link to this type

read_ret()

read_ret() ::
  {:ok, t()}
  | {:incomplete, {(binary() -> read_ret()), binary()}}
  | {:error, reason :: any()}

Link to this section Functions

Link to this function

complete_read(reader, segment, segment_spec, buffer, acc)

Link to this function

delete(message, segment_id, repetition \\ 0)

delete(t(), HL7.Type.segment_id(), non_neg_integer()) :: t()

Deletes the given repetition (0-based) of a segment in a message

Examples

iex> HL7.delete(message, "NTE", 0)

Link to this function

insert_after(message, segment_id, segment)

insert_after(t(), HL7.Type.segment_id(), HL7.Segment.t() | [HL7.Segment.t()]) ::
  t()

Inserts a segment or group of segments after the first repetition of an existing segment in a message.

Arguments

  • message: the HL7.message where the segment/s will be inserted.

  • segment_id: the segment ID of a segment that should be present in the message.

  • segment: the segment or list of segments that will be inserted

Return values

If a segment with the segment_id was present, the function will return a new message with the inserted segments. If not, it will return the original message

Examples

iex> alias HL7.Segment.MSA iex> ack = %MSA{ack_code: "AA", message_control_id: "1234"} iex> HL7.Message.insert_after(message, "MSH", msa)

Link to this function

insert_after(message, segment_id, repetition, new_segments)

insert_after(
  t(),
  HL7.Type.segment_id(),
  non_neg_integer(),
  HL7.Segment.t() | [HL7.Segment.t()]
) :: t()

Inserts a segment or group of segments after the given repetition of an existing segment in a message.

Arguments

  • message: the HL7.message where the segment/s will be inserted.

  • segment_id: the segment ID of a segment that should be present in the message.

  • repetition: the repetition (0-based) of the segment_id in the message.

  • segment: the segment or list of segments that will be inserted

Return values

If a segment with the segment_id was present with the given repetition, the function will return a new message with the inserted segments. If not, it will return the original message

Examples

iex> alias HL7.Segment.MSA iex> ack = %MSA{ack_code: "AA", message_control_id: "1234"} iex> HL7.Message.insert_after(message, "MSH", 0, msa)

Link to this function

insert_before(message, segment_id, segment)

insert_before(
  t(),
  HL7.Type.segment_id(),
  HL7.Segment.t() | [HL7.Segment.t()]
) :: t()

Inserts a segment or group of segments before the first repetition of an existing segment in a message.

Arguments

  • message: the HL7.message where the segment/s will be inserted.

  • segment_id: the segment ID of a segment that should be present in the message.

  • segment: the segment or list of segments that will be inserted

Return values

If a segment with the segment_id was present, the function will return a new message with the inserted segments. If not, it will return the original message

Examples

iex> alias HL7.Segment.MSA iex> ack = %MSA{ack_code: "AA", message_control_id: "1234"} iex> HL7.insert_before(message, "ERR", msa)

Link to this function

insert_before(message, segment_id, repetition, new_segments)

insert_before(
  t(),
  HL7.Type.segment_id(),
  non_neg_integer(),
  HL7.Segment.t() | [HL7.Segment.t()]
) :: t()

Inserts a segment or group of segments before the given repetition of an existing segment in a message.

Arguments

  • message: the HL7.message where the segment/s will be inserted.

  • segment_id: the segment ID of a segment that should be present in the message.

  • repetition: the repetition (0-based) of the segment_id in the message.

  • segment: the segment or list of segments that will be inserted

Return values

If a segment with the segment_id was present with the given repetition, the function will return a new message with the inserted segments. If not, it will return the original message

Examples

iex> alias HL7.Segment.MSA iex> ack = %MSA{ack_code: "AA", message_control_id: "1234"} iex> HL7.Message.insert_before(message, "ERR", 0, msa)

Link to this function

paired_segments(message, segment_ids, repetition \\ 0)

paired_segments(t(), [HL7.Type.segment_id()], non_neg_integer()) :: [
  HL7.Segment.t()
]

Return the nth (0-based) grouping of segments with the specified segment IDs.

In HL7 messages sometimes some segments are immediately followed by other segments within the message. This function was created to help find those "grouped segments".

For example, the PR1 segment is sometimes followed by some other segments (e.g. OBX, AUT, etc.) to include observations and other related information for a practice. Note that there might be multiple segment groupings in a message.

Return value

A list of segments corresponding to the segment IDs that were passed. The list might not include all of the requested segments if they were not present in the message. The function will stop as soon as it finds a segment that does not belong to the passed sequence.

Examples

iex> [pr1, aut] = HL7.Message.paired_segments(message, ["PR1", "AUT"], 0)
iex> [pr1, aut] = HL7.Message.paired_segments(message, ["PR1", "AUT"], 1)
iex> [] = HL7.Message.paired_segments(message, ["PR1", "AUT"], 2)
iex> [aut] = HL7.Message.paired_segments(message, ["PR1", "OBX"], 1)
Link to this function

read(reader, buffer)

read(HL7.Reader.t(), buffer :: binary()) :: read_ret()

Reads a binary containing an HL7 message converting it to a list of segments.

Arguments

  • reader: a HL7.Reader.t that will hold the state of the HL7 parser.

  • buffer: a binary containing the HL7 message to be parsed (partial messages are allowed).

Return value

  • {:ok, HL7.Message.t} if the buffer could be parsed successfully, then a message will be returned. This is actually a list of HL7.Segment.t structs (check the segment.ex file to see the list of included segment definitions).

  • {:incomplete, {(binary -> read_ret), rest :: binary}}: if the message in the string is not a complete HL7 message, then a function will be returned together with the part of the message that could not be parsed. You should acquire the remaining part of the message and concatenate it to the rest of the previous buffer. Finally, you have to call the function that was returned passing it the concatenated string.

  • {:error, reason :: any} if the contents of the buffer were malformed and could not be parsed correctly.

Examples

Given an HL7 message like the following bound to the buffer variable:

"MSH|^~\&|CLIENTHL7|CLI01020304|SERVHL7|PREPAGA^112233^IIN|20120201101155||ZQA^Z02^ZQA_Z02|00XX20120201101155|P|2.4|||ER|SU|ARG\r" <>
"PRD|PS~4600^^HL70454||^^^B||||30123456789^CU\r" <>
"PID|0||1234567890ABC^^^&112233&IIN^HC||unknown\r" <>
"PR1|1||903401^^99DH\r" <>
"AUT||112233||||||1|0\r" <>
"PR1|2||904620^^99DH\r" <>
"AUT||112233||||||1|0\r"

You could read the message in the following way:

iex> reader = HL7.Reader.new(input_format: :wire, trim: true)
iex> {:ok, message} = HL7.Message.read(reader, buffer)
Link to this function

read(reader, buffer, acc)

Link to this function

read!(reader, buffer)

read!(HL7.Reader.t(), buffer :: binary()) :: t() | no_return()

Reads a binary containing an HL7 message converting it to a list of segments.

Arguments

  • reader: a HL7.Reader.t that will hold the state of the HL7 parser.

  • buffer: a binary containing the HL7 message to be parsed (partial messages will raise an HL7.ReadError exception).

Return value

Returns the parsed message (i.e. list of segments) or raises an HL7.ReadError exception in case of error.

Examples

Given an HL7 message like the following bound to the buffer variable:

"MSH|^~\&|CLIENTHL7|CLI01020304|SERVHL7|PREPAGA^112233^IIN|20120201101155||ZQA^Z02^ZQA_Z02|00XX20120201101155|P|2.4|||ER|SU|ARG\r" <>
"PRD|PS~4600^^HL70454||^^^B||||30123456789^CU\r" <>
"PID|0||1234567890ABC^^^&112233&IIN^HC||unknown\r" <>
"PR1|1||903401^^99DH\r" <>
"AUT||112233||||||1|0\r" <>
"PR1|2||904620^^99DH\r" <>
"AUT||112233||||||1|0\r"

You could read the message in the following way:

iex> reader = HL7.Reader.new(input_format: :wire, trim: true)
iex> message = HL7.Message.read!(reader, buffer)
Link to this function

read_segment(reader, segment, segment_spec, buffer)

Link to this function

reduce_paired_segments(message, segment_ids, initial_repetition, acc, fun)

reduce_paired_segments(
  t(),
  [HL7.Type.segment_id()],
  non_neg_integer(),
  acc :: term(),
  ([HL7.Segment.t()], non_neg_integer(), acc :: term() -> acc :: term())
) :: acc :: term()

It skips over the first repetition groups of paired segment and invokes fun for each subsequent group of paired segments in the message. It passes the following arguments to fun on each call:

  • list of segments found that correspond to the group.
  • index of the group of segments in the message (0-based).
  • accumulator acc with the incremental results returned by fun.

In HL7 messages sometimes some segments are immediately followed by other segments within the message. This function was created to easily process those "paired segments".

For example, the PR1 segment is sometimes followed by some other segments (e.g. OBX, AUT, etc.) to include observations and other related information for a procedure. Note that there might be multiple segment groupings in a message.

Return value

The accumulator returned by fun in its last invocation.

Examples

iex> HL7.Message.reduce_paired_segments(message, ["PR1", "AUT"], 0, [], fun segments, index, acc ->
  segment_ids = for segment <- segments, do: HL7.segment_id(segment)
  [{index, segment_ids} | acc]
end
[{0, ["PR1", "AUT"]}, {1, ["PR1", "AUT"]}]
Link to this function

replace(message, segment_id, segment)

replace(t(), HL7.Type.segment_id(), HL7.Segment.t() | [HL7.Segment.t()]) ::
  t()

Replaces the first repetition of an existing segment in a message.

Arguments

  • message: the HL7.message where the segment/s will be inserted.

  • segment_id: the segment ID of a segment that should be present in the message.

  • segment: the segment or list of segments that will replace the existing one.

Return values

If a segment with the segment_id was present, the function will return a new message with the replaced segments. If not, it will return the original message

Examples

iex> alias HL7.Segment.MSA iex> ack = %MSA{ack_code: "AA", message_control_id: "1234"} iex> HL7.Message.replace(message, "MSA", msa)

Link to this function

replace(message, segment_id, repetition, new_segments)

Replaces the given repetition of an existing segment in a message.

Arguments

  • message: the HL7.message where the segment/s will be inserted.

  • segment_id: the segment ID of a segment that should be present in the message.

  • repetition: the repetition (0-based) of the segment_id in the message.

  • segment: the segment or list of segments that will replace the existing one.

Return values

If a segment with the segment_id was present with the given repetition, the function will return a new message with the replaced segments. If not, it will return the original message

Examples

iex> alias HL7.Segment.MSA iex> ack = %MSA{ack_code: "AA", message_control_id: "1234"} iex> HL7.Message.replace(message, "MSA", 0, msa)

Link to this function

segment(message, segment_id, repetition \\ 0)

segment(t(), HL7.Type.segment_id(), non_neg_integer()) ::
  HL7.Segment.t() | nil

Return the nth repetition (0-based) of a segment within a message.

Return value

If the corresponding repetition of a segment with the passed segment_id is present in the message then the function returns the segment; otherwise it returns nil.

Examples

iex> pr1 = HL7.Message.segment(message, "PR1", 0) iex> 1 = pr1.set_id iex> pr1 = HL7.Message.segment(message, "PR1", 1) iex> 2 = pr1.set_id

Link to this function

segment_count(segments, segment_id)

segment_count(t(), HL7.Type.segment_id()) :: non_neg_integer()

Return the number of segments with a specified segment ID in an HL7 message.

Examples

iex> 2 = HL7.Message.segment_count(message, "PR1") iex> 0 = HL7.Message.segment_count(message, "OBX")

Link to this function

write(writer, message)

write(HL7.Writer.t(), t()) :: iodata()

Writes a list of HL7 segments into an iolist.

Arguments

  • writer: an HL7.Writer.t holding the state of the writer.

  • message: a list of HL7 segments to be written into the string.

Return value

iolist containing the message in the selected output format.

Examples

Given the message parsed in the HL7.Message.read/2 example you could do:

iex> writer = HL7.Writer.new(output_format: :text, trim: true)
iex> buffer = HL7.Message.write(writer, message)
iex> IO.puts(buffer)

MSH|^~\&|CLIENTHL7|CLI01020304|SERVHL7|PREPAGA^112233^IIN|20120201101155||ZQA^Z02^ZQA_Z02|00XX20120201101155|P|2.4|||ER|SU|ARG
PRD|PS~4600^^HL70454||^^^B||||30123456789^CU
PID|0||1234567890ABC^^^&112233&IIN^HC||unknown
PR1|1||903401^^99DH
AUT||112233||||||1|0
PR1|2||904620^^99DH
AUT||112233||||||1|0