From 1810ba1ad8625d3b091a137aa95839d7f7faed8c Mon Sep 17 00:00:00 2001 From: mbklein Date: Wed, 1 Apr 2009 16:24:10 +0000 Subject: [PATCH] =?utf8?q?Added=20na=C3=AFve=20translation=20of=20EDIFACT?= =?utf8?q?=20to=20JEDI?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: svn://svn.open-ils.org/ILS-Contrib/acq_edi/trunk@239 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- lib/edi/edi2json.rb | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/edi/mapper.rb | 5 +--- test/map_spec.rb | 10 +++++++ test/openils_map_spec.rb | 2 +- test/test_po.edi | 36 +++++++++++++++++++++++ 5 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 lib/edi/edi2json.rb create mode 100644 test/test_po.edi diff --git a/lib/edi/edi2json.rb b/lib/edi/edi2json.rb new file mode 100644 index 000000000..5cb8ad8aa --- /dev/null +++ b/lib/edi/edi2json.rb @@ -0,0 +1,76 @@ +require 'json' + +module EDI + +class Collection + include Enumerable + + def to_hash + result = {} + + self.each { |child| + if child.is_a?(Collection) + hash = child.to_hash + result[child.name] = hash unless hash.empty? + unless self.children.empty? + segments = [] + self.children.each { |segment| + segments << [segment.name, segment.to_hash] + } + result[self.sg_name] = segments + end + else + unless child.value.nil? + result[child.name] = child.value + end + end + } + result + end + + def to_json(*a) + self.to_hash.to_json(*a) + end + +end + +class Interchange + + def to_hash + + messages = [] + self.each { |message| + messages << [message.name, message.to_hash] + } + + { + 'UNA' => self.una.to_s, + 'header' => [self.header.name, self.header.to_hash], + 'body' => messages, + 'trailer' => [self.trailer.name, self.trailer.to_hash] + } + end + +end + +class Message + + def to_hash + segments = [] + + self.find_all { |segment| + segment.level < 2 + }.each { |segment| + segments << [segment.name, segment.to_hash] + } + segments << [self.trailer.name, self.trailer.to_hash] + { + 'header' => [self.header.name, self.header.to_hash], + 'body' => segments, + 'trailer' => [self.trailer.name, self.trailer.to_hash] + } + end + +end + +end \ No newline at end of file diff --git a/lib/edi/mapper.rb b/lib/edi/mapper.rb index 0ef23fcec..3400a59ef 100644 --- a/lib/edi/mapper.rb +++ b/lib/edi/mapper.rb @@ -11,10 +11,8 @@ class String end -module EDI +module EDI::E -module E - class Mapper attr :message @@ -147,5 +145,4 @@ module E end -end end \ No newline at end of file diff --git a/test/map_spec.rb b/test/map_spec.rb index 9d925bed1..63cf3feb2 100644 --- a/test/map_spec.rb +++ b/test/map_spec.rb @@ -1,5 +1,6 @@ # map_spec.rb require 'edi/mapper' +require 'edi/edi2json' describe EDI::E::Mapper do @@ -100,4 +101,13 @@ describe EDI::E::Mapper do }.should raise_error(NameError) end + it "should correctly generate a low-level JEDI hash from an EDIFACT message" do + interchange = File.open(File.join(File.dirname(__FILE__), 'test_po.edi')) { |f| + EDI::E::Interchange.parse(f) + } + # Can't compare everything because of timestamping, so we'll just compare + # the bodies for a high degree of confidence + interchange.to_hash['body'].should == [["ORDERS", {"trailer"=>["UNT", {"0074"=>33, "0062"=>"1"}], "body"=>[["BGM", {"C002"=>{"1001"=>"220"}, "1225"=>"9", "1004"=>"2"}], ["DTM", {"C507"=>{"2005"=>"137", "2380"=>"20090331", "2379"=>"102"}}], ["NAD", {"C082"=>{"3039"=>"3472205", "3055"=>"91"}, "SG2"=>[["RFF", {"C506"=>{"1153"=>"API", "1154"=>"3472205 0001"}}]], "3035"=>"BY"}], ["NAD", {"C082"=>{"3039"=>"3472205", "3055"=>"31B"}, "SG2"=>[["RFF", {"C506"=>{"1153"=>"API", "1154"=>"3472205 0001"}}]], "3035"=>"BY"}], ["NAD", {"C082"=>{"3039"=>"1556150", "3055"=>"31B"}, "3035"=>"SU"}], ["NAD", {"C082"=>{"3039"=>"1556150", "3055"=>"91"}, "SG2"=>[["RFF", {"C506"=>{"1153"=>"IA", "1154"=>"1865"}}]], "3035"=>"SU"}], ["CUX", {"C504"=>{"6345"=>"USD", "6347"=>"2", "6343"=>"9"}}], ["LIN", {"SG25"=>[["PIA", {"C212"=>{"7140"=>"03-0010837", "7143"=>"SA"}, "4347"=>"5"}], ["IMD", {"C273"=>{"7008"=>"Discernment"}, "7077"=>"F", "7081"=>"BTI"}], ["IMD", {"C273"=>{"7008"=>"Concord Records,"}, "7077"=>"F", "7081"=>"BPU"}], ["IMD", {"C273"=>{"7008"=>"1986."}, "7077"=>"F", "7081"=>"BPD"}], ["IMD", {"C273"=>{"7008"=>"1 sound disc :"}, "7077"=>"F", "7081"=>"BPH"}], ["QTY", {"C186"=>{"6060"=>2, "6063"=>"21"}}], ["PRI", {"C509"=>{"5125"=>"AAB", "5118"=>35.95}}], ["RFF", {"C506"=>{"1153"=>"LI", "1154"=>"2/1"}}]], "1082"=>1}], ["LIN", {"SG25"=>[["PIA", {"C212"=>{"7140"=>"03-0010840", "7143"=>"SA"}, "4347"=>"5"}], ["IMD", {"C273"=>{"7008"=>"The inner source"}, "7077"=>"F", "7081"=>"BTI"}], ["IMD", {"C273"=>{"7008"=>"Duke, George, 1946-"}, "7077"=>"F", "7081"=>"BAU"}], ["IMD", {"C273"=>{"7008"=>"MPS Records,"}, "7077"=>"F", "7081"=>"BPU"}], ["IMD", {"C273"=>{"7008"=>"1973."}, "7077"=>"F", "7081"=>"BPD"}], ["IMD", {"C273"=>{"7008"=>"2 sound discs :"}, "7077"=>"F", "7081"=>"BPH"}], ["QTY", {"C186"=>{"6060"=>1, "6063"=>"21"}}], ["PRI", {"C509"=>{"5125"=>"AAB", "5118"=>28.95}}], ["RFF", {"C506"=>{"1153"=>"LI", "1154"=>"2/2"}}]], "1082"=>2}], ["UNS", {"0081"=>"S"}], ["CNT", {"C270"=>{"6069"=>"2", "6066"=>2}}], ["UNT", {"0074"=>33, "0062"=>"1"}]], "header"=>["UNH", {"S009"=>{"0052"=>"D", "0065"=>"ORDERS", "0054"=>"96A", "0051"=>"UN"}, "0062"=>"1"}]}]] + end + end \ No newline at end of file diff --git a/test/openils_map_spec.rb b/test/openils_map_spec.rb index 32054cb81..bd87d5df9 100644 --- a/test/openils_map_spec.rb +++ b/test/openils_map_spec.rb @@ -30,7 +30,7 @@ describe OpenILS::Mapper do @map.message.to_s.should == "UNH+1+ORDERS:D:96A:UN'IMD+F+BAU+:::Campbell, James'IMD+F+BTI+:::The Ghost Mountain boys ?: their epi:c march and the terrifying battle f'IMD+F+BTI+:::or New Guinea -- the forgotten war :of the South Pacific'IMD+F+BPU+:::Crown Publishers'IMD+F+BPD+:::2007'UNT+7+1'" end - it "should create a message from JSON input" do + it "should create a message from high-level JEDI input" do json = File.read(File.join(File.dirname(__FILE__), 'test_po.json')) @map = OpenILS::Mapper.from_json('ORDERS',json) @map.message.to_s.should == "UNH+1+ORDERS:D:96A:UN'BGM+220+2+9'DTM+137:20090331:102'NAD+BY+3472205::91'RFF+API:3472205 0001'NAD+BY+3472205::31B'RFF+API:3472205 0001'NAD+SU+1556150::31B'NAD+SU+1556150::91'RFF+IA:1865'CUX+2:USD:9'LIN+1'PIA+5+03-0010837:SA'IMD+F+BTI+:::Discernment'IMD+F+BPU+:::Concord Records,'IMD+F+BPD+:::1986.'IMD+F+BPH+:::1 sound disc ?:'QTY+21:2'PRI+AAB:35.95'RFF+LI:2/1'LIN+2'PIA+5+03-0010840:SA'IMD+F+BTI+:::The inner source'IMD+F+BAU+:::Duke, George, 1946-'IMD+F+BPU+:::MPS Records,'IMD+F+BPD+:::1973.'IMD+F+BPH+:::2 sound discs ?:'QTY+21:1'PRI+AAB:28.95'RFF+LI:2/2'UNS+S'CNT+2:2'UNT+33+1'" diff --git a/test/test_po.edi b/test/test_po.edi new file mode 100644 index 000000000..7a297e956 --- /dev/null +++ b/test/test_po.edi @@ -0,0 +1,36 @@ +UNA:+.? ' +UNB+UNOB:3+++090401:1152+1' +UNH+1+ORDERS:D:96A:UN' +BGM+220+2+9' +DTM+137:20090331:102' +NAD+BY+3472205::91' +RFF+API:3472205 0001' +NAD+BY+3472205::31B' +RFF+API:3472205 0001' +NAD+SU+1556150::31B' +NAD+SU+1556150::91' +RFF+IA:1865' +CUX+2:USD:9' +LIN+1' +PIA+5+03-0010837:SA' +IMD+F+BTI+:::Discernment' +IMD+F+BPU+:::Concord Records,' +IMD+F+BPD+:::1986.' +IMD+F+BPH+:::1 sound disc ?:' +QTY+21:2' +PRI+AAB:35.95' +RFF+LI:2/1' +LIN+2' +PIA+5+03-0010840:SA' +IMD+F+BTI+:::The inner source' +IMD+F+BAU+:::Duke, George, 1946-' +IMD+F+BPU+:::MPS Records,' +IMD+F+BPD+:::1973.' +IMD+F+BPH+:::2 sound discs ?:' +QTY+21:1' +PRI+AAB:28.95' +RFF+LI:2/2' +UNS+S' +CNT+2:2' +UNT+33+1' +UNZ+1+1' -- 2.11.0