hyperresource 0.9.0 → 0.9.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 524a169b824f7c61e5aa5c05109415206095ad99
4
- data.tar.gz: 70538dc63539ed231dfb71d5cedaccedaf081bda
3
+ metadata.gz: af24d48bc701443215065bd5c689530014860cfd
4
+ data.tar.gz: 47b4d6f60ae59bf154ca9395dc42404f1aea0124
5
5
  SHA512:
6
- metadata.gz: 875712e54fc2b58369277dc07d4b2418b8726aeef5dac678584ba0cbc5d617ab6a626d57365381ce3dd3ec3924de0b714060a6f6472e61396258c4cb40439fd8
7
- data.tar.gz: 97a73477ec2d1f378b0ae5904f0c09228dd581b7f8d21289e07311c012f3b8ee2ee2f3fef08a04d15e7ba9d2af4b09ab1d19b5f489e1c7e17912dc51d6abb124
6
+ metadata.gz: 599e643a3ff95f29a9616b0465228a8e233748c91abc1234ceb67e467f3418f82ae5a003ba63f6805807af73b528e0e5142bfacea5f9f298fb4b799f6832ec1d
7
+ data.tar.gz: cb51756aef3602776930786acf55889163ffd6e7a0cc03ff017822909b6a21617bc486673a39beb2b6221619dff80ac250a9efbf6070ac97a0e3476cfca67847
@@ -54,6 +54,7 @@ class HyperResource
54
54
  self.templated = !!link_spec['templated']
55
55
  self.params = link_spec['params'] || {}
56
56
  self.default_method = link_spec['method'] || 'get'
57
+ @headers = link_spec['headers'] || {}
57
58
  end
58
59
 
59
60
  ## Returns this link's href, applying any URI template params.
@@ -85,7 +86,28 @@ class HyperResource
85
86
  'name' => self.name,
86
87
  'templated' => self.templated,
87
88
  'params' => self.params.merge(params),
88
- 'method' => self.default_method)
89
+ 'method' => self.default_method,
90
+ 'headers' => @headers)
91
+ end
92
+
93
+ ## When called with a hash, returns a new scope with the given headers;
94
+ ## that is, returns a copy of itself with the given headers applied.
95
+ ## These headers will be merged with `resource.headers` at request time.
96
+ ##
97
+ ## When called with no arguments, returns the headers for this link.
98
+ def headers(*args)
99
+ if args.count == 0
100
+ @headers
101
+ else
102
+ self.class.new(self.resource,
103
+ 'href' => self.base_href,
104
+ 'name' => self.name,
105
+ 'templated' => self.templated,
106
+ 'params' => self.params,
107
+ 'method' => self.default_method,
108
+ 'headers' => @headers.merge(args[0]))
109
+
110
+ end
89
111
  end
90
112
 
91
113
  ## Unrecognized methods invoke an implicit load of the resource pointed
@@ -22,6 +22,12 @@ class HyperResource
22
22
  to_link.get
23
23
  end
24
24
 
25
+ ## Performs a GET request to this resource's URL, and returns a
26
+ ## `Faraday::Response` object representing the response.
27
+ def get_response
28
+ to_link.get_response
29
+ end
30
+
25
31
  ## Performs a POST request to this resource's URL, sending all of
26
32
  ## `attributes` as a request body unless an `attrs` Hash is given.
27
33
  ## Returns a new resource representing the response.
@@ -29,6 +35,13 @@ class HyperResource
29
35
  to_link.post(attrs)
30
36
  end
31
37
 
38
+ ## Performs a POST request to this resource's URL, sending all of
39
+ ## `attributes` as a request body unless an `attrs` Hash is given.
40
+ ## Returns a `Faraday::Response` object representing the response.
41
+ def post_response(attrs=nil)
42
+ to_link.post_response(attrs)
43
+ end
44
+
32
45
  ## Performs a PUT request to this resource's URL, sending all of
33
46
  ## `attributes` as a request body unless an `attrs` Hash is given.
34
47
  ## Returns a new resource representing the response.
@@ -36,6 +49,13 @@ class HyperResource
36
49
  to_link.put(*args)
