apidiesel 0.2 → 0.3

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: d15d627f5ccaedcfd3a26398782dee3ddd2a1884
4
- data.tar.gz: bccf9fdf4ec8300f6f115d71da7deb77790e62e0
3
+ metadata.gz: bfb34fa51d6af7ae15a45c3886e85f56bfe12faa
4
+ data.tar.gz: 509b195c1cc17324c7f8b5c84526e1e759001ec5
5
5
  SHA512:
6
- metadata.gz: 31aacd8dd6e107102d96a6523acea5e24d463f1b54c1578155046afce20d1f61f646513299ef1b63cdef0a6407ad1062cc4cf55c4e62fac933a85b78d8822227
7
- data.tar.gz: ecc0307c212442ea40418f251df6471483c06520777f50ef063107941e1918a4ae5cfbaf435bb67e9707bf93e846997b75f184e53694875043e187f0ad76b7a0
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
- response_filters.concat builder.response_filters
38
- response_formatters.concat builder.response_formatters
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(key, *args)
186
- copy_value_directly(key, *args)
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(key, *args)
194
- copy_value_directly(key, *args)
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 hash.
230
+ # Returns `key` from the API response as a float.
198
231
  #
199
232
  # @param (see #string)
200
233
  # @option (see #string)
201
- def hash(key, *args)
202
- copy_value_directly(key, *args)
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 an array.
238
+ # Returns `key` from the API response as a symbol.
206
239
  #
207
240
  # @param (see #string)
208
241
  # @option (see #string)
209
- def array(key, *args)
210
- copy_value_directly(key, *args)
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 an_array_of(*args, **kargs, &block)
265
- if args.length == 1
266
- kargs[:as] ||= args.first
267
- kargs[:at] ||= args.first
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
- if kargs[:at]
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
- if kargs[:as]
293
- processed_data[ kargs[:as] ] = array_of_hashes
294
- else
295
- processed_data = array_of_hashes
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(key, *args)
321
- options = args.extract_options!
416
+ def objects(*args, **kargs)
417
+ args = normalize_arguments(args, kargs)
322
418
 
323
419
  response_formatters << lambda do |data, processed_data|
324
- d = get_value(key, data, options[:within])
420
+ value = get_value(data, args[:at])
325
421
 
326
- if options[:processed_with]
327
- d = options[:processed_with].call(d)
328
- end
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
- result_key = options[:as] || key
426
+ value = apply_filter(args[:postfilter] || args[:filter], value)
334
427
 
335
- processed_data[result_key] = d
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 get_value(key, hash, namespace = nil)
375
- if namespace.is_a?(Array)
376
- fetch_path(hash, *namespace)
377
- elsif namespace.nil?
378
- hash[key]
379
- else
380
- hash[namespace][key]
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 fetch_path(hash, *parts)
385
- parts.reduce(hash) do |memo, key|
386
- memo[key] if memo
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 copy_value_directly(key, *args)
391
- options = args.extract_options!
496
+ def apply_filter(filter, value)
497
+ return value unless filter
392
498
 
393
- response_formatters << lambda do |data, processed_data|
394
- processed_data[key] = get_value(key, data, options[:within])
499
+ filter.call(value)
500
+ end
395
501
 
396
- processed_data
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
 
@@ -1,3 +1,3 @@
1
1
  module Apidiesel
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
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.2'
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-16 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport