Class: Hyperclient::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hyperclient/resource.rb

Overview

Represents a resource from your API. Its responsability is to ease the way you access its attributes, links and embedded resources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(representation, entry_point, response = nil) ⇒ Resource

Initializes a Resource.

Parameters:

  • representation

    The hash with the HAL representation of the Resource.

  • entry_point

    The EntryPoint object to inject the configutation.



34
35
36
37
38
39
40
41
# File 'lib/hyperclient/resource.rb', line 34

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

Instance Attribute Details

#attributesObject (readonly)

Returns the attributes of the Resource as Attributes.



13
14
15
# File 'lib/hyperclient/resource.rb', line 13

def attributes
  @attributes
end

#embeddedObject (readonly)

Returns the embedded resource of the Resource as a ResourceCollection.



20
21
22
# File 'lib/hyperclient/resource.rb', line 20

def embedded
  @embedded
end

Returns the links of the Resource as a LinkCollection.



16
17
18
# File 'lib/hyperclient/resource.rb', line 16

def links
  @links
end

#responseObject (readonly)

Returns the response object for the HTTP request that created this resource, if one exists.



24
25
26
# File 'lib/hyperclient/resource.rb', line 24

def response
  @response
end

Instance Method Details

#inspectObject



43
44
45
# File 'lib/hyperclient/resource.rb', line 43

def inspect
  "#<#{self.class.name} self_link:#{self_link.inspect} attributes:#{@attributes.inspect}>"
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the self Link of the Resource. Used to handle the HTTP methods.



58
59
60
# File 'lib/hyperclient/resource.rb', line 58

def self_link
  @links['self']
end

#statusObject



51
52
53
# File 'lib/hyperclient/resource.rb', line 51

def status
  response && response.status
end

#success?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/hyperclient/resource.rb', line 47

def success?
  response && response.success?
end