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 +4 -4
- data/README.md +91 -0
- data/lib/purple/path.rb +2 -2
- data/lib/purple/response.rb +1 -0
- data/lib/purple/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81f0cb64364ec0312d07b900ae67a2e65137d03818c6b3e2c1b8631221ea9a65
|
4
|
+
data.tar.gz: 7404f01d3ba5714093d62fbac16218b263f20c43d0b29391add9fd9833ccdb29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
data/lib/purple/response.rb
CHANGED
data/lib/purple/version.rb
CHANGED