Skip to content

Commit 0b4eea3

Browse files
authored
Serialize list (#367)
* Add serializer for list types * Explicitely check for an Array type
1 parent be2d7cd commit 0b4eea3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/twilio-ruby/framework/serialize.rb

+9
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,13 @@ def self.prefixed_collapsible_map(map, prefix)
6767

6868
result
6969
end
70+
71+
def self.serialize_list(input_list)
72+
return input_list unless input_list.is_a? Array
73+
result = []
74+
input_list.each do |e|
75+
result.push yield e
76+
end
77+
result
78+
end
7079
end

spec/framework/serialize_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,25 @@
5656
expect(actual).to eq(123)
5757
end
5858
end
59+
60+
describe 'serialize_list' do
61+
it 'should only operate on things with .each method' do
62+
actual = Twilio.serialize_list(nil)
63+
expect(actual).to eq(nil)
64+
65+
actual = Twilio.serialize_list("something")
66+
expect(actual).to eq("something")
67+
68+
actual = Twilio.serialize_list(1)
69+
expect(actual).to eq(1)
70+
71+
actual = Twilio.serialize_list({ some_obj: 'some_val' })
72+
expect(actual).to eq({ some_obj: 'some_val' })
73+
end
74+
75+
it 'should apply block to every element in list' do
76+
actual = Twilio.serialize_list([1, 2, 3, 4]) { |e| e * 2 }
77+
expect(actual).to eq([2, 4, 6, 8])
78+
end
79+
end
5980
end

0 commit comments

Comments
 (0)