json_api_client 1.17.1 → 1.21.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0177efb037e8cf64cb3dae65d29ff7dcadb09de9ea502f722da0086db4b7faab
4
- data.tar.gz: 71650f09239773eea01bafdc8bc2527ec2f6b6142f268dfe6cba3deccd4bb4a9
3
+ metadata.gz: 5f4a758332e3d5a88f73ad623771ea645a6c68c4329b6c0f8616fa0a4760e72c
4
+ data.tar.gz: 8ad725ee77c0aebdbae7020d37b02e155e272a7e6a33fc475bb80a19f6b23eb2
5
5
  SHA512:
6
- metadata.gz: e8d4cde22354f51620a16bbc7b05f7e81a77aaf5a664731af6603d71be81139cafeede497e0f0be539f8d4ecc8b53b83bf01c71fa966ada1fc0904f499baa780
7
- data.tar.gz: 0d640bae03a241523468eb1a4d11b2073a65b3fad000d066203ea1ce79c6765700d9a303fda8c1049c25c4fab9d750084bacac97300f6398172b11b0a064fa06
6
+ metadata.gz: 208619a74a51642c54c6793194ff2178f6c07aa1e4e54b78b0d8a176f55319dbc114900aa09acd536a90d5679538b57bccc825e24fe48e03c67077b7f2b18798
7
+ data.tar.gz: 1521db737a029e3b1cdadf22e044c1a7d8ebdbc7171792e7c291df3db7c991a5df7aba786fa593aea6523c6fa2d26231e1cf6e1d87c86c1020fde42f960aaea9
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # JsonApiClient [![Build Status](https://travis-ci.org/JsonApiClient/json_api_client.png)](https://travis-ci.org/JsonApiClient/json_api_client) [![Code Climate](https://codeclimate.com/github/JsonApiClient/json_api_client.png)](https://codeclimate.com/github/JsonApiClient/json_api_client) [![Code Coverage](https://codeclimate.com/github/JsonApiClient/json_api_client/coverage.png)](https://codeclimate.com/github/JsonApiClient/json_api_client)
1
+ # JsonApiClient [![Build Status](https://travis-ci.org/JsonApiClient/json_api_client.png?branch=master)](https://travis-ci.org/JsonApiClient/json_api_client) [![Code Climate](https://codeclimate.com/github/JsonApiClient/json_api_client.png)](https://codeclimate.com/github/JsonApiClient/json_api_client) [![Code Coverage](https://codeclimate.com/github/JsonApiClient/json_api_client/coverage.png)](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 Conflict < ServerError
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 NotFound < ServerError
69
- attr_reader :uri
70
- def initialize(uri)
71
- @uri = uri
87
+ class BadGateway < ServerError
88
+ end
72
89
 
73
- msg = "Couldn't find resource at: #{uri.to_s}"
74
- super nil, msg
75
- end
90
+ class ServiceUnavailable < ServerError
76
91
  end
77
92
 
78
- class InternalServerError < ServerError
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.symbolize_keys)
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.symbolize_keys)
74
+ klass.create @path_params.merge(attrs.with_indifferent_access)
75
75
  end
76
76
 
77
77
  def 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.symbolize_keys
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 ).symbolize_keys
556
+ _belongs_to_params.merge attributes.slice( self.class.primary_key ).with_indifferent_access
542
557
  end
543
558
 
544
559
  protected
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.17.1"
2
+ VERSION = "1.21.0"
3
3
  end
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.17.1
4
+ version: 1.21.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: 2020-09-09 00:00:00.000000000 Z
11
+ date: 2022-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 0.15.2
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
- version: 1.2.0
36
+ version: '2.0'
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 0.15.2
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: 1.2.0
46
+ version: '2.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: faraday_middleware
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +53,7 @@ dependencies:
53
53
  version: 0.9.0
54
54
  - - "<"
55
55
  - !ruby/object:Gem::Version
56
- version: 1.2.0
56
+ version: '2.0'
57
57
  type: :runtime
58
58
  prerelease: false
59
59
  version_requirements: !ruby/object:Gem::Requirement
@@ -63,7 +63,7 @@ dependencies:
63
63
  version: 0.9.0
64
64
  - - "<"
65
65
  - !ruby/object:Gem::Version
66
- version: 1.2.0
66
+ version: '2.0'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: addressable
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -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.1
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