protip 0.11.1 → 0.11.2

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: eacbefd07c48cf491c512855a68a820c48d44be6
4
- data.tar.gz: f79accd3d4f140b5f7e6696d6c27c19e76726bce
3
+ metadata.gz: 8ab7e84f6fd394d2b5cb52c67d1a5db5f245c36a
4
+ data.tar.gz: 4726d4e87f0453b0c2bd0bc55c2ac52ff23c58e3
5
5
  SHA512:
6
- metadata.gz: 3359ad844b26d2792f63e09254a6266810eb09e21915be9c5fe4fdfcc0752bb70bff905d107be4edba1acfad6911040ef13885b076a54767842d8b9f2e9fefe0
7
- data.tar.gz: 574b9fab112f596088f28f66119a246efd64133a1732a3193b5556457857d8edee814eb5749f957ef1b5f6ff986351486770acacbe2b2405978f0fdb79272c0a
6
+ metadata.gz: 4721c4d9f10135735502dccfb105532d4b6b31a74dcda08b1298bb0cc9bec791897c90002997c826cf399fb3953018bfdab5f5e62710834ccf99a21d5a5c458f
7
+ data.tar.gz: cde835698f8a9feb4eb3743b7c3bfdd5ab2bb0e42d0963cb0ebd1849aa96640d6f2169db6302901ca1a4e4e33c5385ce16ae2e86efb674672dbbb5ff1114aa28
@@ -178,13 +178,17 @@ module Protip
178
178
  if query
179
179
  if actions.include?(:show)
180
180
  define_singleton_method :find do |id, query_params = {}|
181
- SearchMethods.show(self, id, query.new(query_params))
181
+ wrapper = ::Protip::Wrapper.new(query.new, converter)
182
+ wrapper.assign_attributes query_params
183
+ SearchMethods.show(self, id, wrapper.message)
182
184
  end
183
185
  end
184
186
 
185
187
  if actions.include?(:index)
186
188
  define_singleton_method :all do |query_params = {}|
187
- SearchMethods.index(self, query.new(query_params))
189
+ wrapper = ::Protip::Wrapper.new(query.new, converter)
190
+ wrapper.assign_attributes query_params
191
+ SearchMethods.index(self, wrapper.message)
188
192
  end
189
193
  end
190
194
  else
@@ -209,7 +213,9 @@ module Protip
209
213
  def member(action:, method:, request: nil, response: nil)
210
214
  if request
211
215
  define_method action do |request_params = {}|
212
- ExtraMethods.member self, action, method, request.new(request_params), response
216
+ wrapper = ::Protip::Wrapper.new(request.new, self.class.converter)
217
+ wrapper.assign_attributes request_params
218
+ ExtraMethods.member self, action, method, wrapper.message, response
213
219
  end
214
220
  else
215
221
  define_method action do
@@ -221,7 +227,9 @@ module Protip
221
227
  def collection(action:, method:, request: nil, response: nil)
222
228
  if request
223
229
  define_singleton_method action do |request_params = {}|
224
- ExtraMethods.collection self, action, method, request.new(request_params), response
230
+ wrapper = ::Protip::Wrapper.new(request.new, converter)
231
+ wrapper.assign_attributes request_params
232
+ ExtraMethods.collection self, action, method, wrapper.message, response
225
233
  end
226
234
  else
227
235
  define_singleton_method action do
@@ -21,6 +21,7 @@ module Protip::ResourceTest # Namespace for internal constants
21
21
 
22
22
  add_message 'resource_query' do
23
23
  optional :param, :string, 1
24
+ optional :nested_message, :message, 2, 'nested_message'
24
25
  end
25
26
 
26
27
  # Give these things a different structure than resource_query_class,
@@ -28,6 +29,7 @@ module Protip::ResourceTest # Namespace for internal constants
28
29
  # type but still yielding correct results.
29
30
  add_message 'action_query' do
30
31
  optional :param, :string, 4
32
+ optional :nested_message, :message, 5, 'nested_message'
31
33
  end
32
34
  add_message 'action_response' do
33
35
  optional :response, :string, 3
@@ -124,6 +126,73 @@ module Protip::ResourceTest # Namespace for internal constants
124
126
  end
125
127
  end
126
128
 
129
+ # index/find/member/collection actions should all convert more complex Ruby objects to submessages in their
130
+ # queries
131
+ def self.it_converts_query_parameters
132
+ before do
133
+ # Sanity check - the user should specify all these variables in "let" statements
134
+ # http_method, path, query_class, and response specify the expected call to the client
135
+ # nested_message_field specifies the field on the query class that may or may not be convertible, and should
136
+ # refer to a submessage field of type nested_message_class
137
+ # invoke_method! should call the desired method, assuming that +parameters+ contains the query parameters to
138
+ # pass in (e.g. `resource_class.all(parameters)` or `resource_class.find('id', parameters)`)
139
+ %i(
140
+ http_method
141
+ path
142
+ query_class
143
+ response
144
+ nested_message_field
145
+ invoke_method!
146
+ ).each do |name|
147
+ raise "Must define #{name} before invoking `it_converts_query_parameters`" unless respond_to?(name)
148
+ end
149
+
150
+ # All tests expect the same HTTP call
151
+ client.expects(:request)
152
+ .once
153
+ .with(method: http_method, path: path,
154
+ message: query_class.new(:"#{nested_message_field}" => nested_message_class.new(number: 43)),
155
+ response_type: (nil == response ? nil : response.class),
156
+ ).returns(response)
157
+ end
158
+
159
+
160
+
161
+ describe 'with a convertible message' do
162
+ before do
163
+ resource_class.converter.stubs(:convertible?).with(nested_message_class).returns(true)
164
+ resource_class.converter.stubs(:to_message).with(42, nested_message_class).returns(nested_message_class.new(number: 43))
165
+ end
166
+
167
+ let(:parameters) { {"#{nested_message_field}" => 42} }
168
+ it 'converts query parameters' do
169
+ invoke_method!
170
+ end
171
+ end
172
+
173
+ describe 'with an inconvertible message' do
174
+ before do
175
+ resource_class.converter.stubs(:convertible?).with(nested_message_class).returns(false)
176
+ resource_class.converter.expects(:to_message).never
177
+ end
178
+
179
+ describe 'with a hash' do
180
+ let(:parameters) { {"#{nested_message_field}" => {number: 43}} }
181
+ it 'allows a hash to be provided for the nested message' do
182
+ invoke_method!
183
+ end
184
+ end
185
+
186
+ describe 'with a submessage' do
187
+ let(:parameters) { {"#{nested_message_field}" => nested_message_class.new(number: 43)} }
188
+ it 'allows a submessage to be provided directly' do
189
+ invoke_method!
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+
127
196
  describe '.all' do
128
197
  let :response do
129
198
  Protip::Messages::Array.new({
@@ -213,6 +282,15 @@ module Protip::ResourceTest # Namespace for internal constants
213
282
  .returns(response)
214
283
  resource_class.all
215
284
  end
285
+
286
+ describe '(convertibility)' do
287
+ let(:http_method) { Net::HTTP::Get }
288
+ let(:path) { 'base_path' }
289
+ let(:query_class) { resource_query_class }
290
+ let(:nested_message_field) { :nested_message }
291
+ let(:invoke_method!) { resource_class.all(parameters) }
292
+ it_converts_query_parameters
293
+ end
216
294
  end
217
295
  end
218
296
 
@@ -288,6 +366,15 @@ module Protip::ResourceTest # Namespace for internal constants
288
366
  .returns(response)
289
367
  resource_class.find 6
290
368
  end
369
+
370
+ describe '(convertibility)' do
371
+ let(:http_method) { Net::HTTP::Get }
372
+ let(:path) { 'base_path/5' }
373
+ let(:query_class) { resource_query_class }
374
+ let(:nested_message_field) { :nested_message }
375
+ let(:invoke_method!) { resource_class.find 5, parameters }
376
+ it_converts_query_parameters
377
+ end
291
378
  end
292
379
  end
293
380
 
@@ -537,6 +624,8 @@ module Protip::ResourceTest # Namespace for internal constants
537
624
  end
538
625
  end
539
626
 
627
+ let(:response) { nil }
628
+
540
629
  it 'sends a request with a body to the expected endpoint' do
541
630
  client.expects(:request)
542
631
  .once
@@ -554,6 +643,15 @@ module Protip::ResourceTest # Namespace for internal constants
554
643
  .returns(nil)
555
644
  target.action
556
645
  end
646
+
647
+ describe '(convertibility)' do
648
+ let(:http_method) { Net::HTTP::Post }
649
+ let(:path) { path }
650
+ let(:query_class) { action_query_class }
651
+ let(:nested_message_field) { :nested_message }
652
+ let(:invoke_method!) { target.action(parameters) }
653
+ it_converts_query_parameters
654
+ end
557
655
  end
558
656
 
559
657
  describe 'with a response type' do
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.1
4
+ version: 0.11.2
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-13 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel