json_api_client 1.11.0 → 1.12.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 +12 -4
- data/lib/json_api_client/errors.rb +38 -8
- data/lib/json_api_client/middleware/status.rb +3 -1
- data/lib/json_api_client/version.rb +1 -1
- metadata +21 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cfb32875fc8446747a7d25e363af32fcd2b7667
|
4
|
+
data.tar.gz: '0178c432e6547f8d542d05f138b0b80642cdb724'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60f9e5bb268999432abfb9253384dcc75d2fa8ba7321324ec2a55a8003f94c7241ffd08c8cfbe3ca6120f8a7a78c059e5a95fb3d15b7823cbb60686a52729259
|
7
|
+
data.tar.gz: 5c7540a4f1d9d43be2c55864d5483155c1b89ff543ab005a8ecea749b5c9cd591755f5d795ad9e1bcee7e0454eabdb377b1f6298605ba628b7c3aee14a8cc6a2
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
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
|
|
5
|
-
*Note: master is currently tracking the 1.0.0 specification. If you're looking for the older code, see [0.x branch](https://github.com/
|
5
|
+
*Note: master is currently tracking the 1.0.0 specification. If you're looking for the older code, see [0.x branch](https://github.com/JsonApiClient/json_api_client/tree/0.x)*
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
@@ -474,6 +474,14 @@ module MyApi
|
|
474
474
|
end
|
475
475
|
```
|
476
476
|
|
477
|
+
##### Server errors handling
|
478
|
+
|
479
|
+
Non-success API response will cause the specific `JsonApiClient::Errors::SomeException` raised, depends on responded HTTP status.
|
480
|
+
Please refer to [JsonApiClient::Middleware::Status#handle_status](https://github.com/JsonApiClient/json_api_client/blob/master/lib/json_api_client/middleware/status.rb)
|
481
|
+
method for concrete status-to-exception mapping used out of the box.
|
482
|
+
|
483
|
+
JsonApiClient will try determine is failed API response JsonApi-compatible, if so - JsonApi error messages will be parsed from response body, and tracked as a part of particular exception message. In additional, `JsonApiClient::Errors::ServerError` exception will keep the actual HTTP status and message within its message.
|
484
|
+
|
477
485
|
##### Custom status handler
|
478
486
|
|
479
487
|
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
|
@@ -569,7 +577,7 @@ end
|
|
569
577
|
|
570
578
|
You can customize how your resources find pagination information from the response.
|
571
579
|
|
572
|
-
If the [existing paginator](https://github.com/
|
580
|
+
If the [existing paginator](https://github.com/JsonApiClient/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) fits your requirements but you don't use the default `page` and `per_page` params for pagination, you can customise the param keys as follows:
|
573
581
|
|
574
582
|
```ruby
|
575
583
|
JsonApiClient::Paginating::Paginator.page_param = "number"
|
@@ -578,7 +586,7 @@ JsonApiClient::Paginating::Paginator.per_page_param = "size"
|
|
578
586
|
|
579
587
|
Please note that this is a global configuration, so library authors should create a custom paginator that inherits `JsonApiClient::Paginating::Paginator` and configure the custom paginator to avoid modifying global config.
|
580
588
|
|
581
|
-
If the [existing paginator](https://github.com/
|
589
|
+
If the [existing paginator](https://github.com/JsonApiClient/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) does not fit your needs, you can create a custom paginator:
|
582
590
|
|
583
591
|
```ruby
|
584
592
|
class MyPaginator
|
@@ -680,4 +688,4 @@ required. The commits will be squashed into master once accepted.
|
|
680
688
|
|
681
689
|
## Changelog
|
682
690
|
|
683
|
-
See [changelog](https://github.com/
|
691
|
+
See [changelog](https://github.com/JsonApiClient/json_api_client/blob/master/CHANGELOG.md)
|
@@ -1,10 +1,31 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
1
3
|
module JsonApiClient
|
2
4
|
module Errors
|
3
5
|
class ApiError < StandardError
|
4
6
|
attr_reader :env
|
7
|
+
|
5
8
|
def initialize(env, msg = nil)
|
6
|
-
super msg
|
7
9
|
@env = env
|
10
|
+
# Try to fetch json_api errors from response
|
11
|
+
msg = track_json_api_errors(msg)
|
12
|
+
|
13
|
+
super msg
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# Try to fetch json_api errors from response
|
19
|
+
def track_json_api_errors(msg)
|
20
|
+
return msg unless env.try(:body).kind_of?(Hash) || env.body.key?('errors')
|
21
|
+
|
22
|
+
errors_msg = env.body['errors'].map { |e| e['title'] }.compact.join('; ').presence
|
23
|
+
return msg unless errors_msg
|
24
|
+
|
25
|
+
msg.nil? ? errors_msg : "#{msg} (#{errors_msg})"
|
26
|
+
# Just to be sure that it is back compatible
|
27
|
+
rescue StandardError
|
28
|
+
msg
|
8
29
|
end
|
9
30
|
end
|
10
31
|
|
@@ -21,7 +42,13 @@ module JsonApiClient
|
|
21
42
|
end
|
22
43
|
|
23
44
|
class ServerError < ApiError
|
24
|
-
def initialize(env, msg =
|
45
|
+
def initialize(env, msg = nil)
|
46
|
+
msg ||= begin
|
47
|
+
status = env.status
|
48
|
+
message = ::Rack::Utils::HTTP_STATUS_CODES[status]
|
49
|
+
"#{status} #{message}"
|
50
|
+
end
|
51
|
+
|
25
52
|
super env, msg
|
26
53
|
end
|
27
54
|
end
|
@@ -36,20 +63,23 @@ module JsonApiClient
|
|
36
63
|
attr_reader :uri
|
37
64
|
def initialize(uri)
|
38
65
|
@uri = uri
|
39
|
-
|
40
|
-
|
41
|
-
|
66
|
+
|
67
|
+
msg = "Couldn't find resource at: #{uri.to_s}"
|
68
|
+
super nil, msg
|
42
69
|
end
|
43
70
|
end
|
44
71
|
|
72
|
+
class InternalServerError < ServerError
|
73
|
+
end
|
74
|
+
|
45
75
|
class UnexpectedStatus < ServerError
|
46
76
|
attr_reader :code, :uri
|
47
77
|
def initialize(code, uri)
|
48
78
|
@code = code
|
49
79
|
@uri = uri
|
50
|
-
|
51
|
-
|
52
|
-
|
80
|
+
|
81
|
+
msg = "Unexpected response status: #{code} from: #{uri.to_s}"
|
82
|
+
super nil, msg
|
53
83
|
end
|
54
84
|
end
|
55
85
|
end
|
@@ -44,7 +44,9 @@ module JsonApiClient
|
|
44
44
|
# Allow to proceed as resource errors will be populated
|
45
45
|
when 400..499
|
46
46
|
raise Errors::ClientError, env
|
47
|
-
when 500
|
47
|
+
when 500
|
48
|
+
raise Errors::InternalServerError, env
|
49
|
+
when 501..599
|
48
50
|
raise Errors::ServerError, env
|
49
51
|
else
|
50
52
|
raise Errors::UnexpectedStatus.new(code, env[:url])
|
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.12.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-
|
11
|
+
date: 2019-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -87,19 +87,33 @@ dependencies:
|
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: 3.2.0
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
|
-
name:
|
90
|
+
name: rack
|
91
91
|
requirement: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
93
|
- - ">="
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: '0'
|
96
|
-
type: :
|
95
|
+
version: '0.2'
|
96
|
+
type: :runtime
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - ">="
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: '0'
|
102
|
+
version: '0.2'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: webmock
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 3.5.1
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 3.5.1
|
103
117
|
- !ruby/object:Gem::Dependency
|
104
118
|
name: mocha
|
105
119
|
requirement: !ruby/object:Gem::Requirement
|
@@ -185,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
199
|
version: '0'
|
186
200
|
requirements: []
|
187
201
|
rubyforge_project:
|
188
|
-
rubygems_version: 2.6.14
|
202
|
+
rubygems_version: 2.6.14.3
|
189
203
|
signing_key:
|
190
204
|
specification_version: 4
|
191
205
|
summary: Build client libraries compliant with specification defined by jsonapi.org
|