class Edifunct::Parser

The main entry point to converting a raw EDIFACT message into a structured object. The structure is dictated by the provided schema.

Attributes

schema[R]
segments[R]

Public Class Methods

new(message, schema:, tokenizer: nil) click to toggle source
# File lib/edifunct/parser.rb, line 10
def initialize(message, schema:, tokenizer: nil)
  @message = message
  @schema = schema
  @tokenizer = tokenizer || Tokenizer.for_message(message)
end

Public Instance Methods

as_root_group(root_group_tag = "<root>") click to toggle source
# File lib/edifunct/parser.rb, line 16
def as_root_group(root_group_tag = "<root>")
  @segments = @tokenizer.as_segments(@message)

  SegmentGroup.new(root_group_tag).tap do |root_group|
    parse_group(root_group, schema)
  end
end

Private Instance Methods

current_segment() click to toggle source
# File lib/edifunct/parser.rb, line 49
def current_segment
  segments[0]
end
current_segment_is?(tag) click to toggle source
# File lib/edifunct/parser.rb, line 45
def current_segment_is?(tag)
  current_segment && current_segment.tag == tag
end
parse_group(parent_group, group_schema) click to toggle source
# File lib/edifunct/parser.rb, line 28
def parse_group(parent_group, group_schema)
  group_schema.each do |schema_entry|
    case schema_entry.fetch("type")
    when "segment"
      if current_segment_is?(schema_entry.fetch("segment_tag"))
        parent_group.add_segment(segments.shift)
        parent_group.add_segment(segments.shift) while schema_entry["repeat"] && current_segment_is?(schema_entry.fetch("segment_tag"))
      end
    when "segment_group"
      while current_segment_is?(schema_entry.fetch("content").fetch(0).fetch("segment_tag"))
        group = parent_group.create_group(schema_entry.fetch("group_name"))
        parse_group(group, schema_entry.fetch("content"))
      end
    end
  end
end