purple-client 0.1.7.3 → 0.1.7.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08cd363a5b6e96500dde9beaa81ac46fdb282d6e6225d8ca19d4ba917c0b6f4f'
4
- data.tar.gz: 3eb93db2b0b359bc7b455ce6282bcc02d1d50991fbd8578a0527388b785fd9e8
3
+ metadata.gz: 81f0cb64364ec0312d07b900ae67a2e65137d03818c6b3e2c1b8631221ea9a65
4
+ data.tar.gz: 7404f01d3ba5714093d62fbac16218b263f20c43d0b29391add9fd9833ccdb29
5
5
  SHA512:
6
- metadata.gz: 2653930ab7bcb2855c8ff142d401ffb8b1c676f0ca579f66fb7a780f8cb45760afff1f7f60fbabe7573091c4a877e6e5ea4d8d31d26c9dafd41a8c4d2b953095
7
- data.tar.gz: f2aacc6f3e1d6ca9b6661c5ad08be55f29b12061e1b037d30aca40ef7bb397ebb2b6ef88e6df772e66abf8bb7b61d6247d7d1980b35009d9f759d3bbd48504b5
6
+ metadata.gz: ab18072bf25f80e0ded0dc7d3f317230fb93e400cbc9a563bf0d6be68b3c166458bf94f39877eb6dc535bce4c1e8c962b002699e3f4e08702effee65ba86e397
7
+ data.tar.gz: ef5033eb7da7a10bf033583603906b31176a63222e95e5f18bbdcab613b4ac775d4cb76594419c796206d65680dfc836d88ffe8f62973b38ad867a81928a1c8d
data/README.md CHANGED
@@ -121,6 +121,36 @@ end
121
121
  PostsClient.user_posts(user_id: 7)
122
122
  ```
123
123
 
124
+ ### Paths nested under parameters
125
+
126
+ When a path segment is marked with `is_param: true`, any paths nested
127
+ inside it will not have a `root_method` generated. Instead of calling a
128
+ root method, you need to chain the segment methods manually.
129
+
130
+ ```ruby
131
+ class BrowserClient < Purple::Client
132
+ domain 'https://api.example.com'
133
+
134
+ path :browser do
135
+ path :id, is_param: true do
136
+ path :web, method: :post do
137
+ response :ok do
138
+ end
139
+
140
+ response :bad_request do
141
+ body do |res|
142
+ puts res
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ # root_method :web will not work here
151
+ BrowserClient.browser.id('123').web
152
+ ```
153
+
124
154
  ### Callbacks with additional arguments
125
155
 
126
156
  ```ruby
@@ -197,6 +227,30 @@ end
197
227
  CalendarClient.schedule
198
228
  ```
199
229
 
230
+ ### Allow blank fields
231
+
232
+ Some APIs return keys that are present but contain `null` or empty string values.
233
+ You can mark those fields with `allow_blank` so blank values do not raise
234
+ validation errors.
235
+
236
+ ```ruby
237
+ class ProfilesClient < Purple::Client
238
+ domain 'https://api.example.com'
239
+
240
+ path :profile do
241
+ response :ok do
242
+ body(
243
+ middle_name: { type: String, allow_blank: true },
244
+ )
245
+ end
246
+ root_method :profile
247
+ end
248
+ end
249
+
250
+ # The `middle_name` attribute may be blank or omitted in the response
251
+ ProfilesClient.profile
252
+ ```
253
+
200
254
  ### Array responses
201
255
 
202
256
  When an endpoint returns an array of objects, you can use `:array_of` to
@@ -226,6 +280,43 @@ end
226
280
  MerchantsClient.merchants
227
281
  ```
228
282
 
283
+ ### Response body processing
284
+
285
+ After the body structure is validated, you can supply a block to `body`
286
+ to transform or handle the parsed response. This is useful for mapping
287
+ error payloads to simpler return values or for normalizing data.
288
+
289
+ ```ruby
290
+ class MessagesClient < Purple::Client
291
+ domain 'https://api.example.com'
292
+
293
+ path :messages do
294
+ response :unprocessable_entity do
295
+ structure = {
296
+ status: Integer,
297
+ type: String,
298
+ title: String,
299
+ detail: String
300
+ }
301
+
302
+ body(**structure) do |res|
303
+ case res.type
304
+ when 'errors/invalid_recipient'
305
+ :not_found
306
+ else
307
+ res
308
+ end
309
+ end
310
+ end
311
+ root_method :send_message
312
+ end
313
+ end
314
+
315
+ # Returns :not_found when the recipient is invalid, otherwise returns the
316
+ # parsed response body.
317
+ MessagesClient.send_message
318
+ ```
319
+
229
320
  ## Development
230
321
 
231
322
  After checking out the repo, run `bin/setup` to install dependencies. Then run
data/lib/purple/path.rb CHANGED
@@ -27,7 +27,7 @@ module Purple
27
27
  @param_value = args.first
28
28
  end
29
29
 
30
- def method_missing(method_name, *args, &)
30
+ def method_missing(method_name, *args, **kw_args, &)
31
31
  if children.any? { |child| child.name == method_name }
32
32
  child = children.find { |child| child.name == method_name }
33
33
 
@@ -38,7 +38,7 @@ module Purple
38
38
  if child.children.any?
39
39
  child
40
40
  else
41
- callback_arguments = additional_callback_arguments.map do |arg|
41
+ callback_arguments = client.additional_callback_arguments.map do |arg|
42
42
  kw_args.delete(arg)
43
43
  end
44
44
 
@@ -12,6 +12,7 @@ class Purple::Response
12
12
  CODES = {
13
13
  ok: 200,
14
14
  created: 201,
15
+ accepted: 202,
15
16
  bad_request: 400,
16
17
  unauthorized: 401,
17
18
  unprocessable_entity: 422,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Purple
4
- VERSION = "0.1.7.3"
4
+ VERSION = "0.1.7.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: purple-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7.3
4
+ version: 0.1.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov