bin/edi-pp
#!/usr/bin/env ruby
require File.expand_path("../../lib/stupidedi", __FILE__)
require "pp"
# This will be auto-enabled when $stdout.tty?, but -C forces color output
require "term/ansicolor" if ARGV.delete("-C")
if idx = ARGV.index("--format")
ARGV.delete("--format")
format = ARGV.delete_at(idx)
else
format = "tree"
end
unless %w(html tree x12).include?(format)
$stderr.puts "unrecognized format (expected html, tree, or x12)"
exit(1)
end
config = Stupidedi::Config.contrib(Stupidedi::Config.hipaa(Stupidedi::Config.default))
parser = Stupidedi::Parser.build(config)
start = Time.now
ARGV.each do |path|
# Reading the entire input at once is slightly faster than streaming
# from a file handle.
#
# content = File.read(path, :encoding => "ISO-8859-1")
# parser, r = parser.read(Stupidedi::Reader.build(content))
#
reader = Stupidedi::Reader.build(File.read(path))
parser, = parser.read(reader)
end
stop = Time.now
case format
when "tree"
parser.zipper.tap{|z| pp z.root.node }
when "html"
Stupidedi::Writer::Claredi.new(parser.zipper.fetch.root.node).write($stdout)
when "x12"
separators = Stupidedi::Reader::Separators.build \
:segment => "~\n",
:element => "*",
:component => ":",
:repetition => "^"
Stupidedi::Writer::Default.new(parser.zipper.fetch.root, separators).write($stdout)
end
begin
a = parser.first
b = parser.last
a.flatmap{|a1| b.flatmap{|b1| a1.distance(b1) }}.
tap{|d| $stderr.puts "#{d + 1} segments" }
end
$stderr.puts "%0.3f seconds" % (stop - start)