Sha256: fbb4a15f3f42975a2bd16067f4165de9e595cefb449b892330c50efb3dbc0d84

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module EdiParser
  class EdiFile

    # @return [EdiParser::InterchangeHeader] the parsed interchange header object.
    attr_accessor :interchange_header

    # @return [EdiParser::DocumentHeader] the parsed document header object.
    attr_accessor :document_header

    # @return [EdiParser::CarrierData] the parsed carrier data object.
    attr_accessor :carrier_data

    # @return [Array<EdiParser::Ocurrence>] the array of parse ocurrences object.
    attr_accessor :ocurrences

    def initialize(attributes = {})
      attributes.each do |key, value|
        __send__("#{key}=", value)
      end
    end

    def self.parse(file)
      raise InvalidEdiFileError unless File.exists?(file)

      edi_file = EdiFile.new

      File.open(file) do |f|
        edi_file.interchange_header = InterchangeHeader.parse(f.readline)
        edi_file.document_header = DocumentHeader.parse(f.readline)
        edi_file.carrier_data = CarrierData.parse(f.readline)

        edi_file.ocurrences = []
        f.readlines.each do |line|
          ocurrence = Ocurrence.parse(line)
          edi_file.ocurrences << ocurrence unless ocurrence.nil?
        end
      end

      edi_file
    end
  end

  class InvalidEdiFileError < StandardError; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
edi_parser-0.0.1 lib/edi_parser/edi_file.rb