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

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Mar 1 15:08:44 EST 2010


Author: mbklein
Date: 2010-03-01 15:08:40 -0500 (Mon, 01 Mar 2010)
New Revision: 807

Modified:
   acq_edi/trunk/lib/edi/mapper.rb
   acq_edi/trunk/test/openils_map_spec.rb
Log:
Mapper#from_json() can now do everything from within JSON with no external args

Modified: acq_edi/trunk/lib/edi/mapper.rb
===================================================================
--- acq_edi/trunk/lib/edi/mapper.rb	2010-03-01 18:51:03 UTC (rev 806)
+++ acq_edi/trunk/lib/edi/mapper.rb	2010-03-01 20:08:40 UTC (rev 807)
@@ -85,9 +85,29 @@
       handler[:proc].call(self, name, value)
     end
     
-    def self.from_json(msg_type, json, msg_opts = {}, ic_opts = {})
-      result = self.new(msg_type, msg_opts, ic_opts)
-      result.add(JSON.parse(json))
+    def self.from_json(json, ic_opts = {})
+      struct = JSON.parse(json)
+
+      json_opts = {}
+      [:sender,:recipient].each { |party|
+        party_info = struct[party.to_s]
+        if party_info.is_a?(Hash)
+          json_opts[party] = party_info['id']
+          json_opts["#{party}_qual".to_sym] = party_info['id-qualifier']
+        elsif party_info.is_a?(String)
+          (id,qual) = party_info.split(/:/)
+          json_opts[party] = id
+          json_opts["#{party}_qual".to_sym] = qual
+        end
+      }
+      
+      json_msg_opts = {}
+      if struct['msg_opts'].is_a?(Hash)
+        struct['msg_opts'].each_pair { |k,v| json_msg_opts[k.to_sym] = v }
+      end
+      
+      result = self.new(struct['msg_type'], json_msg_opts, ic_opts.merge(json_opts))
+      result.add(struct['msg'])
       result.finalize
     end
 

Modified: acq_edi/trunk/test/openils_map_spec.rb
===================================================================
--- acq_edi/trunk/test/openils_map_spec.rb	2010-03-01 18:51:03 UTC (rev 806)
+++ acq_edi/trunk/test/openils_map_spec.rb	2010-03-01 20:08:40 UTC (rev 807)
@@ -32,8 +32,10 @@
 
   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 = OpenILS::Mapper.from_json(%{{ "msg_type": "ORDERS", "msg": #{json}, "sender": "123456", "recipient": {"id": "999999999", "id-qualifier": "1"}}})
     @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'"
+    @map.header.cS002.to_s.should == "123456:31B"
+    @map.header.cS003.to_s.should == "999999999:1"
   end
   
 end
\ No newline at end of file



More information about the open-ils-commits mailing list