Skip to content

Commit 9c7a6ed

Browse files
author
Daniel Tolbert
committed
Merge pull request #3 from bandwidthcom/develop
Fixed issues with serializing of arrays to xml
2 parents 3e0ebda + 8f2267c commit 9c7a6ed

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

lib/bandwidth-iris/client.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def create_connection()
120120
end
121121

122122
def build_xml(data)
123-
doc = build_doc(data)
123+
doc = build_doc(data, data.keys.first.to_s().camelcase(:upper))
124124
doc.values.first.to_xml({:root => doc.keys.first, :skip_types => true, :indent => 0 })
125125
end
126126

@@ -163,15 +163,19 @@ def parse_xml(xml)
163163
process_parsed_doc(doc.values.first)
164164
end
165165

166-
def build_doc(v)
166+
def build_doc(v, name)
167167
case
168168
when v.is_a?(Array)
169-
v.map {|i| build_doc(i)}
169+
list = v.map {|i| build_doc(i, name)}
170+
list.extend(XmlArraySerializer)
171+
list.element_name = name
172+
list
170173
when v.is_a?(Hash)
171174
result = {}
172175
v.each do |k, val|
173176
if k[0] != '_'
174-
result[v["_#{k}XmlElement"] || (k.to_s().camelcase(:upper))] = build_doc(val)
177+
element_name = v["_#{k}XmlElement"] || (k.to_s().camelcase(:upper))
178+
result[element_name] = build_doc(val, element_name)
175179
end
176180
end
177181
result
@@ -229,4 +233,23 @@ def find_first_descendant v, name
229233
result
230234
end
231235
end
236+
237+
module XmlArraySerializer
238+
def element_name=(name)
239+
@element_name = name
240+
end
241+
242+
def to_xml(options = {})
243+
builder = options[:builder]
244+
self.map do |item|
245+
if item.class.method_defined?(:to_xml)
246+
builder << item.to_xml({skip_instruct: true, root: @element_name, skip_types: true, indent:0})
247+
else
248+
builder << "<#{@element_name}>#{item}</#{@element_name}>"
249+
end
250+
end
251+
builder
252+
end
253+
end
232254
end
255+

lib/bandwidth-iris/lnp_checker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ class LnpChecker
55
extend ClientWrapper
66

77
def self.check(client, numbers, full_check = false)
8+
list = if numbers.is_a?(Array) then numbers else [numbers] end
89
data = {
910
:number_portability_request => {
10-
:tn_list => {:tn => numbers}
11+
:tn_list => {:tn => list}
1112
}
1213
}
1314
client.make_request(:post, "#{client.concat_account_path(LNP_CHECKER_PATH)}?fullCheck=#{full_check}", data)[0]

lib/bandwidth-iris/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module BandwidthIris
22
# Version of this gem
3-
VERSION = "1.0.1"
3+
VERSION = "1.0.2"
44
end

spec/bandwidth-iris/client_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,17 @@
123123
end
124124
end
125125
end
126+
127+
describe '#build_xml' do
128+
client = nil
129+
before :each do
130+
client = Helper.get_client()
131+
end
132+
133+
it 'should generate valid XML' do
134+
expect(client.build_xml({root: {item: {value: '123'}}})).to eql('<?xml version="1.0" encoding="UTF-8"?><Root><Item><Value>123</Value></Item></Root>')
135+
expect(client.build_xml({root: {item: [1,2,3]}})).to eql('<?xml version="1.0" encoding="UTF-8"?><Root><Item>1</Item><Item>2</Item><Item>3</Item></Root>')
136+
expect(client.build_xml({root: {item: [{value: '111'}, {value: '222'}]}})).to eql('<?xml version="1.0" encoding="UTF-8"?><Root><Item><Value>111</Value></Item><Item><Value>222</Value></Item></Root>')
137+
expect(client.build_xml({root: {item: [{value: [1,2,3]}, {value: '222'}]}})).to eql('<?xml version="1.0" encoding="UTF-8"?><Root><Item><Value>1</Value><Value>2</Value><Value>3</Value></Item><Item><Value>222</Value></Item></Root>')
138+
end
139+
end

spec/bandwidth-iris/lnp_checker_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
expect(result[:supported_rate_centers][:rate_center_group][:city]).to eql("City1")
2020
expect(result[:supported_rate_centers][:rate_center_group][:state]).to eql("State1")
2121
end
22+
23+
it 'should allow to use 1 number as string' do
24+
number = '1111'
25+
data = {:number_portability_request => {:tn_list => {:tn => number}}}
26+
client.stubs.post('/v1.0/accounts/accountId/lnpchecker?fullCheck=true', client.build_xml(data)) {|env| [200, {}, Helper.xml['lnp_check']]}
27+
result = LnpChecker.check(client, number, true)
28+
expect(result[:supported_rate_centers][:rate_center_group][:rate_center]).to eql("Center1")
29+
expect(result[:supported_rate_centers][:rate_center_group][:city]).to eql("City1")
30+
expect(result[:supported_rate_centers][:rate_center_group][:state]).to eql("State1")
31+
end
2232
end
2333

2434
end

0 commit comments

Comments
 (0)