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 +4 -4
- data/lib/hyper_resource/adapter/hal_json.rb +6 -4
- data/lib/hyper_resource/exceptions.rb +13 -9
- data/lib/hyper_resource/link.rb +3 -1
- data/lib/hyper_resource/modules/http.rb +17 -5
- data/lib/hyper_resource/version.rb +1 -1
- data/lib/hyper_resource.rb +19 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 294b14e1ecc640c411926b683c3b47755e00701f
|
4
|
+
data.tar.gz: 3a7ed8ec37100074abba126ec84509b61134d862
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
87
|
-
|
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
|
-
|
92
|
+
rsrc.attributes.create_methods!
|
91
93
|
end
|
92
94
|
|
93
95
|
end
|
@@ -1,12 +1,16 @@
|
|
1
|
-
class HyperResource
|
2
|
-
|
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
|
-
|
5
|
-
|
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
|
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
|
-
|
data/lib/hyper_resource/link.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
34
|
-
|
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
|
52
|
+
raise HyperResource::ClientError.new(status.to_s,
|
53
|
+
:response => self.response)
|
44
54
|
elsif status / 100 == 5
|
45
|
-
raise HyperResource::ServerError
|
55
|
+
raise HyperResource::ServerError.new(status.to_s,
|
56
|
+
:response => self.response)
|
46
57
|
else ## 1xx? really?
|
47
|
-
raise HyperResource::ResponseError
|
58
|
+
raise HyperResource::ResponseError.new("Got status #{status}, wtf?",
|
59
|
+
:response => self.response)
|
48
60
|
end
|
49
61
|
end
|
50
62
|
|
data/lib/hyper_resource.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|