lib/hyper_resource/modules/http.rb in hyperresource-0.1.9.5 vs lib/hyper_resource/modules/http.rb in hyperresource-0.2.0
- old
+ new
@@ -1,95 +1,123 @@
require 'faraday'
require 'uri'
require 'json'
class HyperResource
- module Modules; module HTTP
+ module Modules
+ module HTTP
- ## Loads and returns the resource pointed to by +href+. The returned
- ## resource will be blessed into its "proper" class, if
- ## +self.class.namespace != nil+.
- def get
- self.response = faraday_connection.get(self.href || '')
- finish_up
- end
+ ## Loads and returns the resource pointed to by +href+. The returned
+ ## resource will be blessed into its "proper" class, if
+ ## +self.class.namespace != nil+.
+ def get
+ self.response = faraday_connection.get(self.href || '')
+ finish_up
+ end
- def create(*args); post(*args) end
- def post(params=nil)
- params ||= self.attributes
- self.response = faraday_connection.post do |req|
- req.body = adapter.serialize(params)
+ ## By default, calls +post+ with the given arguments. Override to
+ ## change this behavior.
+ def create(*args)
+ post(*args)
end
- finish_up
- end
- def update(*args); put(*args) end
- def put(params=nil)
- params ||= self.attributes.changed_attributes
- self.response = faraday_connection.put do |req|
- req.body = adapter.serialize(params)
+ ## POSTs the given attributes to this resource's href, and returns
+ ## the response resource.
+ def post(attrs)
+ self.response = faraday_connection.post do |req|
+ req.body = adapter.serialize(attrs)
+ end
+ finish_up
end
- finish_up
- end
- def delete
- self.response = faraday_connection.delete
- finish_up
- end
+ ## By default, calls +put+ with the given arguments. Override to
+ ## change this behavior.
+ def update(*args)
+ put(*args)
+ end
- ## Returns a raw Faraday connection to this resource's URL, with proper
- ## headers (including auth).
- def faraday_connection(url=nil)
- url ||= URI.join(self.root, self.href)
- key = "faraday_connection_#{url}"
- return Thread.current[key] if Thread.current[key]
+ ## PUTs this resource's attributes to this resource's href, and returns
+ ## the response resource. If attributes are given, +put+ uses those
+ ## instead.
+ def put(attrs=nil)
+ attrs ||= self.attributes
+ self.response = faraday_connection.put do |req|
+ req.body = adapter.serialize(attrs)
+ end
+ finish_up
+ end
- 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)
+ ## PATCHes this resource's changed attributes to this resource's href,
+ ## and returns the response resource. If attributes are given, +patch+
+ ## uses those instead.
+ 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
- Thread.current[key] = fc
- end
- private
+ ## DELETEs this resource's href, and returns the response resource.
+ def delete
+ self.response = faraday_connection.delete
+ finish_up
+ end
- def finish_up
- begin
- self.response_object = self.adapter.deserialize(self.response.body)
- rescue StandardError => e
- raise HyperResource::ResponseError.new(
- "Error when deserializing response body",
- :response => self.response,
- :cause => e
- )
+ ## Returns a raw Faraday connection to this resource's URL, with proper
+ ## headers (including auth). Threadsafe.
+ 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
- self.adapter.apply(self.response_object, self)
- self.loaded = true
+ private
- status = self.response.status
- if status / 100 == 2
- return to_response_class
- elsif status / 100 == 3
- ## TODO redirect logic?
- elsif status / 100 == 4
- raise HyperResource::ClientError.new(status.to_s,
- :response => self.response,
- :response_object => self.response_object)
- elsif status / 100 == 5
- raise HyperResource::ServerError.new(status.to_s,
- :response => self.response,
- :response_object => self.response_object)
+ def finish_up
+ begin
+ self.body = self.adapter.deserialize(self.response.body)
+ rescue StandardError => e
+ raise HyperResource::ResponseError.new(
+ "Error when deserializing response body",
+ :response => self.response,
+ :cause => e
+ )
+ end
- else ## 1xx? really?
- raise HyperResource::ResponseError.new("Got status #{status}, wtf?",
+ self.adapter.apply(self.body, self)
+ self.loaded = true
+
+ status = self.response.status
+ if status / 100 == 2
+ return to_response_class
+ elsif status / 100 == 3
+ ## TODO redirect logic?
+ elsif status / 100 == 4
+ raise HyperResource::ClientError.new(status.to_s,
:response => self.response,
- :response_object => self.response_object)
+ :body => self.body)
+ elsif status / 100 == 5
+ raise HyperResource::ServerError.new(status.to_s,
+ :response => self.response,
+ :body => self.body)
+ else ## 1xx? really?
+ raise HyperResource::ResponseError.new("Got status #{status}, wtf?",
+ :response => self.response,
+ :body => self.body)
+
+ end
end
- end
- end end
+ end
+ end
end