From b8e0f8a2d765057efe05d51c9e82b380c6f2c824 Mon Sep 17 00:00:00 2001 From: mbklein Date: Mon, 1 Mar 2010 20:08:40 +0000 Subject: [PATCH] Mapper#from_json() can now do everything from within JSON with no external args git-svn-id: svn://svn.open-ils.org/ILS-Contrib/acq_edi/trunk@807 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- lib/edi/mapper.rb | 26 +++++++++++++++++++++++--- test/openils_map_spec.rb | 4 +++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/edi/mapper.rb b/lib/edi/mapper.rb index 1abfa0695..bb2500377 100644 --- a/lib/edi/mapper.rb +++ b/lib/edi/mapper.rb @@ -85,9 +85,29 @@ module EDI::E 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 diff --git a/test/openils_map_spec.rb b/test/openils_map_spec.rb index bd87d5df9..5cbf73f8f 100644 --- a/test/openils_map_spec.rb +++ b/test/openils_map_spec.rb @@ -32,8 +32,10 @@ describe OpenILS::Mapper 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 = 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 -- 2.11.0