37
50
  end
38
51
 
52
+ ## Performs a PUT request to this resource's URL, sending all of
53
+ ## `attributes` as a request body unless an `attrs` Hash is given.
54
+ ## Returns a `Faraday::Response` object representing the response.
55
+ def put_response(*args)
56
+ to_link.put_response(*args)
57
+ end
58
+
39
59
  ## Performs a PATCH request to this resource's URL, sending
40
60
  ## `attributes.changed_attributes` as a request body
41
61
  ## unless an `attrs` Hash is given. Returns a new resource
@@ -44,10 +64,24 @@ class HyperResource
44
64
  self.to_link.patch(*args)
45
65
  end
46
66
 
67
+ ## Performs a PATCH request to this resource's URL, sending
68
+ ## `attributes.changed_attributes` as a request body
69
+ ## unless an `attrs` Hash is given.
70
+ ## Returns a `Faraday::Response` object representing the response.
71
+ def patch_response(*args)
72
+ self.to_link.patch_response(*args)
73
+ end
74
+
47
75
  ## Performs a DELETE request to this resource's URL. Returns a new
48
76
  ## resource representing the response.
49
- def delete(*args)
50
- to_link.delete(*args)
77
+ def delete
78
+ to_link.delete
79
+ end
80
+
81
+ ## Performs a DELETE request to this resource's URL.
82
+ ## Returns a `Faraday::Response` object representing the response.
83
+ def delete_response
84
+ to_link.delete_response
51
85
  end
52
86
 
53
87
  ## Creates a Link representing this resource. Used for HTTP delegation.
@@ -85,6 +119,13 @@ class HyperResource
85
119
  ## resource will be blessed into its "proper" class, if
86
120
  ## +self.class.namespace != nil+.
87
121
  def get
122
+ new_resource_from_response(self.get_response)
123
+ end
124
+
125
+ ## Performs a GET request on the given link, and returns the
126
+ ## response as a `Faraday::Response` object.
127
+ ## Does not parse the response as a `HyperResource` object.
128
+ def get_response
88
129
  ## Adding default_attributes to URL query params is not automatic
89
130
  url = FuzzyURL.new(self.url || '')
90
131
  query_str = url[:query] || ''
@@ -94,8 +135,7 @@ class HyperResource
94
135
  if attrs_str != ''
95
136
  url = FuzzyURL.new(url.to_hash.merge(:query => attrs_str))
96
137
  end
97
- response = faraday_connection.get(url.to_s)
98
- new_resource_from_response(response)
138
+ faraday_connection.get(url.to_s)
99
139
  end
100
140
 
101
141
  ## By default, calls +post+ with the given arguments. Override to
@@ -109,15 +149,22 @@ class HyperResource
109
149
  ## POSTs the given attributes to this resource's href, and returns
110
150
  ## the response resource.
111
151
  def post(attrs=nil)
152
+ new_resource_from_response(post_response(attrs))
153
+ end
154
+
155
+ ## POSTs the given attributes to this resource's href, and returns the
156
+ ## response as a `Faraday::Response` object.
157
+ ## Does not parse the response as a `HyperResource` object.
158
+ def post_response(attrs=nil)
112
159
  attrs ||= self.resource.attributes
113
160
  attrs = (self.resource.default_attributes || {}).merge(attrs)
114
161
  response = faraday_connection.post do |req|
115
162
  req.body = self.resource.adapter.serialize(attrs)
116
163
  end
117
- new_resource_from_response(response)
164
+ response
118
165
  end
119
166
 
120
- ## By default, calls +puwt+ with the given arguments. Override to
167
+ ## By default, calls +put+ with the given arguments. Override to
121
168
  ## change this behavior.
122
169
  def update(*args)
