apidiesel 0.1.3 → 0.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: 798839eabb874dd1436557c30354b5db5c34cbeb
4
- data.tar.gz: a32a71d7c332df16d34adefdbcf210de2ae7797f
3
+ metadata.gz: d15d627f5ccaedcfd3a26398782dee3ddd2a1884
4
+ data.tar.gz: bccf9fdf4ec8300f6f115d71da7deb77790e62e0
5
5
  SHA512:
6
- metadata.gz: 8a49d3ba3ca856b677049a6f7f42901a04962624ab7fd04a8d2054283c6db0d571076546120a549976b4a1ab702c036423877842ed7668fc9c7014a188724ace
7
- data.tar.gz: 327efab8c20eb431b8f410d85613a64ceeb8e2493312b36882f0d5c9ef992efb57c6d05dcc1865d1e35b396b3881226631d0c7a6cad44fb69486cc722369859f
6
+ metadata.gz: 31aacd8dd6e107102d96a6523acea5e24d463f1b54c1578155046afce20d1f61f646513299ef1b63cdef0a6407ad1062cc4cf55c4e62fac933a85b78d8822227
7
+ data.tar.gz: ecc0307c212442ea40418f251df6471483c06520777f50ef063107941e1918a4ae5cfbaf435bb67e9707bf93e846997b75f184e53694875043e187f0ad76b7a0
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Apidiesel
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/apidiesel`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Apidiesel is a DSL for building API clients. It is made to be highly readable,
4
+ easily extensible and to assume as little as possible about your API.
6
5
 
7
6
  ## Installation
8
7
 
@@ -22,7 +21,34 @@ Or install it yourself as:
22
21
 
23
22
  ## Usage
24
23
 
25
- TODO: Write usage instructions here
24
+ Apidiesel consists of three main parts: the base `Api`, one `Action` for each API
25
+ endpoint and `Handler` plugins for processing incoming and outgoing data.
26
+
27
+ module Actions
28
+ class GetUsers < Apidiesel::Action
29
+ url path: '/users'
30
+
31
+ expects do
32
+ string :firstname, optional: true
33
+ string :lastname, optional: true
34
+ boolean :active, default: true
35
+ end
36
+
37
+ responds_with do
38
+ objects :users, wrapped_in: MyUserModel
39
+ end
40
+ end
41
+ end
42
+
43
+ class Api < Apidiesel::Api
44
+ url 'https://foo.example'
45
+ http_method :post
46
+
47
+ register_actions
48
+ end
49
+
50
+ api = Api.new
51
+ api.get_users(firstname: 'Jane', lastname: 'Doe')
26
52
 
27
53
  ## Development
28
54
 
data/lib/apidiesel/dsl.rb CHANGED
@@ -210,6 +210,95 @@ module Apidiesel
210
210
  copy_value_directly(key, *args)
211
211
  end
212
212
 
213
+ # Returns an array of subhashes
214
+ #
215
+ # @example
216
+ #
217
+ # # Given an API response:
218
+ # #
219
+ # # {
220
+ # # order_id: 5,
221
+ # # ordered_at :"2020-01-01",
222
+ # # products: [{
223
+ # # name: 'Catnip 2lbs',
224
+ # # product_id: 2004921
225
+ # # }]
226
+ # # }
227
+ #
228
+ # expects do
229
+ # integer :order_id
230
+ # datetime :ordered_at
231
+ #
232
+ # an_array_of :products do
233
+ # string :name
234
+ # integer :product_id
235
+ # end
236
+ # end
237
+ #
238
+ # # Given an API response:
239
+ # #
240
+ # # [
241
+ # # {
242
+ # # name: 'Catnip 2lbs',
243
+ # # order_id: 2004921
244
+ # # },
245
+ # # {
246
+ # # name: 'Catnip 5lbs',
247
+ # # order_id: 2004922
248
+ # # },
249
+ # # ]
250
+ #
251
+ # @example
252
+ # expects do
253
+ # an_array_of do
254
+ # string :name
255
+ # integer :order_id
256
+ # end
257
+ # end
258
+ #
259
+ # @option *args [Symbol] the key for finding and returning the array
260
+ # (sets both :as and :at)
261
+ # @option **kargs [Symbol] :at which key to find the hash at in the
262
+ # response
263
+ # @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
268
+ end
269
+
270
+ response_formatters << lambda do |data, processed_data|
271
+ if kargs[:at]
272
+ data = data[ kargs[:at] ]
273
+ end
274
+
275
+ return processed_data unless data.present?
276
+
277
+ data = [data] if data.is_a?(Hash)
278
+
279
+ array_of_hashes = data.map do |hash|
280
+ builder = FilterBuilder.new
281
+ builder.instance_eval(&block)
282
+
283
+ result = {}
284
+
285
+ builder.response_formatters.each do |filter|
286
+ result = filter.call(hash, result)
287
+ end
288
+
289
+ result
290
+ end
291
+
292
+ if kargs[:as]
293
+ processed_data[ kargs[:as] ] = array_of_hashes
294
+ else
295
+ processed_data = array_of_hashes
296
+ end
297
+
298
+ processed_data
299
+ end
300
+ end
301
+
213
302
  # Returns the API response processed or wrapped in wrapper objects.
214
303
  #
215
304
  # @example
@@ -1,3 +1,3 @@
1
1
  module Apidiesel
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2"
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.1.3
4
+ version: '0.2'
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-12 00:00:00.000000000 Z
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport