apidiesel 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/apidiesel/dsl.rb +169 -51
- data/lib/apidiesel/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfb34fa51d6af7ae15a45c3886e85f56bfe12faa
|
4
|
+
data.tar.gz: 509b195c1cc17324c7f8b5c84526e1e759001ec5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cc7ba3308ef13073c3c8b2d10dcf4c945467bbd6830fb7b8735618639a05a417e7a689e9e5591a3cd3c9f557742d0f9033dcaa3fd384283f598878de7e9f932
|
7
|
+
data.tar.gz: ca568384a06cf4f011414d57aa54ac06e137a8999fde0b8bd27bccc180da99adebbae3120837a95a5622eae1727d8805a1bff0ee92f43cfe81e76aacff704244
|
data/lib/apidiesel/dsl.rb
CHANGED
@@ -31,11 +31,23 @@ module Apidiesel
|
|
31
31
|
#
|
32
32
|
# @macro [attach] responds_with
|
33
33
|
# @yield [Apidiesel::Dsl::FilterBuilder]
|
34
|
-
def responds_with(&block)
|
34
|
+
def responds_with(**args, &block)
|
35
35
|
builder = FilterBuilder.new
|
36
|
+
|
36
37
|
builder.instance_eval(&block)
|
37
|
-
|
38
|
-
|
38
|
+
|
39
|
+
response_filters.concat(builder.response_filters)
|
40
|
+
response_formatters.concat(builder.response_formatters)
|
41
|
+
|
42
|
+
if args[:unnested_hash]
|
43
|
+
response_formatters << lambda do |_, response|
|
44
|
+
if response.is_a?(Hash) && response.keys.length == 1
|
45
|
+
response.values.first
|
46
|
+
else
|
47
|
+
response
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
39
51
|
end
|
40
52
|
|
41
53
|
# ExpectationBuilder defines the methods available within an `expects` block
|
@@ -121,6 +133,9 @@ module Apidiesel
|
|
121
133
|
validation_builder(:strftime, param_name, **args)
|
122
134
|
end
|
123
135
|
|
136
|
+
alias_method :time, :datetime
|
137
|
+
alias_method :date, :datetime
|
138
|
+
|
124
139
|
protected
|
125
140
|
|
126
141
|
def validation_builder(duck_typing_check, param_name, *args)
|
@@ -177,37 +192,82 @@ module Apidiesel
|
|
177
192
|
@response_formatters = []
|
178
193
|
end
|
179
194
|
|
195
|
+
def value(*args, **kargs)
|
196
|
+
args = normalize_arguments(args, kargs)
|
197
|
+
|
198
|
+
response_formatters << lambda do |data, processed_data|
|
199
|
+
value = get_value(data, args[:at])
|
200
|
+
|
201
|
+
value = apply_filter(args[:prefilter], value)
|
202
|
+
|
203
|
+
value = apply_filter(args[:postfilter] || args[:filter], value)
|
204
|
+
|
205
|
+
value = args[:map][value] if args[:map]
|
206
|
+
|
207
|
+
processed_data[ args[:as] ] = value
|
208
|
+
|
209
|
+
processed_data
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
180
213
|
# Returns `key` from the API response as a string.
|
181
214
|
#
|
182
215
|
# @param [Symbol] key the key name to be returned as a string
|
183
216
|
# @param [Hash] *args
|
184
217
|
# @option *args [Symbol] :within look up the key in a namespace (nested hash)
|
185
|
-
def string(
|
186
|
-
|
218
|
+
def string(*args, **kargs)
|
219
|
+
create_primitive_formatter(:to_s, *args, **kargs)
|
187
220
|
end
|
188
221
|
|
189
222
|
# Returns `key` from the API response as an integer.
|
190
223
|
#
|
191
224
|
# @param (see #string)
|
192
225
|
# @option (see #string)
|
193
|
-
def integer(
|
194
|
-
|
226
|
+
def integer(*args, **kargs)
|
227
|
+
create_primitive_formatter(:to_i, *args, **kargs)
|
195
228
|
end
|
196
229
|
|
197
|
-
# Returns `key` from the API response as a
|
230
|
+
# Returns `key` from the API response as a float.
|
198
231
|
#
|
199
232
|
# @param (see #string)
|
200
233
|
# @option (see #string)
|
201
|
-
def
|
202
|
-
|
234
|
+
def float(*args, **kargs)
|
235
|
+
create_primitive_formatter(:to_f, *args, **kargs)
|
203
236
|
end
|
204
237
|
|
205
|
-
# Returns `key` from the API response as
|
238
|
+
# Returns `key` from the API response as a symbol.
|
206
239
|
#
|
207
240
|
# @param (see #string)
|
208
241
|
# @option (see #string)
|
209
|
-
def
|
210
|
-
|
242
|
+
def symbol(*args, **kargs)
|
243
|
+
create_primitive_formatter(:to_sym, *args, **kargs)
|
244
|
+
end
|
245
|
+
|
246
|
+
# Returns `key` from the API response as DateTime.
|
247
|
+
#
|
248
|
+
# @param (see #string)
|
249
|
+
# @option (see #string)
|
250
|
+
def datetime(*args, **kargs)
|
251
|
+
args = normalize_arguments(args, kargs)
|
252
|
+
args.reverse_merge!(format: '%Y-%m-%d')
|
253
|
+
|
254
|
+
response_formatters << lambda do |data, processed_data|
|
255
|
+
value = get_value(data, args[:at])
|
256
|
+
|
257
|
+
value = apply_filter(args[:prefilter], value)
|
258
|
+
|
259
|
+
if args.has_key?(:on_error)
|
260
|
+
value = DateTime.strptime(value, args[:format]) rescue args[:on_error]
|
261
|
+
else
|
262
|
+
value = DateTime.strptime(value, args[:format])
|
263
|
+
end
|
264
|
+
|
265
|
+
value = apply_filter(args[:postfilter] || args[:filter], value)
|
266
|
+
|
267
|
+
processed_data[ args[:as] ] = value
|
268
|
+
|
269
|
+
processed_data
|
270
|
+
end
|
211
271
|
end
|
212
272
|
|
213
273
|
# Returns an array of subhashes
|
@@ -261,16 +321,16 @@ module Apidiesel
|
|
261
321
|
# @option **kargs [Symbol] :at which key to find the hash at in the
|
262
322
|
# response
|
263
323
|
# @option **kargs [Symbol] :as which key to return the result under
|
264
|
-
def
|
265
|
-
|
266
|
-
|
267
|
-
|
324
|
+
def array(*args, **kargs, &block)
|
325
|
+
unless block.present?
|
326
|
+
create_primitive_formatter(:to_a, *args, **kargs)
|
327
|
+
return
|
268
328
|
end
|
269
329
|
|
330
|
+
args = normalize_arguments(args, kargs)
|
331
|
+
|
270
332
|
response_formatters << lambda do |data, processed_data|
|
271
|
-
|
272
|
-
data = data[ kargs[:at] ]
|
273
|
-
end
|
333
|
+
data = get_value(data, args[:at])
|
274
334
|
|
275
335
|
return processed_data unless data.present?
|
276
336
|
|
@@ -282,19 +342,55 @@ module Apidiesel
|
|
282
342
|
|
283
343
|
result = {}
|
284
344
|
|
345
|
+
hash = apply_filter(args[:prefilter_each], hash)
|
346
|
+
|
285
347
|
builder.response_formatters.each do |filter|
|
286
348
|
result = filter.call(hash, result)
|
287
349
|
end
|
288
350
|
|
351
|
+
result = apply_filter(args[:postfilter_each] || args[:filter_each], result)
|
352
|
+
|
289
353
|
result
|
290
354
|
end
|
291
355
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
356
|
+
processed_data[ args[:as] ] = array_of_hashes
|
357
|
+
|
358
|
+
processed_data
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
# Returns `key` from the API response as a hash.
|
363
|
+
#
|
364
|
+
# @param (see #string)
|
365
|
+
# @option (see #string)
|
366
|
+
def hash(*args, **kargs, &block)
|
367
|
+
unless block.present?
|
368
|
+
create_primitive_formatter(:to_hash, *args, **kargs)
|
369
|
+
return
|
370
|
+
end
|
371
|
+
|
372
|
+
args = normalize_arguments(args, kargs)
|
373
|
+
|
374
|
+
response_formatters << lambda do |data, processed_data|
|
375
|
+
data = get_value(data, args[:at])
|
376
|
+
|
377
|
+
return processed_data unless data.is_a?(Hash)
|
378
|
+
|
379
|
+
hash = apply_filter(args[:prefilter], data)
|
380
|
+
|
381
|
+
result = {}
|
382
|
+
|
383
|
+
builder = FilterBuilder.new
|
384
|
+
builder.instance_eval(&block)
|
385
|
+
|
386
|
+
builder.response_formatters.each do |filter|
|
387
|
+
result = filter.call(hash, result)
|
296
388
|
end
|
297
389
|
|
390
|
+
result = apply_filter(args[:postfilter_each] || args[:filter_each], result)
|
391
|
+
|
392
|
+
processed_data[ args[:as] ] = result
|
393
|
+
|
298
394
|
processed_data
|
299
395
|
end
|
300
396
|
end
|
@@ -317,22 +413,19 @@ module Apidiesel
|
|
317
413
|
# @option *args [Proc] :processed_with yield the data to this Proc for processing
|
318
414
|
# @option *args [Class] :wrapped_in wrapper object, will be called as `Object.create(data)`
|
319
415
|
# @option *args [Symbol] :as key name to save the result as
|
320
|
-
def objects(
|
321
|
-
|
416
|
+
def objects(*args, **kargs)
|
417
|
+
args = normalize_arguments(args, kargs)
|
322
418
|
|
323
419
|
response_formatters << lambda do |data, processed_data|
|
324
|
-
|
420
|
+
value = get_value(data, args[:at])
|
325
421
|
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
if options[:wrapped_in]
|
330
|
-
d = options[:wrapped_in].send(:create, d)
|
331
|
-
end
|
422
|
+
value = apply_filter(args[:prefilter], value)
|
423
|
+
|
424
|
+
value = args[:type].send(:create, value)
|
332
425
|
|
333
|
-
|
426
|
+
value = apply_filter(args[:postfilter] || args[:filter], value)
|
334
427
|
|
335
|
-
processed_data[
|
428
|
+
processed_data[ args[:as] ] = value
|
336
429
|
|
337
430
|
processed_data
|
338
431
|
end
|
@@ -345,7 +438,7 @@ module Apidiesel
|
|
345
438
|
# @param [Symbol, Array] key
|
346
439
|
def set_scope(key)
|
347
440
|
response_filters << lambda do |data|
|
348
|
-
fetch_path(data, *key)
|
441
|
+
begin; fetch_path(data, *key); rescue => e; binding.pry; end
|
349
442
|
end
|
350
443
|
end
|
351
444
|
|
@@ -371,29 +464,54 @@ module Apidiesel
|
|
371
464
|
|
372
465
|
protected
|
373
466
|
|
374
|
-
def
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
467
|
+
def create_primitive_formatter(cast_method_symbol, *args, **kargs)
|
468
|
+
args = normalize_arguments(args, kargs)
|
469
|
+
|
470
|
+
response_formatters << lambda do |data, processed_data|
|
471
|
+
value = get_value(data, args[:at])
|
472
|
+
|
473
|
+
value = apply_filter(args[:prefilter], value)
|
474
|
+
|
475
|
+
value = value.try(cast_method_symbol)
|
476
|
+
|
477
|
+
value = apply_filter(args[:postfilter] || args[:filter], value)
|
478
|
+
|
479
|
+
value = args[:map][value] if args[:map]
|
480
|
+
|
481
|
+
processed_data[ args[:as] ] = value
|
482
|
+
|
483
|
+
processed_data
|
381
484
|
end
|
382
485
|
end
|
383
486
|
|
384
|
-
def
|
385
|
-
|
386
|
-
|
487
|
+
def normalize_arguments(args, kargs)
|
488
|
+
if args.length == 1
|
489
|
+
kargs[:as] ||= args.first
|
490
|
+
kargs[:at] ||= args.first
|
387
491
|
end
|
492
|
+
|
493
|
+
kargs
|
388
494
|
end
|
389
495
|
|
390
|
-
def
|
391
|
-
|
496
|
+
def apply_filter(filter, value)
|
497
|
+
return value unless filter
|
392
498
|
|
393
|
-
|
394
|
-
|
499
|
+
filter.call(value)
|
500
|
+
end
|
395
501
|
|
396
|
-
|
502
|
+
def get_value(hash, *keys)
|
503
|
+
keys = keys.first if keys.first.is_a?(Array)
|
504
|
+
|
505
|
+
if keys.length > 1
|
506
|
+
fetch_path(hash, keys)
|
507
|
+
else
|
508
|
+
hash[keys.first]
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
def fetch_path(hash, key_or_keys)
|
513
|
+
Array(key_or_keys).reduce(hash) do |memo, key|
|
514
|
+
memo[key] if memo
|
397
515
|
end
|
398
516
|
end
|
399
517
|
|
data/lib/apidiesel/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apidiesel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan-Christian Foeh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|