openils-mapper 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,22 +9,39 @@ class Collection
9
9
  result = {}
10
10
 
11
11
  self.each { |child|
12
+ if self[child.name].length > 1 and result[child.name].nil?
13
+ result[child.name] = []
14
+ end
12
15
  if child.is_a?(Collection)
16
+ # Data elements first
13
17
  hash = child.to_hash
14
- result[child.name] = hash unless hash.empty?
15
- unless self.children.empty?
16
- segments = []
17
- self.children.each { |segment|
18
- segments << [segment.name, segment.to_hash]
19
- }
20
- result[self.sg_name] = segments
18
+ unless hash.empty?
19
+ if result[child.name].is_a?(Array)
20
+ result[child.name] << hash
21
+ else
22
+ result[child.name] = hash
23
+ end
21
24
  end
22
25
  else
23
26
  unless child.value.nil?
24
- result[child.name] = child.value
27
+ if result[child.name].is_a?(Array)
28
+ result[child.name] << child.value
29
+ else
30
+ result[child.name] = child.value
31
+ end
25
32
  end
26
33
  end
27
34
  }
35
+
36
+ # Segment groups last
37
+ if self.respond_to?(:children) and (self.children.empty? == false)
38
+ segments = []
39
+ self.children.each { |segment|
40
+ segments << [segment.name, segment.to_hash]
41
+ }
42
+ result[self.sg_name] = segments
43
+ end
44
+
28
45
  result
29
46
  end
30
47
 
@@ -40,14 +57,18 @@ class Interchange
40
57
 
41
58
  messages = []
42
59
  self.each { |message|
43
- messages << [message.name, message.to_hash]
60
+ messages << {message.name => message.to_hash}
44
61
  }
45
62
 
46
63
  {
47
- 'UNA' => self.una.to_s,
48
- 'header' => [self.header.name, self.header.to_hash],
49
- 'body' => messages,
50
- 'trailer' => [self.trailer.name, self.trailer.to_hash]
64
+ 'UNA' => self.una.to_hash,
65
+ 'sender' => self.header.cS002.d0004,
66
+ 'sender_qual' => self.header.cS002.d0007,
67
+ 'recipient' => self.header.cS003.d0010,
68
+ 'recipient_qual' => self.header.cS003.d0007,
69
+ 'header' => [self.header.name, self.header.to_hash],
70
+ 'body' => messages,
71
+ 'trailer' => [self.trailer.name, self.trailer.to_hash]
51
72
  }
52
73
  end
53
74
 
@@ -57,20 +78,26 @@ class Message
57
78
 
58
79
  def to_hash
59
80
  segments = []
60
-
81
+ segments << [self.header.name, self.header.to_hash]
61
82
  self.find_all { |segment|
62
83
  segment.level < 2
63
84
  }.each { |segment|
64
85
  segments << [segment.name, segment.to_hash]
65
86
  }
66
87
  segments << [self.trailer.name, self.trailer.to_hash]
67
- {
68
- 'header' => [self.header.name, self.header.to_hash],
69
- 'body' => segments,
70
- 'trailer' => [self.trailer.name, self.trailer.to_hash]
71
- }
88
+ segments
72
89
  end
73
90
 
74
91
  end
75
92
 
93
+ class E::UNA
94
+ def to_hash
95
+ result = {}
96
+ [:ce_sep,:de_sep,:decimal_sign,:esc_char,:rep_sep,:seg_term].each { |field|
97
+ result[field.to_s] = self.send(field).chr
98
+ }
99
+ result
100
+ end
101
+ end
102
+
76
103
  end
@@ -22,14 +22,14 @@ module EDI::E
22
22
  class Mapper
23
23
  extend Forwardable
24
24
 
25
- attr :message
25
+ attr :message, :ic
26
26
  attr_accessor :defaults
27
- def_delegators :@ic, :charset, :empty?, :groups_created, :header,
28
- :illegal_charset_pattern, :inspect, :is_iedi?, :messages_created,
29
- :output_mode, :output_mode=, :show_una, :show_una=, :syntax, :to_s,
30
- :to_xml, :to_xml_header, :to_xml_trailer, :trailer, :una, :validate,
31
- :version
32
-
27
+ # def_delegators :@ic, :charset, :empty?, :groups_created, :header,
28
+ # :illegal_charset_pattern, :inspect, :is_iedi?, :messages_created,
29
+ # :output_mode, :output_mode=, :show_una, :show_una=, :syntax, :to_s,
30
+ # :to_xml, :to_xml_header, :to_xml_trailer, :trailer, :una, :validate,
31
+ # :version
32
+
33
33
  class << self
34
34
  def defaults
35
35
  @defaults || {}
@@ -106,19 +106,46 @@ module EDI::E
106
106
  struct['msg_opts'].each_pair { |k,v| json_msg_opts[k.to_sym] = v }
107
107
  end
108
108
 
109
- result = self.new(struct['msg_type'], json_msg_opts, ic_opts.merge(json_opts))
110
- result.add(struct['msg'])
109
+ result = self.new(ic_opts.merge(json_opts))
110
+
111
+ ['header','trailer'].each { |envseg|
112
+ if struct[envseg]
113
+ target = result.send(envseg.to_sym)
114
+ struct[envseg].last.each_pair { |de,val|
115
+ if val.is_a?(Hash)
116
+ val.each_pair { |cde,sval|
117
+ target[de][0][cde][0].value = sval
118
+ }
119
+ else
120
+ target[de][0].value = val
121
+ end
122
+ }
123
+ end
124
+ }
125
+
126
+ struct['body'].each { |msg_def|
127
+ msg_def.each_pair { |msg_type, msg_body|
128
+ if unh = msg_body.find { |s| s[0] == 'UNH' }
129
+ version_info = unh[1]['S009']
130
+ json_msg_opts[:resp_agency] = version_info['0051']
131
+ json_msg_opts[:version] = version_info['0052']
132
+ json_msg_opts[:release] = version_info['0054']
133
+ end
134
+ result.add_message(msg_type, json_msg_opts)
135
+ result.add(msg_body)
136
+ }
137
+ }
111
138
  result.finalize
112
139
  end
113
140
 
114
- def initialize(msg_type, msg_opts = {}, ic_opts = {})
141
+ def initialize(ic_opts = {})
115
142
  # Bug in edi4r 0.9 -- sometimes :recipient is used; sometimes :recip. It doesn't
116
143
  # work. We'll override it.
117
144
  local_ic_opts = ic_opts.reject { |k,v| [:sender,:sender_qual,:recipient,:recipient_qual].include?(k) }
118
145
  @ic = EDI::E::Interchange.new(local_ic_opts || {})
119
146
 
120
147
  # Apply any envelope defaults.