123
170
  _hr_deprecate('HyperResource::Link#update is deprecated. Please use '+
@@ -129,30 +176,50 @@ class HyperResource
129
176
  ## the response resource. If attributes are given, +put+ uses those
130
177
  ## instead.
131
178
  def put(attrs=nil)
179
+ new_resource_from_response(put_response(attrs))
180
+ end
181
+
182
+ ## PUTs this resource's attributes to this resource's href, and returns
183
+ ## the response as a `Faraday::Response` object.
184
+ ## Does not parse the response as a `HyperResource` object.
185
+ def put_response(attrs=nil)
132
186
  attrs ||= self.resource.attributes
133
187
  attrs = (self.resource.default_attributes || {}).merge(attrs)
134
188
  response = faraday_connection.put do |req|
135
189
  req.body = self.resource.adapter.serialize(attrs)
136
190
  end
137
- new_resource_from_response(response)
191
+ response
138
192
  end
139
193
 
140
194
  ## PATCHes this resource's changed attributes to this resource's href,
141
195
  ## and returns the response resource. If attributes are given, +patch+
142
196
  ## uses those instead.
143
197
  def patch(attrs=nil)
198
+ new_resource_from_response(patch_response(attrs))
199
+ end
200
+
201
+ ## PATCHes this resource's changed attributes to this resource's href,
202
+ ## and returns the response as a `Faraday::Response` object.
203
+ ## Does not parse the response as a `HyperResource` object.
204
+ def patch_response(attrs=nil)
144
205
  attrs ||= self.resource.attributes.changed_attributes
145
206
  attrs = (self.resource.default_attributes || {}).merge(attrs)
146
207
  response = faraday_connection.patch do |req|
147
208
  req.body = self.resource.adapter.serialize(attrs)
148
209
  end
149
- new_resource_from_response(response)
210
+ response
150
211
  end
151
212
 
152
213
  ## DELETEs this resource's href, and returns the response resource.
153
214
  def delete
154
- response = faraday_connection.delete
155
- new_resource_from_response(response)
215
+ new_resource_from_response(delete_response)
216
+ end
217
+
218
+ ## DELETEs this resource's href, and returns the response as a
219
+ ## `Faraday::Response` object.
220
+ ## Does not parse the response as a `HyperResource` object.
221
+ def delete_response
222
+ faraday_connection.delete
156
223
  end
157
224
 
158
225
  private
@@ -162,7 +229,7 @@ class HyperResource
162
229
  def faraday_connection(url=nil)
163
230
  rsrc = self.resource
164
231
  url ||= self.url
165
- headers = rsrc.headers_for_url(url) || {}
232
+ headers = (rsrc.headers_for_url(url) || {}).merge(self.headers)
166
233
  auth = rsrc.auth_for_url(url) || {}
167
234
 
168
235
  key = ::Digest::MD5.hexdigest({
@@ -1,5 +1,5 @@
1
1
  class HyperResource
2
- VERSION = '0.9.0'
3
- VERSION_DATE = '2014-05-29'
2
+ VERSION = '0.9.1'
3
+ VERSION_DATE = '2015-03-25'
4
4
  end
5
5
 
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperresource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Gamache
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-29 00:00:00.000000000 Z
11
+ date: 2015-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uri_template
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.5.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.5.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.8.6
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.8.6
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
@@ -70,56 +70,56 @@ dependencies:
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 10.0.4
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 10.0.4
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: minitest
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: 4.7.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: 4.7.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mocha
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: 0.13.3
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: 0.13.3
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: sinatra
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>='
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: 1.4.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: 1.4.0
125
125
  description: |2
@@ -158,19 +158,18 @@ require_paths:
158
158
  - lib
159
159
  required_ruby_version: !ruby/object:Gem::Requirement
160
160
  requirements:
161
- - - '>='
161
+ - - ">="
162
162
  - !ruby/object:Gem::Version
163
163
  version: 1.8.7
164
164
  required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  requirements:
166
- - - '>='
166
+ - - ">="
167
167
  - !ruby/object:Gem::Version
168
168
  version: '0'
169
169
  requirements: []
170
170
  rubyforge_project:
171
- rubygems_version: 2.2.0
171
+ rubygems_version: 2.2.2
172
172
  signing_key:
173
173
  specification_version: 4
174
174
  summary: Extensible hypermedia client for Ruby
175
175
  test_files: []
176
- has_rdoc: true