json_api_client 1.17.0 → 1.20.0
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 +1 -1
- data/lib/json_api_client/associations/base_association.rb +1 -1
- data/lib/json_api_client/associations/has_one.rb +1 -1
- data/lib/json_api_client/errors.rb +38 -12
- data/lib/json_api_client/middleware/status.rb +10 -0
- data/lib/json_api_client/query/builder.rb +2 -2
- data/lib/json_api_client/query/requestor.rb +12 -8
- data/lib/json_api_client/resource.rb +17 -2
- data/lib/json_api_client/schema.rb +1 -1
- data/lib/json_api_client/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 399ea138115c4282afb7f24a8c6981533fc0e67d3e80078084002b8ab8cec5bd
|
4
|
+
data.tar.gz: aa737c7689874c8aef404b6aeabee2173b2294178486963928a2690495def95a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32657956fe3e7fc03e54d41c434a169613ee490684354cf1d59d864247743197de0858360a4d9e3f3003d073773563775bc8ca70a43be3948acb1c0557bd798c
|
7
|
+
data.tar.gz: 7aafe127cdc28013bf01e527c98b57f151004b49a5b9a482f12cd03ccce00b60e831c8aab80545651e4712ccb1a5f22bd04b157476503b01ab5ab20e4537750b
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# JsonApiClient [](https://travis-ci.org/JsonApiClient/json_api_client) [](https://codeclimate.com/github/JsonApiClient/json_api_client) [](https://codeclimate.com/github/JsonApiClient/json_api_client)
|
1
|
+
# JsonApiClient [](https://travis-ci.org/JsonApiClient/json_api_client) [](https://codeclimate.com/github/JsonApiClient/json_api_client) [](https://codeclimate.com/github/JsonApiClient/json_api_client)
|
2
2
|
|
3
3
|
This gem is meant to help you build an API client for interacting with REST APIs as laid out by [http://jsonapi.org](http://jsonapi.org). It attempts to give you a query building framework that is easy to understand (it is similar to ActiveRecord scopes).
|
4
4
|
|
@@ -24,7 +24,7 @@ module JsonApiClient
|
|
24
24
|
|
25
25
|
def load_records(data)
|
26
26
|
data.map do |d|
|
27
|
-
record_class = Utils.compute_type(klass, d["type"].classify)
|
27
|
+
record_class = Utils.compute_type(klass, klass.key_formatter.unformat(d["type"]).classify)
|
28
28
|
record_class.load id: d["id"]
|
29
29
|
end
|
30
30
|
end
|
@@ -7,7 +7,7 @@ module JsonApiClient
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def load_records(data)
|
10
|
-
record_class = Utils.compute_type(klass, data["type"].classify)
|
10
|
+
record_class = Utils.compute_type(klass, klass.key_formatter.unformat(data["type"]).classify)
|
11
11
|
record_class.load id: data["id"]
|
12
12
|
end
|
13
13
|
end
|
@@ -44,6 +44,28 @@ module JsonApiClient
|
|
44
44
|
class NotAuthorized < ClientError
|
45
45
|
end
|
46
46
|
|
47
|
+
class NotFound < ClientError
|
48
|
+
attr_reader :uri
|
49
|
+
def initialize(uri)
|
50
|
+
@uri = uri
|
51
|
+
|
52
|
+
msg = "Resource not found: #{uri.to_s}"
|
53
|
+
super nil, msg
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class RequestTimeout < ClientError
|
58
|
+
end
|
59
|
+
|
60
|
+
class Conflict < ClientError
|
61
|
+
def initialize(env, msg = 'Resource already exists')
|
62
|
+
super env, msg
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class TooManyRequests < ClientError
|
67
|
+
end
|
68
|
+
|
47
69
|
class ConnectionError < ApiError
|
48
70
|
end
|
49
71
|
|
@@ -59,23 +81,16 @@ module JsonApiClient
|
|
59
81
|
end
|
60
82
|
end
|
61
83
|
|
62
|
-
class
|
63
|
-
def initialize(env, msg = 'Resource already exists')
|
64
|
-
super env, msg
|
65
|
-
end
|
84
|
+
class InternalServerError < ServerError
|
66
85
|
end
|
67
86
|
|
68
|
-
class
|
69
|
-
|
70
|
-
def initialize(uri)
|
71
|
-
@uri = uri
|
87
|
+
class BadGateway < ServerError
|
88
|
+
end
|
72
89
|
|
73
|
-
|
74
|
-
super nil, msg
|
75
|
-
end
|
90
|
+
class ServiceUnavailable < ServerError
|
76
91
|
end
|
77
92
|
|
78
|
-
class
|
93
|
+
class GatewayTimeout < ServerError
|
79
94
|
end
|
80
95
|
|
81
96
|
class UnexpectedStatus < ServerError
|
@@ -88,5 +103,16 @@ module JsonApiClient
|
|
88
103
|
super nil, msg
|
89
104
|
end
|
90
105
|
end
|
106
|
+
|
107
|
+
class RecordNotSaved < ServerError
|
108
|
+
attr_reader :record
|
109
|
+
|
110
|
+
def initialize(message = nil, record = nil)
|
111
|
+
@record = record
|
112
|
+
end
|
113
|
+
def message
|
114
|
+
"Record not saved"
|
115
|
+
end
|
116
|
+
end
|
91
117
|
end
|
92
118
|
end
|
@@ -38,14 +38,24 @@ module JsonApiClient
|
|
38
38
|
raise Errors::AccessDenied, env
|
39
39
|
when 404
|
40
40
|
raise Errors::NotFound, env[:url]
|
41
|
+
when 408
|
42
|
+
raise Errors::RequestTimeout, env
|
41
43
|
when 409
|
42
44
|
raise Errors::Conflict, env
|
43
45
|
when 422
|
44
46
|
# Allow to proceed as resource errors will be populated
|
47
|
+
when 429
|
48
|
+
raise Errors::TooManyRequests, env
|
45
49
|
when 400..499
|
46
50
|
raise Errors::ClientError, env
|
47
51
|
when 500
|
48
52
|
raise Errors::InternalServerError, env
|
53
|
+
when 502
|
54
|
+
raise Errors::BadGateway, env
|
55
|
+
when 503
|
56
|
+
raise Errors::ServiceUnavailable, env
|
57
|
+
when 504
|
58
|
+
raise Errors::GatewayTimeout, env
|
49
59
|
when 501..599
|
50
60
|
raise Errors::ServerError, env
|
51
61
|
else
|
@@ -67,11 +67,11 @@ module JsonApiClient
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def build(attrs = {})
|
70
|
-
klass.new @path_params.merge(attrs.
|
70
|
+
klass.new @path_params.merge(attrs.with_indifferent_access)
|
71
71
|
end
|
72
72
|
|
73
73
|
def create(attrs = {})
|
74
|
-
klass.create @path_params.merge(attrs.
|
74
|
+
klass.create @path_params.merge(attrs.with_indifferent_access)
|
75
75
|
end
|
76
76
|
|
77
77
|
def params
|
@@ -10,17 +10,21 @@ module JsonApiClient
|
|
10
10
|
|
11
11
|
# expects a record
|
12
12
|
def create(record)
|
13
|
-
request(
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
request(
|
14
|
+
:post,
|
15
|
+
klass.path(record.path_attributes),
|
16
|
+
body: { data: record.as_json_api },
|
17
|
+
params: record.request_params.to_params
|
18
|
+
)
|
17
19
|
end
|
18
20
|
|
19
21
|
def update(record)
|
20
|
-
request(
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
request(
|
23
|
+
:patch,
|
24
|
+
resource_path(record.path_attributes),
|
25
|
+
body: { data: record.as_json_api },
|
26
|
+
params: record.request_params.to_params
|
27
|
+
)
|
24
28
|
end
|
25
29
|
|
26
30
|
def get(params = {})
|
@@ -172,6 +172,12 @@ module JsonApiClient
|
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
175
|
+
def create!(attributes = {})
|
176
|
+
new(attributes).tap do |resource|
|
177
|
+
raise(Errors::RecordNotSaved.new("Failed to save the record", resource)) unless resource.save
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
175
181
|
# Within the given block, add these headers to all requests made by
|
176
182
|
# the resource class
|
177
183
|
#
|
@@ -348,7 +354,7 @@ module JsonApiClient
|
|
348
354
|
#
|
349
355
|
# @param params [Hash] Attributes, links, and relationships
|
350
356
|
def initialize(params = {})
|
351
|
-
params = params.
|
357
|
+
params = params.with_indifferent_access
|
352
358
|
@persisted = nil
|
353
359
|
@destroyed = nil
|
354
360
|
self.links = self.class.linker.new(params.delete(:links) || {})
|
@@ -376,6 +382,11 @@ module JsonApiClient
|
|
376
382
|
save
|
377
383
|
end
|
378
384
|
|
385
|
+
def update_attributes!(attrs = {})
|
386
|
+
self.attributes = attrs
|
387
|
+
save ? true : raise(Errors::RecordNotSaved.new("Failed to update the record", self))
|
388
|
+
end
|
389
|
+
|
379
390
|
# Alias to update_attributes
|
380
391
|
#
|
381
392
|
# @param attrs [Hash] Attributes to update
|
@@ -384,6 +395,10 @@ module JsonApiClient
|
|
384
395
|
update_attributes(attrs)
|
385
396
|
end
|
386
397
|
|
398
|
+
def update!(attrs = {})
|
399
|
+
update_attributes!(attrs)
|
400
|
+
end
|
401
|
+
|
387
402
|
# Mark the record as persisted
|
388
403
|
def mark_as_persisted!
|
389
404
|
@persisted = true
|
@@ -538,7 +553,7 @@ module JsonApiClient
|
|
538
553
|
end
|
539
554
|
|
540
555
|
def path_attributes
|
541
|
-
_belongs_to_params.merge attributes.slice( self.class.primary_key ).
|
556
|
+
_belongs_to_params.merge attributes.slice( self.class.primary_key ).with_indifferent_access
|
542
557
|
end
|
543
558
|
|
544
559
|
protected
|
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.20.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:
|
11
|
+
date: 2021-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -205,7 +205,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
205
205
|
- !ruby/object:Gem::Version
|
206
206
|
version: '0'
|
207
207
|
requirements: []
|
208
|
-
rubygems_version: 3.0.
|
208
|
+
rubygems_version: 3.0.3
|
209
209
|
signing_key:
|
210
210
|
specification_version: 4
|
211
211
|
summary: Build client libraries compliant with specification defined by jsonapi.org
|