Module: HyperResource::Modules::HTTP
- Included in:
- Link
- Defined in:
- lib/hyper_resource/modules/http.rb
Overview
HyperResource::Modules::HTTP is included by HyperResource::Link. It provides support for GET, POST, PUT, PATCH, and DELETE. Each method returns a new object which is a kind_of HyperResource.
Instance Method Summary collapse
-
#create(*args) ⇒ Object
By default, calls
post
with the given arguments. -
#delete ⇒ Object
DELETEs this resource’s href, and returns the response resource.
-
#delete_response ⇒ Object
DELETEs this resource’s href, and returns the response as a ‘Faraday::Response` object.
-
#get ⇒ Object
Loads and returns the resource pointed to by
href
. -
#get_response ⇒ Object
Performs a GET request on the given link, and returns the response as a ‘Faraday::Response` object.
-
#patch(attrs = nil) ⇒ Object
PATCHes this resource’s changed attributes to this resource’s href, and returns the response resource.
-
#patch_response(attrs = nil) ⇒ Object
PATCHes this resource’s changed attributes to this resource’s href, and returns the response as a ‘Faraday::Response` object.
-
#post(attrs = nil) ⇒ Object
POSTs the given attributes to this resource’s href, and returns the response resource.
-
#post_response(attrs = nil) ⇒ Object
POSTs the given attributes to this resource’s href, and returns the response as a ‘Faraday::Response` object.
-
#put(attrs = nil) ⇒ Object
PUTs this resource’s attributes to this resource’s href, and returns the response resource.
-
#put_response(attrs = nil) ⇒ Object
PUTs this resource’s attributes to this resource’s href, and returns the response as a ‘Faraday::Response` object.
-
#update(*args) ⇒ Object
By default, calls
put
with the given arguments.
Instance Method Details
#create(*args) ⇒ Object
By default, calls post
with the given arguments. Override to change this behavior.
143 144 145 146 147 |
# File 'lib/hyper_resource/modules/http.rb', line 143 def create(*args) _hr_deprecate('HyperResource::Link#create is deprecated. Please use '+ '#post instead.') post(*args) end |
#delete ⇒ Object
DELETEs this resource’s href, and returns the response resource.
214 215 216 |
# File 'lib/hyper_resource/modules/http.rb', line 214 def delete new_resource_from_response(delete_response) end |
#delete_response ⇒ Object
DELETEs this resource’s href, and returns the response as a ‘Faraday::Response` object. Does not parse the response as a `HyperResource` object.
221 222 223 |
# File 'lib/hyper_resource/modules/http.rb', line 221 def delete_response faraday_connection.delete end |
#get ⇒ Object
Loads and returns the resource pointed to by href
. The returned resource will be blessed into its “proper” class, if self.class.namespace != nil.
121 122 123 |
# File 'lib/hyper_resource/modules/http.rb', line 121 def get new_resource_from_response(self.get_response) end |
#get_response ⇒ Object
Performs a GET request on the given link, and returns the response as a ‘Faraday::Response` object. Does not parse the response as a `HyperResource` object.
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/hyper_resource/modules/http.rb', line 128 def get_response ## Adding default_attributes to URL query params is not automatic url = FuzzyURL.new(self.url || '') query_str = url[:query] || '' query_attrs = Hash[ query_str.split('&').map{|p| p.split('=')} ] attrs = (self.resource.default_attributes || {}).merge(query_attrs) attrs_str = attrs.inject([]){|pairs,(k,v)| pairs<<"#{k}=#{v}"}.join('&') if attrs_str != '' url = FuzzyURL.new(url.to_hash.merge(:query => attrs_str)) end faraday_connection.get(url.to_s) end |
#patch(attrs = nil) ⇒ Object
PATCHes this resource’s changed attributes to this resource’s href, and returns the response resource. If attributes are given, patch
uses those instead.
197 198 199 |
# File 'lib/hyper_resource/modules/http.rb', line 197 def patch(attrs=nil) new_resource_from_response(patch_response(attrs)) end |
#patch_response(attrs = nil) ⇒ Object
PATCHes this resource’s changed attributes to this resource’s href, and returns the response as a ‘Faraday::Response` object. Does not parse the response as a `HyperResource` object.
204 205 206 207 208 209 210 211 |
# File 'lib/hyper_resource/modules/http.rb', line 204 def patch_response(attrs=nil) attrs ||= self.resource.attributes.changed_attributes attrs = (self.resource.default_attributes || {}).merge(attrs) response = faraday_connection.patch do |req| req.body = self.resource.adapter.serialize(attrs) end response end |
#post(attrs = nil) ⇒ Object
POSTs the given attributes to this resource’s href, and returns the response resource.
151 152 153 |
# File 'lib/hyper_resource/modules/http.rb', line 151 def post(attrs=nil) new_resource_from_response(post_response(attrs)) end |
#post_response(attrs = nil) ⇒ Object
POSTs the given attributes to this resource’s href, and returns the response as a ‘Faraday::Response` object. Does not parse the response as a `HyperResource` object.
158 159 160 161 162 163 164 165 |
# File 'lib/hyper_resource/modules/http.rb', line 158 def post_response(attrs=nil) attrs ||= self.resource.attributes attrs = (self.resource.default_attributes || {}).merge(attrs) response = faraday_connection.post do |req| req.body = self.resource.adapter.serialize(attrs) end response end |
#put(attrs = nil) ⇒ Object
PUTs this resource’s attributes to this resource’s href, and returns the response resource. If attributes are given, put
uses those instead.
178 179 180 |
# File 'lib/hyper_resource/modules/http.rb', line 178 def put(attrs=nil) new_resource_from_response(put_response(attrs)) end |
#put_response(attrs = nil) ⇒ Object
PUTs this resource’s attributes to this resource’s href, and returns the response as a ‘Faraday::Response` object. Does not parse the response as a `HyperResource` object.
185 186 187 188 189 190 191 192 |
# File 'lib/hyper_resource/modules/http.rb', line 185 def put_response(attrs=nil) attrs ||= self.resource.attributes attrs = (self.resource.default_attributes || {}).merge(attrs) response = faraday_connection.put do |req| req.body = self.resource.adapter.serialize(attrs) end response end |
#update(*args) ⇒ Object
By default, calls put
with the given arguments. Override to change this behavior.
169 170 171 172 173 |
# File 'lib/hyper_resource/modules/http.rb', line 169 def update(*args) _hr_deprecate('HyperResource::Link#update is deprecated. Please use '+ '#put or #patch instead.') put(*args) end |