json_api_client 1.10.0 → 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -5
- data/lib/json_api_client/query/builder.rb +12 -2
- data/lib/json_api_client/resource.rb +2 -0
- data/lib/json_api_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fd62267b74acfe53f89ff0c7ef23db0b69fd294
|
4
|
+
data.tar.gz: 2139b42adda0d08e98de593576ae8701b35e40df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc06232298c783d5639df475dad7cdca160df99feb32c57dbba9dbd60132b29f057f8307397fe11053f2559bfe4fb3a3aa3826c3a3b343c76784884d2a87fc2d
|
7
|
+
data.tar.gz: 8bfe1faeb6d342fd9a9269efae3d2cb2ba13ad8615018c1546c0f45ac135e21a23d86dc02e6821135b32b672058087df018b0ddda9b0e654e31e0e62645584b8
|
data/README.md
CHANGED
@@ -52,14 +52,14 @@ u.update_attributes(
|
|
52
52
|
c: "d"
|
53
53
|
)
|
54
54
|
|
55
|
-
u.persisted?
|
55
|
+
u.persisted?
|
56
56
|
# => true
|
57
57
|
|
58
58
|
u.destroy
|
59
59
|
|
60
|
-
u.destroyed?
|
60
|
+
u.destroyed?
|
61
61
|
# => true
|
62
|
-
u.persisted?
|
62
|
+
u.persisted?
|
63
63
|
# => false
|
64
64
|
|
65
65
|
u = MyApi::Person.create(
|
@@ -164,7 +164,7 @@ module MyApi
|
|
164
164
|
class Account < JsonApiClient::Resource
|
165
165
|
belongs_to :user
|
166
166
|
end
|
167
|
-
|
167
|
+
|
168
168
|
class Customer < JsonApiClient::Resource
|
169
169
|
belongs_to :user, shallow_path: true
|
170
170
|
end
|
@@ -476,7 +476,7 @@ end
|
|
476
476
|
|
477
477
|
##### Custom status handler
|
478
478
|
|
479
|
-
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
|
479
|
+
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
|
480
480
|
By default it raises `JsonApiClient::Errors::ClientError` but you can skip exception if you want to process errors from the server.
|
481
481
|
You need to provide a `proc` which should call `throw(:handled)` default handler for this status should be skipped.
|
482
482
|
```ruby
|
@@ -636,6 +636,37 @@ end
|
|
636
636
|
|
637
637
|
```
|
638
638
|
|
639
|
+
### Safe singular resource fetching
|
640
|
+
|
641
|
+
That is a bit curios, but `json_api_client` returns an array from `.find` method, always.
|
642
|
+
The history of this fact was discussed [here](https://github.com/JsonApiClient/json_api_client/issues/75)
|
643
|
+
|
644
|
+
So, when we searching for a single resource by primary key, we typically write the things like
|
645
|
+
|
646
|
+
```ruby
|
647
|
+
admin = User.find(id).first
|
648
|
+
```
|
649
|
+
|
650
|
+
The next thing which we need to notice - `json_api_client` will just interpolate the incoming `.find` param to the end of API URL, just like that:
|
651
|
+
|
652
|
+
> http://somehost/api/v1/users/{id}
|
653
|
+
|
654
|
+
What will happen if we pass the blank id (nil or empty string) to the `.find` method then?.. Yeah, `json_api_client` will try to call the INDEX API endpoint instead of SHOW one:
|
655
|
+
|
656
|
+
> http://somehost/api/v1/users/
|
657
|
+
|
658
|
+
Lets sum all together - in case if `id` comes blank (from CGI for instance), we can silently receive the `admin` variable equal to some existing resource, with all the consequences.
|
659
|
+
|
660
|
+
Even worse, `admin` variable can equal to *random* resource, depends on ordering applied by INDEX endpoint.
|
661
|
+
|
662
|
+
If you prefer to get `JsonApiClient::Errors::NotFound` raised, please define in your base Resource class:
|
663
|
+
|
664
|
+
```ruby
|
665
|
+
class Resource < JsonApiClient::Resource
|
666
|
+
self.raise_on_blank_find_param = true
|
667
|
+
end
|
668
|
+
```
|
669
|
+
|
639
670
|
## Contributing
|
640
671
|
|
641
672
|
Contributions are welcome! Please fork this repo and send a pull request. Your pull request should have:
|
@@ -86,11 +86,15 @@ module JsonApiClient
|
|
86
86
|
end
|
87
87
|
|
88
88
|
def to_a
|
89
|
-
@to_a ||=
|
89
|
+
@to_a ||= _fetch
|
90
90
|
end
|
91
91
|
alias all to_a
|
92
92
|
|
93
93
|
def find(args = {})
|
94
|
+
if klass.raise_on_blank_find_param && args.blank?
|
95
|
+
raise Errors::NotFound, 'blank .find param'
|
96
|
+
end
|
97
|
+
|
94
98
|
case args
|
95
99
|
when Hash
|
96
100
|
scope = where(args)
|
@@ -98,13 +102,19 @@ module JsonApiClient
|
|
98
102
|
scope = _new_scope( primary_key: args )
|
99
103
|
end
|
100
104
|
|
101
|
-
|
105
|
+
scope._fetch
|
102
106
|
end
|
103
107
|
|
104
108
|
def method_missing(method_name, *args, &block)
|
105
109
|
to_a.send(method_name, *args, &block)
|
106
110
|
end
|
107
111
|
|
112
|
+
protected
|
113
|
+
|
114
|
+
def _fetch
|
115
|
+
klass.requestor.get(params)
|
116
|
+
end
|
117
|
+
|
108
118
|
private
|
109
119
|
|
110
120
|
def _new_scope( opts = {} )
|
@@ -36,6 +36,7 @@ module JsonApiClient
|
|
36
36
|
:keep_request_params,
|
37
37
|
:search_included_in_result_set,
|
38
38
|
:custom_type_to_class,
|
39
|
+
:raise_on_blank_find_param,
|
39
40
|
instance_accessor: false
|
40
41
|
class_attribute :add_defaults_to_changes,
|
41
42
|
instance_writer: false
|
@@ -54,6 +55,7 @@ module JsonApiClient
|
|
54
55
|
self.add_defaults_to_changes = false
|
55
56
|
self.search_included_in_result_set = false
|
56
57
|
self.custom_type_to_class = {}
|
58
|
+
self.raise_on_blank_find_param = false
|
57
59
|
|
58
60
|
#:underscored_key, :camelized_key, :dasherized_key, or custom
|
59
61
|
self.json_key_format = :underscored_key
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Ching
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|