flexirest 1.10.2 → 1.10.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/docs/filtering-result-lists.md +1 -1
- data/docs/proxying-apis.md +1 -1
- data/docs/raw-requests.md +1 -1
- data/docs/root-elements.md +41 -6
- data/flexirest.gemspec +1 -1
- data/lib/flexirest/configuration.rb +26 -0
- data/lib/flexirest/request.rb +35 -11
- data/lib/flexirest/version.rb +1 -1
- data/spec/lib/request_spec.rb +139 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53b34d9600101b9d80a61559054a3599242a7739b12845df02bde410c5858d94
|
4
|
+
data.tar.gz: e7398bcc97a8a0f89fb3380110e126e482d5fc1e14a973dd9e34208f28634814
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32014dfe143032f040ae140cdcf562f05832f74d0b05e692b8f866d4636363b6f7ffcbe0a4de0e7a895c8b1722aef0f6736f34c130916dc5c02869caa198434a
|
7
|
+
data.tar.gz: a7f3cb00fd3451c4f06ef32bf6e148f90d4046c1cce7526a8bfe6c247e2d65dcd68a0d924f85683ef052929432ddf341768578a7591b37db8b7729ed96053c80
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# *Flexirest:* Filtering result lists
|
2
2
|
|
3
|
-
If the API returns a JSON list of items, this is
|
3
|
+
If the API returns a JSON list of items, this is returned to you as a `Flexirest::ResultIterator` object. A `ResultIterator` sorts simple filtering of the list using a `where` method based on values matching a specified criteria (or matching using regular expressions):
|
4
4
|
|
5
5
|
```ruby
|
6
6
|
class Article < Flexirest::Base
|
data/docs/proxying-apis.md
CHANGED
@@ -40,7 +40,7 @@ Article.all.first_name == "Billy"
|
|
40
40
|
This example does two things:
|
41
41
|
|
42
42
|
1. It rewrites the incoming URL for any requests matching "_/all_" to "/all_people"
|
43
|
-
2. It uses the `translate` method to move the "fname" attribute from the response body to be called "first_name". The translate method must return the new object at the end (either the existing object
|
43
|
+
2. It uses the `translate` method to move the "fname" attribute from the response body to be called "first_name". The translate method must return the new object at the end (either the existing object altered, or a new object to replace it with)
|
44
44
|
|
45
45
|
As the comment shows, you can use `url value` to set the request URL to a particular value, or you can call `gsub!` on the url to replace parts of it using more complicated regular expressions.
|
46
46
|
|
data/docs/raw-requests.md
CHANGED
@@ -12,7 +12,7 @@ people = Person._request('http://api.example.com/v1/people') # Defaults to get w
|
|
12
12
|
Person._request('http://api.example.com/v1/people', :post, {id:1234,name:"John"}) # Post with parameters
|
13
13
|
```
|
14
14
|
|
15
|
-
When you need to specify custom headers (for example for authentication) you can do this with a fourth option to the `_request` method. If you are using the default
|
15
|
+
When you need to specify custom headers (for example for authentication) you can do this with a fourth option to the `_request` method. If you are using the default parameters you'll need to specify them. For example:
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
Person._request("http://api.example.com/v1/people", :get, {}, {headers:{"X-Something": "foo/bar"}})
|
data/docs/root-elements.md
CHANGED
@@ -2,13 +2,32 @@
|
|
2
2
|
|
3
3
|
If your response comes back with a root node and you'd like to ignore it, you can define the mapping as:
|
4
4
|
|
5
|
+
```ruby
|
6
|
+
Flexirest::Base.ignore_root = "data"
|
7
|
+
```
|
8
|
+
|
9
|
+
Any `ignore_root` setting in specific class overrides this declared default.
|
10
|
+
|
5
11
|
```ruby
|
6
12
|
class Feed < Flexirest::Base
|
7
|
-
|
13
|
+
ignore_root: "feed"
|
14
|
+
|
15
|
+
post :list, "/feed"
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
And any `ignore_root` setting in specific request overrides the both default and class specific setting.
|
20
|
+
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class Feed < Flexirest::Base
|
24
|
+
ignore_root: 'feed'
|
25
|
+
|
26
|
+
post :list, "/feed", ignore_root: "result"
|
8
27
|
end
|
9
28
|
```
|
10
29
|
|
11
|
-
|
30
|
+
You can also assign an array to `ignore_root` if you'd want to remove a tree of root nodes.
|
12
31
|
|
13
32
|
```ruby
|
14
33
|
class Feed < Flexirest::Base
|
@@ -26,16 +45,32 @@ Alternatively if you want to wrap your JSON request body in a root element, e.g.
|
|
26
45
|
}
|
27
46
|
```
|
28
47
|
|
29
|
-
You can do it
|
48
|
+
You can do it using `wrap_root`:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Flexirest::Base.wrap_root = "data"
|
52
|
+
```
|
53
|
+
|
54
|
+
Any `wrap_root` setting in specific class overrides this declared default.
|
30
55
|
|
31
56
|
```ruby
|
32
57
|
class Feed < Flexirest::Base
|
33
|
-
|
34
|
-
end
|
58
|
+
wrap_root: "feed"
|
35
59
|
|
36
|
-
|
60
|
+
post :list, "/feed"
|
61
|
+
end
|
37
62
|
```
|
38
63
|
|
64
|
+
And any `wrap_root` setting in specific request overrides the both default and class specific setting.
|
65
|
+
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class Feed < Flexirest::Base
|
69
|
+
wrap_root: 'feed'
|
70
|
+
|
71
|
+
post :list, "/feed", wrap_root: "data"
|
72
|
+
end
|
73
|
+
```
|
39
74
|
|
40
75
|
-----
|
41
76
|
|
data/flexirest.gemspec
CHANGED
@@ -39,7 +39,7 @@ Gem::Specification.new do |spec|
|
|
39
39
|
spec.add_development_dependency "api-auth", ">= 1.3.1", "< 2.4"
|
40
40
|
spec.add_development_dependency 'typhoeus'
|
41
41
|
spec.add_development_dependency 'activemodel'
|
42
|
-
spec.add_development_dependency 'rest-client'
|
42
|
+
spec.add_development_dependency 'rest-client'
|
43
43
|
|
44
44
|
spec.add_runtime_dependency "mime-types"
|
45
45
|
spec.add_runtime_dependency "multi_json"
|
@@ -13,6 +13,32 @@ module Flexirest
|
|
13
13
|
@api_auth_access_id = nil
|
14
14
|
@api_auth_secret_key = nil
|
15
15
|
@api_auth_options = {}
|
16
|
+
@ignore_root = nil
|
17
|
+
@wrap_root = nil
|
18
|
+
|
19
|
+
def ignore_root(value=nil)
|
20
|
+
if value.nil?
|
21
|
+
value = if @ignore_root.nil? && superclass.respond_to?(:ignore_root)
|
22
|
+
superclass.ignore_root
|
23
|
+
else
|
24
|
+
@ignore_root
|
25
|
+
end
|
26
|
+
else
|
27
|
+
@ignore_root = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def wrap_root(value=nil)
|
32
|
+
if value.nil?
|
33
|
+
value = if @wrap_root.nil? && superclass.respond_to?(:wrap_root)
|
34
|
+
superclass.wrap_root
|
35
|
+
else
|
36
|
+
@wrap_root
|
37
|
+
end
|
38
|
+
else
|
39
|
+
@wrap_root = value
|
40
|
+
end
|
41
|
+
end
|
16
42
|
|
17
43
|
def base_url(value = nil)
|
18
44
|
@base_url ||= nil
|
data/lib/flexirest/request.rb
CHANGED
@@ -136,6 +136,30 @@ module Flexirest
|
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
139
|
+
def ignore_root
|
140
|
+
if @method[:options][:ignore_root]
|
141
|
+
@method[:options][:ignore_root]
|
142
|
+
elsif @object.nil?
|
143
|
+
nil
|
144
|
+
elsif object_is_class?
|
145
|
+
@object.ignore_root
|
146
|
+
else
|
147
|
+
@object.class.ignore_root
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def wrap_root
|
152
|
+
if @method[:options][:wrap_root]
|
153
|
+
@method[:options][:wrap_root]
|
154
|
+
elsif @object.nil?
|
155
|
+
nil
|
156
|
+
elsif object_is_class?
|
157
|
+
@object.wrap_root
|
158
|
+
else
|
159
|
+
@object.class.wrap_root
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
139
163
|
def verbose?
|
140
164
|
if object_is_class?
|
141
165
|
@object.verbose
|
@@ -438,8 +462,8 @@ module Flexirest
|
|
438
462
|
@post_params
|
439
463
|
else
|
440
464
|
p = (params || @post_params || {})
|
441
|
-
if
|
442
|
-
p = {
|
465
|
+
if wrap_root.present?
|
466
|
+
p = {wrap_root => p}
|
443
467
|
end
|
444
468
|
p.to_query
|
445
469
|
end
|
@@ -452,8 +476,8 @@ module Flexirest
|
|
452
476
|
@post_params
|
453
477
|
else
|
454
478
|
p = (params || @post_params || {})
|
455
|
-
if
|
456
|
-
p = {
|
479
|
+
if wrap_root.present?
|
480
|
+
p = {wrap_root => p}
|
457
481
|
end
|
458
482
|
data, mp_headers = Flexirest::Multipart::Post.prepare_query(p)
|
459
483
|
mp_headers.each do |k,v|
|
@@ -467,8 +491,8 @@ module Flexirest
|
|
467
491
|
elsif @post_params.is_a?(String)
|
468
492
|
@post_params
|
469
493
|
else
|
470
|
-
if
|
471
|
-
{
|
494
|
+
if wrap_root.present?
|
495
|
+
{wrap_root => (params || @post_params || {})}.to_json
|
472
496
|
else
|
473
497
|
(params || @post_params || {}).to_json
|
474
498
|
end
|
@@ -601,7 +625,7 @@ module Flexirest
|
|
601
625
|
else
|
602
626
|
Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Response received #{@response.body.size} bytes"
|
603
627
|
end
|
604
|
-
result = generate_new_object(ignore_root:
|
628
|
+
result = generate_new_object(ignore_root: ignore_root, ignore_xml_root: @method[:options][:ignore_xml_root])
|
605
629
|
# TODO: Cleanup when ignore_xml_root is removed
|
606
630
|
else
|
607
631
|
raise ResponseParseException.new(status:status, body:@response.body, headers: @response.headers)
|
@@ -814,15 +838,15 @@ module Flexirest
|
|
814
838
|
body = JsonAPIProxy::Response.parse(body, @object)
|
815
839
|
end
|
816
840
|
|
817
|
-
if
|
818
|
-
[
|
841
|
+
if ignore_root
|
842
|
+
[ignore_root].flatten.each do |key|
|
819
843
|
body = body[key.to_s]
|
820
844
|
end
|
821
845
|
end
|
822
846
|
elsif is_xml_response?
|
823
847
|
body = @response.body.blank? ? {} : Crack::XML.parse(@response.body)
|
824
|
-
if
|
825
|
-
[
|
848
|
+
if ignore_root
|
849
|
+
[ignore_root].flatten.each do |key|
|
826
850
|
body = body[key.to_s]
|
827
851
|
end
|
828
852
|
elsif options[:ignore_xml_root]
|
data/lib/flexirest/version.rb
CHANGED
data/spec/lib/request_spec.rb
CHANGED
@@ -199,6 +199,89 @@ describe Flexirest::Request do
|
|
199
199
|
}
|
200
200
|
end
|
201
201
|
|
202
|
+
class LocalIgnoredRootExampleClient < ExampleClient
|
203
|
+
ignore_root "feed"
|
204
|
+
|
205
|
+
get :root, "/root", fake: %Q{
|
206
|
+
{
|
207
|
+
"feed": {
|
208
|
+
"title": "Example Feed"
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}
|
212
|
+
end
|
213
|
+
|
214
|
+
class LocalIgnoredMultiLevelRootExampleClient < ExampleClient
|
215
|
+
ignore_root [:response, "data", "object"]
|
216
|
+
|
217
|
+
get :multi_level_root, "/multi-level-root", fake: %Q{
|
218
|
+
{
|
219
|
+
"response": {
|
220
|
+
"data": {
|
221
|
+
"object": {
|
222
|
+
"title": "Example Multi Level Feed"
|
223
|
+
}
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
end
|
229
|
+
|
230
|
+
class BaseIgnoredRootExampleClient < Flexirest::Base
|
231
|
+
base_url "http://www.example.com"
|
232
|
+
ignore_root "feed"
|
233
|
+
end
|
234
|
+
|
235
|
+
class GlobalIgnoredRootExampleClient < BaseIgnoredRootExampleClient
|
236
|
+
get :root, "/root", fake: %Q{
|
237
|
+
{
|
238
|
+
"feed": {
|
239
|
+
"title": "Example Feed"
|
240
|
+
}
|
241
|
+
}
|
242
|
+
}
|
243
|
+
end
|
244
|
+
|
245
|
+
class OverrideGlobalIgnoredRootForFileExampleClient < BaseIgnoredRootExampleClient
|
246
|
+
ignore_root "data"
|
247
|
+
|
248
|
+
get :root, "/root", fake: %Q{
|
249
|
+
{
|
250
|
+
"data": {
|
251
|
+
"title": "Example Feed"
|
252
|
+
}
|
253
|
+
}
|
254
|
+
}
|
255
|
+
end
|
256
|
+
|
257
|
+
class OverrideGlobalIgnoredRootForRequestExampleClient < BaseIgnoredRootExampleClient
|
258
|
+
get :root, "/root", ignore_root: "data", fake: %Q{
|
259
|
+
{
|
260
|
+
"data": {
|
261
|
+
"title": "Example Feed"
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
end
|
266
|
+
|
267
|
+
class BaseWrappedRootExampleClient < Flexirest::Base
|
268
|
+
base_url "http://www.example.com"
|
269
|
+
wrap_root "base_data"
|
270
|
+
end
|
271
|
+
|
272
|
+
class GlobalWrappedRootExampleClient < BaseWrappedRootExampleClient
|
273
|
+
put :wrapped, "/put/:id"
|
274
|
+
end
|
275
|
+
|
276
|
+
class OverrideGlobalWrappedRootForFileExampleClient < BaseWrappedRootExampleClient
|
277
|
+
wrap_root "class_specific_data"
|
278
|
+
put :wrapped, "/put/:id"
|
279
|
+
end
|
280
|
+
|
281
|
+
class OverrideGlobalWrappedRootForRequestExampleClient < BaseWrappedRootExampleClient
|
282
|
+
put :wrapped, "/put/:id", wrap_root: "request_specific_data"
|
283
|
+
end
|
284
|
+
|
202
285
|
class WhitelistedDateClient < Flexirest::Base
|
203
286
|
base_url "http://www.example.com"
|
204
287
|
put :conversion, "/put/:id"
|
@@ -1355,6 +1438,62 @@ describe Flexirest::Request do
|
|
1355
1438
|
expect(IgnoredMultiLevelRootExampleClient.multi_level_root.title).to eq("Example Multi Level Feed")
|
1356
1439
|
end
|
1357
1440
|
|
1441
|
+
it "should ignore a specified root element" do
|
1442
|
+
expect(LocalIgnoredRootExampleClient.root.title).to eq("Example Feed")
|
1443
|
+
end
|
1444
|
+
|
1445
|
+
it "should ignore a specified multi-level root element" do
|
1446
|
+
expect(LocalIgnoredMultiLevelRootExampleClient.multi_level_root.title).to eq("Example Multi Level Feed")
|
1447
|
+
end
|
1448
|
+
|
1449
|
+
it "should ignore a specified root element" do
|
1450
|
+
expect(GlobalIgnoredRootExampleClient.root.title).to eq("Example Feed")
|
1451
|
+
end
|
1452
|
+
|
1453
|
+
it "should ignore a specified root element" do
|
1454
|
+
expect(OverrideGlobalIgnoredRootForFileExampleClient.root.title).to eq("Example Feed")
|
1455
|
+
end
|
1456
|
+
|
1457
|
+
it "should ignore a specified root element" do
|
1458
|
+
expect(OverrideGlobalIgnoredRootForRequestExampleClient.root.title).to eq("Example Feed")
|
1459
|
+
end
|
1460
|
+
|
1461
|
+
it "should wrap elements if specified, in form-encoded format" do
|
1462
|
+
expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q(base_data%5Bdebug%5D=true&base_data%5Btest%5D=foo), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
|
1463
|
+
GlobalWrappedRootExampleClient.request_body_type :form_encoded
|
1464
|
+
GlobalWrappedRootExampleClient.wrapped id:1234, debug:true, test:'foo'
|
1465
|
+
end
|
1466
|
+
|
1467
|
+
it "should wrap elements if specified, in form-encoded format" do
|
1468
|
+
expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q(class_specific_data%5Bdebug%5D=true&class_specific_data%5Btest%5D=foo), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
|
1469
|
+
OverrideGlobalWrappedRootForFileExampleClient.request_body_type :form_encoded
|
1470
|
+
OverrideGlobalWrappedRootForFileExampleClient.wrapped id:1234, debug:true, test:'foo'
|
1471
|
+
end
|
1472
|
+
|
1473
|
+
it "should wrap elements if specified, in form-encoded format" do
|
1474
|
+
expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q(request_specific_data%5Bdebug%5D=true&request_specific_data%5Btest%5D=foo), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
|
1475
|
+
OverrideGlobalWrappedRootForRequestExampleClient.request_body_type :form_encoded
|
1476
|
+
OverrideGlobalWrappedRootForRequestExampleClient.wrapped id:1234, debug:true, test:'foo'
|
1477
|
+
end
|
1478
|
+
|
1479
|
+
it "should encode the body wrapped in a root element in a JSON format if specified" do
|
1480
|
+
expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q({"base_data":{"debug":true,"test":"foo"}}), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
|
1481
|
+
GlobalWrappedRootExampleClient.request_body_type :json
|
1482
|
+
GlobalWrappedRootExampleClient.wrapped id:1234, debug:true, test:'foo'
|
1483
|
+
end
|
1484
|
+
|
1485
|
+
it "should encode the body wrapped in a root element in a JSON format if specified" do
|
1486
|
+
expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q({"class_specific_data":{"debug":true,"test":"foo"}}), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
|
1487
|
+
OverrideGlobalWrappedRootForFileExampleClient.request_body_type :json
|
1488
|
+
OverrideGlobalWrappedRootForFileExampleClient.wrapped id:1234, debug:true, test:'foo'
|
1489
|
+
end
|
1490
|
+
|
1491
|
+
it "should encode the body wrapped in a root element in a JSON format if specified" do
|
1492
|
+
expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q({"request_specific_data":{"debug":true,"test":"foo"}}), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
|
1493
|
+
OverrideGlobalWrappedRootForRequestExampleClient.request_body_type :json
|
1494
|
+
OverrideGlobalWrappedRootForRequestExampleClient.wrapped id:1234, debug:true, test:'foo'
|
1495
|
+
end
|
1496
|
+
|
1358
1497
|
context "Parameter preparation" do
|
1359
1498
|
method = {url: "http://www.example.com", method: :get}
|
1360
1499
|
object = nil
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flexirest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.10.
|
4
|
+
version: 1.10.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Jeffries
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -176,14 +176,14 @@ dependencies:
|
|
176
176
|
requirements:
|
177
177
|
- - ">="
|
178
178
|
- !ruby/object:Gem::Version
|
179
|
-
version:
|
179
|
+
version: '0'
|
180
180
|
type: :development
|
181
181
|
prerelease: false
|
182
182
|
version_requirements: !ruby/object:Gem::Requirement
|
183
183
|
requirements:
|
184
184
|
- - ">="
|
185
185
|
- !ruby/object:Gem::Version
|
186
|
-
version:
|
186
|
+
version: '0'
|
187
187
|
- !ruby/object:Gem::Dependency
|
188
188
|
name: mime-types
|
189
189
|
requirement: !ruby/object:Gem::Requirement
|