Module: HyperResource::Modules::HTTP
- Included in:
- HyperResource
- Defined in:
- lib/hyper_resource/modules/http.rb
Instance Method Summary collapse
-
#create(*args) ⇒ Object
By default, calls
postwith the given arguments. -
#delete ⇒ Object
DELETEs this resource’s href, and returns the response resource.
-
#faraday_connection(url = nil) ⇒ Object
Returns a raw Faraday connection to this resource’s URL, with proper headers (including auth).
-
#get ⇒ Object
Loads and returns the resource pointed to by
href. -
#patch(attrs = nil) ⇒ Object
PATCHes this resource’s changed attributes to this resource’s href, and returns the response resource.
-
#post(attrs) ⇒ Object
POSTs the given attributes to this resource’s href, and returns the response resource.
-
#put(attrs = nil) ⇒ Object
PUTs this resource’s attributes to this resource’s href, and returns the response resource.
-
#update(*args) ⇒ Object
By default, calls
putwith the given arguments.
Instance Method Details
#create(*args) ⇒ Object
By default, calls post with the given arguments. Override to change this behavior.
19 20 21 |
# File 'lib/hyper_resource/modules/http.rb', line 19 def create(*args) post(*args) end |
#delete ⇒ Object
DELETEs this resource’s href, and returns the response resource.
61 62 63 64 |
# File 'lib/hyper_resource/modules/http.rb', line 61 def delete self.response = faraday_connection.delete finish_up end |
#faraday_connection(url = nil) ⇒ Object
Returns a raw Faraday connection to this resource’s URL, with proper headers (including auth). Threadsafe.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/hyper_resource/modules/http.rb', line 68 def faraday_connection(url=nil) url ||= URI.join(self.root, self.href) key = "faraday_connection_#{url}" return Thread.current[key] if Thread.current[key] fc = Faraday.new(:url => url) fc.headers.merge!('User-Agent' => "HyperResource #{HyperResource::VERSION}") fc.headers.merge!(self.headers || {}) if ba=self.auth[:basic] fc.basic_auth(*ba) end Thread.current[key] = fc 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.
12 13 14 15 |
# File 'lib/hyper_resource/modules/http.rb', line 12 def get self.response = faraday_connection.get(self.href || '') finish_up 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.
52 53 54 55 56 57 58 |
# File 'lib/hyper_resource/modules/http.rb', line 52 def patch(attrs=nil) attrs ||= self.attributes.changed_attributes self.response = faraday_connection.patch do |req| req.body = adapter.serialize(attrs) end finish_up end |
#post(attrs) ⇒ Object
POSTs the given attributes to this resource’s href, and returns the response resource.
25 26 27 28 29 30 |
# File 'lib/hyper_resource/modules/http.rb', line 25 def post(attrs) self.response = faraday_connection.post do |req| req.body = adapter.serialize(attrs) end finish_up 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.
41 42 43 44 45 46 47 |
# File 'lib/hyper_resource/modules/http.rb', line 41 def put(attrs=nil) attrs ||= self.attributes self.response = faraday_connection.put do |req| req.body = adapter.serialize(attrs) end finish_up end |
#update(*args) ⇒ Object
By default, calls put with the given arguments. Override to change this behavior.
34 35 36 |
# File 'lib/hyper_resource/modules/http.rb', line 34 def update(*args) put(*args) end |