module REX12

Represents a full (raw) EDI file.

currently, the full text of the file is read into memory, but if you use the block form of the methods, then the subsequent ruby objects are created within the block loops so they can be garbage collected

Represents an ANSI X.12 Element

Raised when there is an error parsing the syntax of the document

This classes sole purpose is to read an IO object containing EDI text and turn it into the REX12 component classes.

Represents a collection of all EDI elements between the ST/SE segments of a single EDI document transaction. An EDI document may have multiple transactions in it.

Constants

VERSION

Public Class Methods

document(file_or_io) click to toggle source

Reads EDI data from the given IO object or file path and returns a REX12::Document representing every segment from the given datasource.

@return [REX12::Document] - a REX12 object representing an ordered enumeration of all the segments in the given data source.

# File lib/rex12.rb, line 66
def self.document file_or_io
  val = nil
  with_io(file_or_io) do |io| 
    val = parser.document io
  end
  val
end
each_segment(file_or_io) click to toggle source

Reads EDI data from the given IO object or file path and returns or yields each EDI segement encountered in the data. The ISA segment is returned/yielded as a specialized REX12::IsaSegment subclass of REX12::Segment.

Example:

REX12.each_segment(“path/to/file.edi”) do |segment|

if segment.isa_segment?
  # Do something w/ the ISA
else 
  # Do something w/ some other segment type
end

end

@param [String, IO] - If a String, expected to be the path to a file that can be read. If IO, needs to be able to be read from AND be able to be rewound.

@return [Enumerator<REX12::Segment>] - If no block given, returns an Enumerator for iterating over the Segements in the file @yield [REX12::Segment] - Progressively yields each segment in the given EDI data.

# File lib/rex12.rb, line 51
def self.each_segment file_or_io
  val = nil
  with_io(file_or_io) do |io| 
    if block_given?
      val = parser.each_segment io, &Proc.new
    else
      val = parser.each_segment io
    end
  end
  val
end
each_transaction(file_or_io) click to toggle source

Reads EDI data from the given IO object or file path and returns or yields an enumerator returning every “transaction” in the EDI data.

@param [String, IO] - If a String, expected to be the path to a file that can be read. If IO, needs to be able to be read from AND be able to be rewound.

@return [Enumerator<REX12::Transaction>] - If no block given, returns an Enumerator for iterating over the Transactions in the file @yield [REX12::Transaction] - Progressively yields each transaction in the given EDI data.

# File lib/rex12.rb, line 20
def self.each_transaction file_or_io
  val = nil
  with_io(file_or_io) do |io| 
    if block_given?
      val = parser.each_transaction io, &Proc.new
    else
      val = parser.each_transaction io
    end
  end
  val
end

Private Class Methods

parser() click to toggle source
# File lib/rex12.rb, line 74
def self.parser
  REX12::Parser.new
end
with_io(file_or_io) { |file_or_io| ... } click to toggle source
# File lib/rex12.rb, line 78
def self.with_io file_or_io
  # If we got an actual IO or Tempfile, then use them as given, else we'll assume the
  # value is a path and
  if REX12::Parser.required_io_methods.map {|m| file_or_io.respond_to?(m) }.all?
    yield file_or_io
  else
    path = file_or_io.respond_to?(:path) ? file_or_io.path : file_or_io.to_s

    File.open(path, "r") {|f| yield f }
  end
end