module Eancom module Parser class Document class SegmentTypeNotDefined < StandardError; end attr_reader :file, :document def initialize(file:) @file = file @document = Eancom::Edifact::Document.new end def parse content.split(segment_delimiter).each do |segment_string| segment = Parser::Segment.new(segment_string) segment = segment.parse add(segment) end @document end private def add(segment) if segment.is_header? @document.add_to_header(segment) elsif segment.is_body? @document.add_to_body(segment) elsif segment.is_footer? @document.add_to_body(segment) else raise SegmentTypeNotDefined end end def content @content ||= begin @file.rewind string = convert(@file.read) string.delete!("\n") string.delete!("\r") string.chomp! string end end def composite_delimiter DELIMITERS[:data] end def segment_delimiter DELIMITERS[:segment] end def convert(string) string.encode('UTF-8') end end end end