protip 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25ad2e3a190f034f359fc8ca65ade0e535cb9ae6
4
- data.tar.gz: 1e5a004ee50b758b10af25a4013dbb28f8aca8aa
3
+ metadata.gz: eacbefd07c48cf491c512855a68a820c48d44be6
4
+ data.tar.gz: f79accd3d4f140b5f7e6696d6c27c19e76726bce
5
5
  SHA512:
6
- metadata.gz: 6cfef2853b9da32d395e76fdffaf9ffa71f527cc31da27264b54a0f338aa8a9b78b4011ea307c93e85a6285325ab3b13b371752fa048cf51df0940d1538c77b9
7
- data.tar.gz: cc4d369252fc7b80a414a039d59942643e8a70a2ecdb1544ca256a3a8577f6ed0dff15a08f62a07268ff61601d37cb2d7b5a4c4a15f86af896666a7ba20fddd7
6
+ metadata.gz: 3359ad844b26d2792f63e09254a6266810eb09e21915be9c5fe4fdfcc0752bb70bff905d107be4edba1acfad6911040ef13885b076a54767842d8b9f2e9fefe0
7
+ data.tar.gz: 574b9fab112f596088f28f66119a246efd64133a1732a3193b5556457857d8edee814eb5749f957ef1b5f6ff986351486770acacbe2b2405978f0fdb79272c0a
@@ -5,7 +5,7 @@ require 'google/protobuf'
5
5
 
6
6
  Google::Protobuf::DescriptorPool.generated_pool.build do
7
7
  add_message "protip.messages.Array" do
8
- repeated :messages, :string, 1
8
+ repeated :messages, :bytes, 1
9
9
  end
10
10
  end
11
11
 
@@ -109,12 +109,23 @@ module Protip
109
109
  end
110
110
 
111
111
  def as_json
112
- json = {}
113
- message.class.descriptor.map(&:name).each do |name|
114
- value = public_send(name)
115
- json[name.to_s] = value.respond_to?(:as_json) ? value.as_json : value
112
+ to_h.as_json
113
+ end
114
+
115
+ # @return [Hash] A hash whose keys are the fields of our message, and whose values are the Ruby representations
116
+ # (either nested hashes or converted messages) of the field values.
117
+ def to_h
118
+ hash = {}
119
+ message.class.descriptor.each do |field|
120
+ value = public_send(field.name)
121
+ if field.label == :repeated
122
+ value.map!{|v| v.is_a?(self.class) ? v.to_h : v}
123
+ else
124
+ value = (value.is_a?(self.class) ? value.to_h : value)
125
+ end
126
+ hash[field.name.to_sym] = value
116
127
  end
117
- json
128
+ hash
118
129
  end
119
130
 
120
131
  def ==(wrapper)
@@ -127,18 +138,25 @@ module Protip
127
138
  private
128
139
 
129
140
  def get(field)
141
+ if field.label == :repeated
142
+ message[field.name].map{|value| to_ruby_value field, value}
143
+ else
144
+ to_ruby_value field, message[field.name]
145
+ end
146
+ end
147
+
148
+ # Helper for getting values - converts the value for the given field to one that we can return to the user
149
+ def to_ruby_value(field, value)
130
150
  if field.type == :message
131
- if nil == message[field.name]
151
+ if nil == value
132
152
  nil
153
+ elsif converter.convertible?(field.subtype.msgclass)
154
+ converter.to_object value
133
155
  else
134
- if converter.convertible?(field.subtype.msgclass)
135
- converter.to_object message[field.name]
136
- else
137
- self.class.new message[field.name], converter
138
- end
156
+ self.class.new value, converter
139
157
  end
140
158
  else
141
- message[field.name]
159
+ value
142
160
  end
143
161
  end
144
162
 
@@ -92,9 +92,11 @@ describe 'Protip::Resource (functional)' do
92
92
  describe '.all' do
93
93
  describe 'with a successful server response' do
94
94
  before do