121
- ['UNA','UNB','UNZ'].each { |seg|
148
+ ['UNB','UNZ'].each { |seg|
122
149
  seg_defs = self.class.defaults[seg]
123
150
  if seg_defs
124
151
  seg_defs.each_pair { |cde,defs|
@@ -136,7 +163,9 @@ module EDI::E
136
163
  @ic.header.cS002.d0007 = ic_opts[:sender_qual] unless ic_opts[:sender_qual].nil?
137
164
  @ic.header.cS003.d0010 = ic_opts[:recipient] unless ic_opts[:recipient].nil?
138
165
  @ic.header.cS003.d0007 = ic_opts[:recipient_qual] unless ic_opts[:recipient_qual].nil?
139
-
166
+ end
167
+
168
+ def add_message(msg_type, msg_opts = {})
140
169
  @message = @ic.new_message( { :msg_type => msg_type, :version => 'D', :release => '96A', :resp_agency => 'UN' }.merge(msg_opts || {}) )
141
170
  @ic.add(@message,false)
142
171
  end
@@ -166,16 +195,26 @@ module EDI::E
166
195
  @ic.to_s
167
196
  end
168
197
 
198
+ def method_missing(sym, *args)
199
+ if @ic.respond_to?(sym)
200
+ @ic.send(sym, *args)
201
+ else
202
+ super(sym, *args)
203
+ end
204
+ end
205
+
169
206
  private
170
207
  def add_segment(seg_name, value)
171
208
  if seg_name =~ /^[A-Z]{3}$/
172
- seg = @message.new_segment(seg_name)
173
- @message.add(seg)
174
- default = self.class.defaults[seg_name]
175
- data = default.nil? ? value : default.merge(value)
176
- data.each_pair { |de,val|
177
- add_element(seg,de,val,default)
178
- }
209
+ if seg_name !~ /^UN[HT]$/
210
+ seg = @message.new_segment(seg_name)
211
+ @message.add(seg)
212
+ default = self.class.defaults[seg_name]
213
+ data = default.nil? ? value : default.merge(value)
214
+ data.each_pair { |de,val|
215
+ add_element(seg,de,val,default)
216
+ }
217
+ end
179
218
  else
180
219
  apply_mapping(seg_name, value)
181
220
  end
@@ -184,7 +223,9 @@ module EDI::E
184
223
  def add_element(parent, de, value, default)
185
224
  default = default[de] unless default.nil?
186
225
 
187
- if value.is_a?(Hash)
226
+ if de =~ /^SG[0-9]+$/
227
+ value.each { |v| self.add(*v) }
228
+ elsif value.is_a?(Hash)
188
229
  new_parent = parent.send("c#{de}")
189
230
  data = default.nil? ? value : default.merge(value)
190
231
  data.each_pair { |k,v| add_element(new_parent,k,v,default) }
@@ -3,7 +3,7 @@ require 'edi/mapper'
3
3
  module OpenILS
4
4
 
5
5
  class Mapper < EDI::E::Mapper
6
- VERSION = '0.8.2'
6
+ VERSION = '0.9.0'
7
7
  end
8
8
 
9
9
  end
@@ -5,7 +5,8 @@ require 'edi/edi2json'
5
5
  describe EDI::E::Mapper do
6
6
 
7
7
  before(:each) do
8
- @map = EDI::E::Mapper.new('ORDERS')
8
+ @map = EDI::E::Mapper.new
9
+ @map.add_message('ORDERS')
9
10
  end
10
11
 
11
12
  it "should chunk text" do
@@ -42,7 +43,8 @@ describe EDI::E::Mapper do
42
43
  end
43
44
 
44
45
  it "should properly fill in interchange envelope defaults" do
45
- map = EDI::E::Mapper.new('ORDERS', nil, {:sender => '123456', :recipient => '654321'})
46
+ map = EDI::E::Mapper.new({:sender => '123456', :recipient => '654321'})
47
+ map.add_message('ORDERS')
46
48
  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'/
47
49
  end
48
50
 
@@ -112,7 +114,7 @@ describe EDI::E::Mapper do
112
114
  }
113
115
  # Can't compare everything because of timestamping, so we'll just compare
114
116
  # the bodies for a high degree of confidence
115
- 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"}]}]]
117
+ interchange.to_hash['body'].should == [{"ORDERS"=>[["UNH", {"S009"=>{"0052"=>"D", "0054"=>"96A", "0065"=>"ORDERS", "0051"=>"UN"}, "0062"=>"1"}], ["BGM", {"1225"=>"9", "C002"=>{"1001"=>"220"}, "1004"=>"2"}], ["DTM", {"C507"=>{"2379"=>"102", "2380"=>"20090331", "2005"=>"137"}}], ["NAD", {"C080"=>{"3036"=>[]}, "C058"=>{"3124"=>[]}, "C059"=>{"3042"=>[]}, "C082"=>{"3039"=>"3472205", "3055"=>"91"}, "SG2"=>[["RFF", {"C506"=>{"1153"=>"API", "1154"=>"3472205 0001"}}]], "3035"=>"BY"}], ["NAD", {"C080"=>{"3036"=>[]}, "C058"=>{"3124"=>[]}, "C059"=>{"3042"=>[]}, "C082"=>{"3039"=>"3472205", "3055"=>"31B"}, "SG2"=>[["RFF", {"C506"=>{"1153"=>"API", "1154"=>"3472205 0001"}}]], "3035"=>"BY"}], ["NAD", {"C080"=>{"3036"=>[]}, "C058"=>{"3124"=>[]}, "C059"=>{"3042"=>[]}, "C082"=>{"3039"=>"1556150", "3055"=>"31B"}, "3035"=>"SU"}], ["NAD", {"C080"=>{"3036"=>[]}, "C058"=>{"3124"=>[]}, "C059"=>{"3042"=>[]}, "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"}]]}]
116
118
  end
117
119
 
118
120
  end
@@ -3,7 +3,8 @@ require 'openils/mapper'
3
3
  describe OpenILS::Mapper do
4
4
 
5
5
  before(:each) do
6
- @map = OpenILS::Mapper.new('ORDERS')
6
+ @map = OpenILS::Mapper.new
7
+ @map.add_message('ORDERS')
7
8
  end
8
9
 
9
10
  it "should add both qualified and unqualified buyer/vendor fields" do
@@ -32,7 +33,7 @@ describe OpenILS::Mapper do
32
33
 
33
34
  it "should create a message from high-level JEDI input" do
34
35
  json = File.read(File.join(File.dirname(__FILE__), 'test_po.json'))
35
- @map = OpenILS::Mapper.from_json(%{{ "msg_type": "ORDERS", "msg": #{json}, "sender": "123456", "recipient": {"id": "999999999", "id-qualifier": "1"}}})
36
+ @map = OpenILS::Mapper.from_json(%{{ "body": [{ "ORDERS": #{json}}], "sender": "123456", "recipient": {"id": "999999999", "id-qualifier": "1"}}})
36
37
  @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'"
37
38
  @map.header.cS002.to_s.should == "123456:31B"
38
39
  @map.header.cS003.to_s.should == "999999999:1"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openils-mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael B. Klein
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-01 00:00:00 -08:00
12
+ date: 2010-03-03 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency