lib/hyperclient/resource.rb in hyperclient-0.3.1 vs lib/hyperclient/resource.rb in hyperclient-0.3.2
- old
+ new
@@ -1,5 +1,6 @@
+require 'forwardable'
require 'hyperclient/attributes'
require 'hyperclient/link_collection'
require 'hyperclient/resource_collection'
module Hyperclient
@@ -16,26 +17,40 @@
# Public: Returns the embedded resource of the Resource as a
# ResourceCollection.
attr_reader :embedded
+ # Public: Returns the response object for the HTTP request that created this
+ # resource, if one exists.
+ attr_reader :response
+
# Public: Delegate all HTTP methods (get, post, put, delete, options and
# head) to its self link.
def_delegators :self_link, :get, :post, :put, :delete, :options, :head
# Public: Initializes a Resource.
#
# representation - The hash with the HAL representation of the Resource.
# entry_point - The EntryPoint object to inject the configutation.
- def initialize(representation, entry_point)
+ def initialize(representation, entry_point, response=nil)
+ representation = representation ? representation.dup : {}
@links = LinkCollection.new(representation['_links'], entry_point)
@embedded = ResourceCollection.new(representation['_embedded'], entry_point)
@attributes = Attributes.new(representation)
@entry_point = entry_point
+ @response = response
end
def inspect
"#<#{self.class.name} self_link:#{self_link.inspect} attributes:#{@attributes.inspect}>"
+ end
+
+ def success?
+ response && response.success?
+ end
+
+ def status
+ response && response.status
end
private
# Internal: Returns the self Link of the Resource. Used to handle the HTTP
# methods.