From 2d48ceab698db40719836eb2b157958fd5e33d44 Mon Sep 17 00:00:00 2001 From: mbklein Date: Mon, 1 Mar 2010 18:46:51 +0000 Subject: [PATCH] Fixed sender/recipient initialization issues git-svn-id: svn://svn.open-ils.org/ILS-Contrib/acq_edi/trunk@805 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- lib/edi/mapper.rb | 23 ++++++++++++++++++++++- lib/openils/mapper.rb | 1 + test/map_spec.rb | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/edi/mapper.rb b/lib/edi/mapper.rb index b9ec82489..0f8199fc0 100644 --- a/lib/edi/mapper.rb +++ b/lib/edi/mapper.rb @@ -92,7 +92,28 @@ module EDI::E end def initialize(msg_type, msg_opts = {}, ic_opts = {}) - @ic = EDI::E::Interchange.new(ic_opts || {}) + # Bug in edi4r 0.9 -- sometimes :recipient is used; sometimes :recip. It doesn't + # work. We'll override it. + local_ic_opts = ic_opts.reject { |k,v| [:sender,:recipient].include?(k) } + @ic = EDI::E::Interchange.new(local_ic_opts || {}) + @ic.header.cS002.d0004 = ic_opts[:sender] unless ic_opts[:sender].nil? + @ic.header.cS003.d0010 = ic_opts[:recipient] unless ic_opts[:recipient].nil? + + # Apply any envelope defaults. + ['UNA','UNB','UNZ'].each { |seg| + seg_defs = self.class.defaults[seg] + if seg_defs + seg_defs.each_pair { |cde,defs| + segment = @ic.header[cde].first + unless segment.nil? + defs.each_pair { |de,val| + segment[de][0].value = val + } + end + } + end + } + @message = @ic.new_message( { :msg_type => msg_type, :version => 'D', :release => '96A', :resp_agency => 'UN' }.merge(msg_opts || {}) ) @ic.add(@message,false) end diff --git a/lib/openils/mapper.rb b/lib/openils/mapper.rb index 76cbfc517..a65a16577 100644 --- a/lib/openils/mapper.rb +++ b/lib/openils/mapper.rb @@ -8,6 +8,7 @@ module OpenILS end OpenILS::Mapper.defaults = { + 'UNB' => { 'S002' => { '0007' => '31B' }, 'S003' => { '0007' => '31B' } }, 'BGM' => { 'C002' => { '1001' => 220 }, '1225' => 9 }, 'DTM' => { 'C507' => { '2005' => 137, '2379' => 102 } }, 'NAD' => { 'C082' => { '3055' => '31B' } }, diff --git a/test/map_spec.rb b/test/map_spec.rb index 63cf3feb2..447c48258 100644 --- a/test/map_spec.rb +++ b/test/map_spec.rb @@ -41,6 +41,11 @@ describe EDI::E::Mapper do }.should raise_error(TypeError) end + it "should properly fill in interchange envelope defaults" do + map = EDI::E::Mapper.new('ORDERS', nil, {:sender => '123456', :recipient => '654321'}) + map.to_s.should =~ /UNA:\+\.\? 'UNB\+UNOB:3\+123456\+654321\+[0-9]{6}:[0-9]{4}\+1'UNH\+1\+ORDERS:D:96A:UN'UNT\+2\+1'UNZ\+1\+1'/ + end + it "should add multiple elements in tuple form" do @map.add( 'BGM', { 'C002' => { '1001' => 220 }, '1004' => '12345678', '1225' => 9 }, -- 2.11.0