95
+ # If this is erroring out, change :string to :bytes in array.rb - the Ruby compiler currently compiles
96
+ # incorrectly.
95
97
  response = Protip::Messages::Array.new(messages: ['bilbo', 'baggins'].each_with_index.map do |name, index|
96
98
  message = resource_message_class.new(id: int_message_class.new(value: index), ordered_tests: name, nested_int: int_message_class.new(value: index + 42))
97
- message.class.encode(message).encode('UTF-8')
99
+ message.class.encode(message)
98
100
  end)
99
101
  stub_request(:get, 'https://external.service/resources')
100
102
  .to_return body: response.class.encode(response)
@@ -130,7 +130,7 @@ module Protip::ResourceTest # Namespace for internal constants
130
130
  messages: [
131
131
  resource_message_class.new(string: 'banjo', id: 1),
132
132
  resource_message_class.new(string: 'kazooie', id: 2),
133
- ].map{|m| resource_message_class.encode(m).encode('UTF-8')}
133
+ ].map{|m| resource_message_class.encode(m)}
134
134
  })
135
135
  end
136
136
 
@@ -215,6 +215,69 @@ module Protip::WrapperTest # namespace for internal constants
215
215
  end
216
216
  end
217
217
 
218
+ describe '#convert' do
219
+ let :wrapped_message do
220
+ m = message_class.new({
221
+ string: 'test',
222
+ inner: inner_message_class.new(value: 1),
223
+ })
224
+ m.strings += %w(test1 test2)
225
+ [2, 3].each do |i|
226
+ m.inners.push inner_message_class.new(value: i)
227
+ end
228
+ m
229
+ end
230
+ before do
231
+ converter.stubs(:convertible?).with(message_class).returns false
232
+ end
233
+
234
+ it 'never checks the convertibility of the top-level message' do
235
+ converter.expects(:convertible?).with(message_class).never
236
+ converter.stubs(:convertible?).with(inner_message_class).returns false
237
+ assert_instance_of Hash, wrapper.to_h
238
+ end
239
+
240
+ describe 'with a nested convertible message' do
241
+ before do
242
+ converter.stubs(:convertible?).with(inner_message_class).returns true
243
+ [1, 2, 3].each{|i| converter.stubs(:to_object).with(inner_message_class.new(value: i)).returns(i)}
244
+ end
245
+ it 'returns a hash with the nested message converted' do
246
+ assert_equal 1, wrapper.to_h[:inner]
247
+ end
248
+ it 'converts a repeated instance of the nested message to an array' do
249
+ assert_equal [2, 3], wrapper.to_h[:inners]
250
+ end
251
+ end
252
+
253
+ describe 'with a nested inconvertible message' do
254
+ before do
255
+ converter.stubs(:convertible?).with(inner_message_class).returns false
256
+ end
257
+
258
+ it 'contains keys for all fields of the parent message' do
259
+ assert_equal %i(string strings inner inners inner_blank).sort, wrapper.to_h.keys.sort
260
+ end
261
+ it 'assigns nil for missing nested messages' do
262
+ hash = wrapper.to_h
263
+ assert hash.has_key?(:inner_blank)
264
+ assert_nil hash[:inner_blank]
265
+ end
266
+ it 'assigns a hash for a scalar instance of the inconvertible message' do
267
+ assert_equal({value: 1, note: ''}, wrapper.to_h[:inner])
268
+ end
269
+ it 'assigns an array of hashes for a repeated instance of the inconvertible message' do
270
+ assert_equal([{value: 2, note: ''}, {value: 3, note: ''}], wrapper.to_h[:inners])
271
+ end
272
+ it 'assigns primitive fields directly' do
273
+ assert_equal 'test', wrapper.to_h[:string]
274
+ end
275
+ it 'assigns an array for repeated primitive fields' do
276
+ assert_equal %w(test1 test2), wrapper.to_h[:strings]
277
+ end
278
+ end
279
+ end
280
+
218
281
  describe '#get' do
219
282
  it 'does not convert simple fields' do
220
283
  converter.expects(:convertible?).never
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - AngelList
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-10 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel