hyperresource 0.1.9 → 0.1.9.2

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: 4befe203aa8c4fc93c4ba6d09c03bcc9d76a2183
4
- data.tar.gz: 1deed89d174bd9ac761763a731d69c06b70eb318
3
+ metadata.gz: 294b14e1ecc640c411926b683c3b47755e00701f
4
+ data.tar.gz: 3a7ed8ec37100074abba126ec84509b61134d862
5
5
  SHA512:
6
- metadata.gz: 4122f5fb687c63252bb05724cc8477c4ae64f3f72a2340af09e21d070066165591292ad6756c16c27381eb6aff43c6ea294ae88d482f5e8520fc9c4adb4adfef
7
- data.tar.gz: e5170d246d1642ce97b5cdb312d47d1a3af99c462ca16116a146af39d1dd54e95e91313226d79853a4d115cf102d1fa55183d8e2a567cf48ae2f66575ae3fa44
6
+ metadata.gz: 792029a5bc68c4a83d6699b6142318dd96ede36749ae080e4c5ec797aa4a1543565cf910eca7fd623489897b54a109d479112227395c8b8b778d568944525f3d
7
+ data.tar.gz: 46d604b2574111678c0a58f937c765e452e99d2d35b0ec07ae62ac184ffd6246dc381ebac2a4f24cd94e8bd7172fdacb14d7678f3bd9f37992dbee03307dff93
@@ -81,13 +81,15 @@ class HyperResource
81
81
 
82
82
  def apply_attributes(resp, rsrc)
83
83
  rsrc.attributes = rsrc.get_response_class::Attributes.new(rsrc)
84
- attrs = rsrc.attributes
85
84
 
86
- (resp.keys - ['_links', '_embedded']).map(&:to_s).each do |attr|
87
- attrs[attr] = resp[attr]
85
+ given_attrs = resp.reject{|k,v| %w(_links _embedded).include?(k)}
86
+ filtered_attrs = rsrc.incoming_body_filter(given_attrs)
87
+
88
+ filtered_attrs.keys.each do |attr|
89
+ rsrc.attributes[attr] = filtered_attrs[attr]
88
90
  end
89
91
 
90
- attrs.create_methods!
92
+ rsrc.attributes.create_methods!
91
93
  end
92
94
 
93
95
  end
@@ -1,12 +1,16 @@
1
- class HyperResource::Exception < Exception
2
- end
1
+ class HyperResource
2
+ class Exception < ::Exception
3
+ attr_accessor :response # response object which led to this exception
4
+ attr_accessor :cause # internal exception which led to this exception
3
5
 
4
- class HyperResource::ResponseError < HyperResource::Exception
5
- end
6
+ def initialize(message, opts={})
7
+ self.response = opts[:response]
8
+ self.cause = opts[:cause]
9
+ super(message)
10
+ end
11
+ end
6
12
 
7
- class HyperResource::ClientError < HyperResource::ResponseError
13
+ class ResponseError < Exception; end
14
+ class ClientError < Exception; end
15
+ class ServerError < Exception; end
8
16
  end
9
-
10
- class HyperResource::ServerError < HyperResource::ResponseError
11
- end
12
-
@@ -20,7 +20,8 @@ class HyperResource::Link
20
20
  ## Returns this link's href, applying any URI template params.
21
21
  def href
22
22
  if self.templated?
23
- URITemplate.new(self.base_href).expand(params)
23
+ filtered_params = self.parent_resource.outgoing_uri_filter(params)
24
+ URITemplate.new(self.base_href).expand(filtered_params)
24
25
  else
25
26
  self.base_href
26
27
  end
@@ -29,6 +30,7 @@ class HyperResource::Link
29
30
  ## Returns a new scope with the given params; that is, returns a copy of
30
31
  ## itself with the given params applied.
31
32
  def where(params)
33
+ params = Hash[ params.map{|(k,v)| [k.to_s, v]} ]
32
34
  self.class.new(self.parent_resource,
33
35
  'href' => self.base_href,
34
36
  'name' => self.name,
@@ -30,9 +30,18 @@ module HyperResource::Modules::HTTP
30
30
  private
31
31
 
32
32
  def finish_up
33
- self.loaded = true
34
- self.response_object = self.adapter.deserialize(self.response.body)
33
+ begin
34
+ self.response_object = self.adapter.deserialize(self.response.body)
35
+ rescue Exception => e
36
+ raise HyperResource::ResponseError.new(
37
+ "Error when deserializing response body",
38
+ :response => self.response,
39
+ :cause => e
40
+ )
41
+ end
42
+
35
43
  self.adapter.apply(self.response_object, self)
44
+ self.loaded = true
36
45
 
37
46
  status = self.response.status
38
47
  if status / 100 == 2
@@ -40,11 +49,14 @@ private
40
49
  elsif status / 100 == 3
41
50
  ## TODO redirect logic?
42
51
  elsif status / 100 == 4
43
- raise HyperResource::ClientError, status.to_s
52
+ raise HyperResource::ClientError.new(status.to_s,
53
+ :response => self.response)
44
54
  elsif status / 100 == 5
45
- raise HyperResource::ServerError, status.to_s
55
+ raise HyperResource::ServerError.new(status.to_s,
56
+ :response => self.response)
46
57
  else ## 1xx? really?
47
- raise HyperResource::ResponseError, "Got status #{status}, wtf?"
58
+ raise HyperResource::ResponseError.new("Got status #{status}, wtf?",
59
+ :response => self.response)
48
60
  end
49
61
  end
50
62
 
@@ -1,4 +1,4 @@
1
1
  class HyperResource
2
- VERSION = '0.1.9'
2
+ VERSION = '0.1.9.2'
3
3
  VERSION_DATE = '2013-09-27'
4
4
  end
@@ -15,7 +15,6 @@ require 'hyper_resource/adapter/hal_json'
15
15
  require 'pp'
16
16
 
17
17
  ## TODO:
18
- ## incoming_filter, outgoing_filter
19
18
  ## as_json, to_json (in adapter?)
20
19
  ## save, update, create, delete
21
20
 
@@ -148,15 +147,31 @@ public
148
147
  response_class.new(self)
149
148
  end
150
149
 
151
- def incoming_filter(attr_hash)
150
+
151
+ ## +incoming_body_filter+ filters a hash of attribute keys and values
152
+ ## on their way from a response body to a HyperResource. Override this
153
+ ## in a subclass of HyperResource to implement filters on incoming data.
154
+ def incoming_body_filter(attr_hash)
152
155
  attr_hash
153
156
  end
154
157
 
155
- def outgoing_filter(attr_hash)
158
+ ## +outgoing_body_filter+ filters a hash of attribute keys and values
159
+ ## on their way from a HyperResource to a request body. Override this
160
+ ## in a subclass of HyperResource to implement filters on outgoing data.
161
+ def outgoing_body_filter(attr_hash)
156
162
  attr_hash
157
163
  end
158
164
 
159
- def get_response_class
165
+ ## +outgoing_uri_filter+ filters a hash of attribute keys and values
166
+ ## on their way from a HyperResource to a URL. Override this
167
+ ## in a subclass of HyperResource to implement filters on outgoing URI
168
+ ## parameters.
169
+ def outgoing_uri_filter(attr_hash)
170
+ attr_hash
171
+ end
172
+
173
+
174
+ def get_response_class # :nodoc:
160
175
  self.namespace ||= self.class.to_s unless self.class.to_s=='HyperResource'
161
176
  self.class.get_response_class(self.response, self.namespace)
162
177
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperresource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Gamache