hyperresource 0.1.9.3 → 0.1.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hyper_resource.rb +8 -4
- data/lib/hyper_resource/adapter/hal_json.rb +3 -2
- data/lib/hyper_resource/attributes.rb +6 -0
- data/lib/hyper_resource/exceptions.rb +14 -2
- data/lib/hyper_resource/modules/http.rb +38 -9
- data/lib/hyper_resource/objects.rb +2 -2
- data/lib/hyper_resource/version.rb +2 -2
- metadata +22 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbbf6ab86efd5ba75dc1c7831f92336b7cfbee64
|
4
|
+
data.tar.gz: 72b1877082068c781f3730fc95977cd3b09f60bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1750d932e5ee8499dd146e52a31090d02809a68b1141d5aed3aaaede921366a33f4a81634b98d960ce083cddf7c28331c873b6386eb64f3681e6b5b4c1c217b6
|
7
|
+
data.tar.gz: 5399981c4f2c35b440f4029168392753371244580c4804ba84a044edee22b878a072f38448d0c0b6c6d49436a74bb9cb558f906e05af312cc4eb68fdda1621c7
|
data/lib/hyper_resource.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
this_dir = File.dirname(
|
1
|
+
this_dir = File.dirname(__FILE__)
|
2
2
|
Dir.glob(this_dir + '/hyper_resource/**/*.rb') {|f| require f}
|
3
3
|
|
4
|
+
if RUBY_VERSION[0..2] == '1.8'
|
5
|
+
require 'rubygems'
|
6
|
+
end
|
7
|
+
|
4
8
|
require 'pp'
|
5
9
|
|
6
10
|
class HyperResource
|
@@ -149,7 +153,7 @@ public
|
|
149
153
|
self.get unless self.loaded
|
150
154
|
|
151
155
|
method = method.to_s
|
152
|
-
if method[-1] == '='
|
156
|
+
if method[-1,1] == '='
|
153
157
|
return attributes[method[0..-2]] = args.first if attributes[method[0..-2]]
|
154
158
|
else
|
155
159
|
return attributes[method] if attributes && attributes[method]
|
@@ -249,8 +253,8 @@ public
|
|
249
253
|
def self._get_response_data_type(response) # :nodoc:
|
250
254
|
return nil unless response
|
251
255
|
return nil unless content_type = response['content-type']
|
252
|
-
return nil unless m=content_type.match(/;\s* type=(
|
253
|
-
m[
|
256
|
+
return nil unless m=content_type.match(/;\s* type=([0-9A-Za-z:]+)/x)
|
257
|
+
m[1][0,1].upcase + m[1][1..-1]
|
254
258
|
end
|
255
259
|
|
256
260
|
def _get_response_data_type # :nodoc:
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'rubygems' if RUBY_VERSION[0..2] == '1.8'
|
1
2
|
require 'json'
|
2
3
|
|
3
4
|
class HyperResource
|
@@ -40,12 +41,12 @@ class HyperResource
|
|
40
41
|
|
41
42
|
resp['_embedded'].each do |name, collection|
|
42
43
|
if collection.is_a? Hash
|
43
|
-
r = rc.new
|
44
|
+
r = rc.new(:root => rsrc.root, :namespace => rsrc.namespace)
|
44
45
|
r.response_object = collection
|
45
46
|
objs[name] = apply(collection, r)
|
46
47
|
else
|
47
48
|
objs[name] = collection.map do |obj|
|
48
|
-
r = rc.new
|
49
|
+
r = rc.new(:root => rsrc.root, :namespace => rsrc.namespace)
|
49
50
|
r.response_object = obj
|
50
51
|
apply(obj, r)
|
51
52
|
end
|
@@ -52,6 +52,12 @@ class HyperResource
|
|
52
52
|
return @_hr_changed.keys.count > 0
|
53
53
|
end
|
54
54
|
|
55
|
+
## Returns a hash of the attributes and values which have been changed
|
56
|
+
## since creation time.
|
57
|
+
def changed_attributes
|
58
|
+
@_hr_changed.select{|k,v| v}.keys.inject({}) {|h,k| h[k]=self[k]; h}
|
59
|
+
end
|
60
|
+
|
55
61
|
def []=(attr, value) # :nodoc:
|
56
62
|
_hr_mark_changed(attr)
|
57
63
|
super(attr.to_s, value)
|
@@ -1,11 +1,23 @@
|
|
1
1
|
class HyperResource
|
2
2
|
class Exception < ::Exception
|
3
|
-
attr_accessor :response
|
4
|
-
attr_accessor :
|
3
|
+
attr_accessor :response # Response body which led to this
|
4
|
+
attr_accessor :response_object # Response object which led to this
|
5
|
+
attr_accessor :cause # Internal exception which led to this
|
5
6
|
|
6
7
|
def initialize(message, opts={})
|
7
8
|
self.response = opts[:response]
|
9
|
+
self.response_object = opts[:response_object]
|
8
10
|
self.cause = opts[:cause]
|
11
|
+
|
12
|
+
## Try to help out with the message
|
13
|
+
if self.response_object
|
14
|
+
if error = self.response_object['error']
|
15
|
+
message = "#{message} (#{error})"
|
16
|
+
end
|
17
|
+
elsif self.response
|
18
|
+
message = "#{message} (\"#{self.response.inspect}\")"
|
19
|
+
end
|
20
|
+
|
9
21
|
super(message)
|
10
22
|
end
|
11
23
|
end
|
@@ -2,8 +2,8 @@ require 'faraday'
|
|
2
2
|
require 'uri'
|
3
3
|
require 'json'
|
4
4
|
|
5
|
-
|
6
|
-
module HTTP
|
5
|
+
class HyperResource
|
6
|
+
module Modules; module HTTP
|
7
7
|
|
8
8
|
## Loads and returns the resource pointed to by +href+. The returned
|
9
9
|
## resource will be blessed into its "proper" class, if
|
@@ -13,9 +13,33 @@ module HyperResource::Modules
|
|
13
13
|
finish_up
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
def create(*args); post(*args) end
|
17
|
+
def post(params=nil)
|
18
|
+
params ||= self.attributes
|
19
|
+
self.response = faraday_connection.post do |req|
|
20
|
+
req.body = adapter.serialize(params)
|
21
|
+
end
|
22
|
+
finish_up
|
23
|
+
end
|
24
|
+
|
25
|
+
def update(*args); put(*args) end
|
26
|
+
def put(params=nil)
|
27
|
+
params ||= self.attributes.changed_attributes
|
28
|
+
self.response = faraday_connection.put do |req|
|
29
|
+
req.body = adapter.serialize(params)
|
30
|
+
end
|
31
|
+
finish_up
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete
|
35
|
+
self.response = faraday_connection.delete
|
36
|
+
finish_up
|
37
|
+
end
|
38
|
+
|
39
|
+
## Returns a raw Faraday connection to this resource's URL, with proper
|
40
|
+
## headers (including auth).
|
17
41
|
def faraday_connection(url=nil)
|
18
|
-
url ||= self.root
|
42
|
+
url ||= URI.join(self.root, self.href)
|
19
43
|
key = "faraday_connection_#{url}"
|
20
44
|
return Thread.current[key] if Thread.current[key]
|
21
45
|
|
@@ -33,7 +57,7 @@ module HyperResource::Modules
|
|
33
57
|
def finish_up
|
34
58
|
begin
|
35
59
|
self.response_object = self.adapter.deserialize(self.response.body)
|
36
|
-
rescue
|
60
|
+
rescue StandardError => e
|
37
61
|
raise HyperResource::ResponseError.new(
|
38
62
|
"Error when deserializing response body",
|
39
63
|
:response => self.response,
|
@@ -51,16 +75,21 @@ module HyperResource::Modules
|
|
51
75
|
## TODO redirect logic?
|
52
76
|
elsif status / 100 == 4
|
53
77
|
raise HyperResource::ClientError.new(status.to_s,
|
54
|
-
:response => self.response
|
78
|
+
:response => self.response,
|
79
|
+
:response_object => self.response_object)
|
55
80
|
elsif status / 100 == 5
|
56
81
|
raise HyperResource::ServerError.new(status.to_s,
|
57
|
-
:response => self.response
|
82
|
+
:response => self.response,
|
83
|
+
:response_object => self.response_object)
|
84
|
+
|
58
85
|
else ## 1xx? really?
|
59
86
|
raise HyperResource::ResponseError.new("Got status #{status}, wtf?",
|
60
|
-
:response => self.response
|
87
|
+
:response => self.response,
|
88
|
+
:response_object => self.response_object)
|
89
|
+
|
61
90
|
end
|
62
91
|
end
|
63
92
|
|
64
|
-
end
|
93
|
+
end end
|
65
94
|
end
|
66
95
|
|
@@ -32,12 +32,12 @@ class HyperResource
|
|
32
32
|
## Returns the first item in the first collection in +self+.
|
33
33
|
alias_method :first_orig, :first
|
34
34
|
def first
|
35
|
-
self.first_orig[1][0]
|
35
|
+
self.first_orig[1][0]
|
36
36
|
end
|
37
37
|
|
38
38
|
## Returns the ith item in the first collection in +self+.
|
39
39
|
def ith(i)
|
40
|
-
self.first_orig[1][i]
|
40
|
+
self.first_orig[1][i]
|
41
41
|
end
|
42
42
|
|
43
43
|
def []=(attr, value) # :nodoc:
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Gamache
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uri_template
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.8.6
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,17 +118,17 @@ executables: []
|
|
104
118
|
extensions: []
|
105
119
|
extra_rdoc_files: []
|
106
120
|
files:
|
107
|
-
- lib/hyper_resource/adapter/hal_json.rb
|
108
|
-
- lib/hyper_resource/adapter.rb
|
109
|
-
- lib/hyper_resource/attributes.rb
|
110
|
-
- lib/hyper_resource/exceptions.rb
|
111
|
-
- lib/hyper_resource/link.rb
|
112
121
|
- lib/hyper_resource/links.rb
|
122
|
+
- lib/hyper_resource/adapter.rb
|
113
123
|
- lib/hyper_resource/modules/http.rb
|
114
124
|
- lib/hyper_resource/modules/utils.rb
|
115
125
|
- lib/hyper_resource/objects.rb
|
116
126
|
- lib/hyper_resource/response.rb
|
127
|
+
- lib/hyper_resource/adapter/hal_json.rb
|
117
128
|
- lib/hyper_resource/version.rb
|
129
|
+
- lib/hyper_resource/link.rb
|
130
|
+
- lib/hyper_resource/exceptions.rb
|
131
|
+
- lib/hyper_resource/attributes.rb
|
118
132
|
- lib/hyper_resource.rb
|
119
133
|
- lib/hyperresource.rb
|
120
134
|
homepage: https://github.com/gamache/hyperresource
|
@@ -137,9 +151,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
151
|
version: '0'
|
138
152
|
requirements: []
|
139
153
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.0.
|
154
|
+
rubygems_version: 2.0.0.rc.2
|
141
155
|
signing_key:
|
142
156
|
specification_version: 4
|
143
157
|
summary: Extensible hypermedia client for Ruby
|
144
158
|
test_files: []
|
145
|
-
has_rdoc: true
|