json_api_resource 2.1.1 → 3.0.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
  SHA1:
3
- metadata.gz: fa7a8562ec59679608e899b037a242b64e114959
4
- data.tar.gz: 7bcdd0f4acacc40908fe073251c4e8c7c15a3667
3
+ metadata.gz: 9b9f3ccf729b8b0bfbc9493560e0395e61c8bdf0
4
+ data.tar.gz: cd57899d7f64b8543eeaa99922d6682efba9ddde
5
5
  SHA512:
6
- metadata.gz: d6d46ca22cfe063c8352fc81d4435a8339784209804583c6cb3b3f46982190f68210a17af91ea151f0263bc5e55510dce386a2a52c4fd1caad4ca4a830a1783f
7
- data.tar.gz: 9d013ba61fcefd2493f97e109413da4371618a18341e9e23903ff4d216e679c47d8cd7ba86842774c5455550163a243143923b32a8dcad090f46ced425e991b8
6
+ metadata.gz: d9ca5e0bead24edeb4a147d9873e3e972fe415db39342b61471fdbaa684b23c84160c0907e0ab11c3b24cc828e3bfe40614bf5f588bcb71aeac905757af2c2f1
7
+ data.tar.gz: d1659f6cf7efdcce11f51b4a2d6235fb65ae237708ce528ed18970168ee070e167a4df3f75d9a2486a23e97839d4edb4002b480d1ac0ac520c2e05970c55631c
data/README.md CHANGED
@@ -140,6 +140,10 @@ NOTE: all properties are optional. If you don't have it defined and it's in the
140
140
 
141
141
  The object will still reply to `id`, `name` and `permissions`, but you won't be able to assume they are there, and they will not appear when you do `ResourceClass.new`.
142
142
 
143
+ ### Error Handling
144
+
145
+ On an unsuccessful call to the server (this means that if you have multiple connections, they will *necessarily **all*** have to fail), errors will be routed through an overridable `handle_failed_request(e)` method. By default it will re-raise the error, but you can handle it any way you want.
146
+
143
147
  ### Other Features
144
148
 
145
149
  #### Caching
@@ -0,0 +1,14 @@
1
+ module JsonApiResource
2
+ module ErrorHandleable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class << self
7
+
8
+ def handle_failed_request( e )
9
+ raise JsonApiResource::Errors::UnsuccessfulRequest.new(class: self, message: e.message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ module JsonApiResource
2
+ module Errors
3
+ class JsonApiResourceError < StandardError
4
+ def initialize(opts = {})
5
+ @klass = opts.fetch :class, JsonApiResource::Resource
6
+ @message = opts.fetch :message, ""
7
+ end
8
+
9
+ def message
10
+ "#{@klass}: #{@message}"
11
+ end
12
+ end
13
+
14
+ class UnsuccessfulRequest < JsonApiResourceError
15
+ end
16
+ end
17
+ end
@@ -26,19 +26,7 @@ module JsonApiResource
26
26
  new(client: result)
27
27
  end
28
28
  rescue Multiconnect::Error::UnsuccessfulRequest => e
29
-
30
- empty_500_set
31
-
32
- end
33
-
34
- def empty_500_set
35
- result = JsonApiClient::ResultSet.new
36
- result.meta = {status: 500}
37
-
38
- result.errors = ActiveModel::Errors.new(result)
39
- result.errors.add("ServerError", "Unable to connect to server or server returned 500")
40
-
41
- result
29
+ handle_failed_request e
42
30
  end
43
31
  end
44
32
 
@@ -6,6 +6,8 @@ module JsonApiResource
6
6
  def initialize(results)
7
7
  self.result = results.first
8
8
 
9
+ return nil unless result
10
+
9
11
  self.result.meta = results.meta
10
12
 
11
13
  self.result.linked_data = results.try(:linked_data)
@@ -23,6 +23,7 @@ module JsonApiResource
23
23
  include JsonApiResource::Executable
24
24
  include JsonApiResource::Conversions
25
25
  include JsonApiResource::Cacheable
26
+ include JsonApiResource::ErrorHandleable
26
27
 
27
28
  attr_accessor :client, :cache_expires_in
28
29
  class_attribute :per_page
@@ -30,7 +31,7 @@ module JsonApiResource
30
31
  delegate :to_json, to: :attributes
31
32
 
32
33
  def initialize(opts={})
33
- raise( JsonApiResourceError, class: self.class, message: "A resource must have a client class" ) unless client_class.present?
34
+ raise( JsonApiResource::Errors::JsonApiResourceError, class: self.class, message: "A resource must have a client class" ) unless client_class.present?
34
35
 
35
36
  self.client = self.client_class.new(self.schema)
36
37
  self.errors = ActiveModel::Errors.new(self)
@@ -87,7 +88,7 @@ module JsonApiResource
87
88
  super
88
89
  end
89
90
  rescue Multiconnect::Error::UnsuccessfulRequest => e
90
- empty_500_set
91
+ handle_failed_request e
91
92
  end
92
93
 
93
94
  def respond_to_missing?(method_name, include_private = false)
@@ -1,3 +1,3 @@
1
1
  module JsonApiResource
2
- VERSION = "2.1.1"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -5,9 +5,10 @@ module JsonApiResource
5
5
  autoload :Clientable, 'json_api_resource/clientable'
6
6
  autoload :Connections, 'json_api_resource/connections'
7
7
  autoload :Conversions, 'json_api_resource/conversions'
8
+ autoload :ErrorHandleable, 'json_api_resource/error_handleable'
8
9
  autoload :ErrorNotifier, 'json_api_resource/error_notifier'
9
10
  autoload :Handlers, 'json_api_resource/handlers'
10
- autoload :JsonApiResourceError, 'json_api_resource/json_api_resource_error'
11
+ autoload :Errors, 'json_api_resource/errors'
11
12
  autoload :Queryable, 'json_api_resource/queryable'
12
13
  autoload :Resource, 'json_api_resource/resource'
13
14
  autoload :Executable, 'json_api_resource/executable'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Sislow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-19 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_api_client
@@ -109,12 +109,13 @@ files:
109
109
  - lib/json_api_resource/connections.rb
110
110
  - lib/json_api_resource/connections/server_connection.rb
111
111
  - lib/json_api_resource/conversions.rb
112
+ - lib/json_api_resource/error_handleable.rb
112
113
  - lib/json_api_resource/error_notifier.rb
113
114
  - lib/json_api_resource/error_notifier/base.rb
115
+ - lib/json_api_resource/errors.rb
114
116
  - lib/json_api_resource/executable.rb
115
117
  - lib/json_api_resource/handlers.rb
116
118
  - lib/json_api_resource/handlers/find_handler.rb
117
- - lib/json_api_resource/json_api_resource_error.rb
118
119
  - lib/json_api_resource/queryable.rb
119
120
  - lib/json_api_resource/resource.rb
120
121
  - lib/json_api_resource/schemable.rb
@@ -1,12 +0,0 @@
1
- module JsonApiResource
2
- class JsonApiResourceError < StandardError
3
- def initialize(opts = {})
4
- @klass = opts.fetch :class, JsonApiResource::Resource
5
- @message = opts.fetch :message, ""
6
- end
7
-
8
- def message
9
- "#{@klass}: #{@message}"
10
- end
11
- end
12
- end