[open-ils-commits] r239 - in acq_edi/trunk: lib/edi test (mbklein)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Apr 1 12:24:14 EDT 2009


Author: mbklein
Date: 2009-04-01 12:24:10 -0400 (Wed, 01 Apr 2009)
New Revision: 239

Added:
   acq_edi/trunk/lib/edi/edi2json.rb
   acq_edi/trunk/test/test_po.edi
Modified:
   acq_edi/trunk/lib/edi/mapper.rb
   acq_edi/trunk/test/map_spec.rb
   acq_edi/trunk/test/openils_map_spec.rb
Log:
Added na?\195?\175ve translation of EDIFACT to JEDI

Added: acq_edi/trunk/lib/edi/edi2json.rb
===================================================================
--- acq_edi/trunk/lib/edi/edi2json.rb	                        (rev 0)
+++ acq_edi/trunk/lib/edi/edi2json.rb	2009-04-01 16:24:10 UTC (rev 239)
@@ -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

Modified: acq_edi/trunk/lib/edi/mapper.rb
===================================================================
--- acq_edi/trunk/lib/edi/mapper.rb	2009-04-01 14:33:06 UTC (rev 238)
+++ acq_edi/trunk/lib/edi/mapper.rb	2009-04-01 16:24:10 UTC (rev 239)
@@ -11,10 +11,8 @@
   
 end
 
-module EDI
+module EDI::E
 
-module E
-  
   class Mapper
     
     attr :message
@@ -147,5 +145,4 @@
     
   end
   
-end
 end
\ No newline at end of file

Modified: acq_edi/trunk/test/map_spec.rb
===================================================================
--- acq_edi/trunk/test/map_spec.rb	2009-04-01 14:33:06 UTC (rev 238)
+++ acq_edi/trunk/test/map_spec.rb	2009-04-01 16:24:10 UTC (rev 239)
@@ -1,5 +1,6 @@
 # map_spec.rb
 require 'edi/mapper'
+require 'edi/edi2json'
 
 describe EDI::E::Mapper do
   
@@ -100,4 +101,13 @@
     }.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

Modified: acq_edi/trunk/test/openils_map_spec.rb
===================================================================
--- acq_edi/trunk/test/openils_map_spec.rb	2009-04-01 14:33:06 UTC (rev 238)
+++ acq_edi/trunk/test/openils_map_spec.rb	2009-04-01 16:24:10 UTC (rev 239)
@@ -30,7 +30,7 @@
     @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'"

Added: acq_edi/trunk/test/test_po.edi
===================================================================
--- acq_edi/trunk/test/test_po.edi	                        (rev 0)
+++ acq_edi/trunk/test/test_po.edi	2009-04-01 16:24:10 UTC (rev 239)
@@ -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'



More information about the open-ils-commits mailing list