Class: Hyperclient::Resource
- Inherits:
-
Object
- Object
- Hyperclient::Resource
- 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
-
#_attributes ⇒ Object
readonly
Returns the attributes of the Resource as Attributes.
-
#_embedded ⇒ Object
readonly
Returns the embedded resource of the Resource as a ResourceCollection.
-
#_links ⇒ Object
readonly
Returns the links of the Resource as a LinkCollection.
-
#_response ⇒ Object
readonly
Returns the response object for the HTTP request that created this resource, if one exists.
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#_self_link ⇒ Object
private
private
Returns the self Link of the Resource.
- #_status ⇒ Object
- #_success? ⇒ Boolean
- #fetch(key, *args) ⇒ Object
-
#initialize(representation, entry_point, response = nil) ⇒ Resource
constructor
Initializes a Resource.
- #inspect ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
private
private
Delegate the method to various elements of the resource.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
private
private
Accessory method to allow the resource respond to methods that will hit method_missing.
Constructor Details
#initialize(representation, entry_point, response = nil) ⇒ Resource
Initializes a Resource.
34 35 36 37 38 39 40 41 42 |
# File 'lib/hyperclient/resource.rb', line 34 def initialize(representation, entry_point, response = nil) representation = representation ? representation.dup : {} links = representation['_links'] || {} @_links = LinkCollection.new(links, links['curies'], entry_point) @_embedded = ResourceCollection.new(representation['_embedded'], entry_point) @_attributes = Attributes.new(representation) @_entry_point = entry_point @_response = response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
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.
Delegate the method to various elements of the resource.
This allows ‘api.posts` instead of `api.links.posts.resource` as well as api.posts(id: 1) assuming posts is a link.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/hyperclient/resource.rb', line 84 def method_missing(method, *args, &block) if args.any? && args.first.is_a?(Hash) _links.send(method, [], &block).(*args) elsif !Enumerable.method_defined?(method) [:_attributes, :_embedded, :_links].each do |target| target = send(target) return target.send(method, *args, &block) if target.respond_to?(method.to_s) end super end end |
Instance Attribute Details
#_attributes ⇒ Object (readonly)
Returns the attributes of the Resource as Attributes.
13 14 15 |
# File 'lib/hyperclient/resource.rb', line 13 def _attributes @_attributes end |
#_embedded ⇒ Object (readonly)
Returns the embedded resource of the Resource as a ResourceCollection.
20 21 22 |
# File 'lib/hyperclient/resource.rb', line 20 def @_embedded end |
#_links ⇒ Object (readonly)
Returns the links of the Resource as a LinkCollection.
16 17 18 |
# File 'lib/hyperclient/resource.rb', line 16 def _links @_links end |
#_response ⇒ Object (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
#[](name) ⇒ Object
56 57 58 |
# File 'lib/hyperclient/resource.rb', line 56 def [](name) send(name) if respond_to?(name) end |
#_self_link ⇒ Object (private)
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.
76 77 78 |
# File 'lib/hyperclient/resource.rb', line 76 def _self_link @_links['self'] end |
#_status ⇒ Object
52 53 54 |
# File 'lib/hyperclient/resource.rb', line 52 def _status _response && _response.status end |
#_success? ⇒ Boolean
48 49 50 |
# File 'lib/hyperclient/resource.rb', line 48 def _success? _response && _response.success? end |
#fetch(key, *args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hyperclient/resource.rb', line 60 def fetch(key, *args) return self[key] if respond_to?(key) if args.any? args.first elsif block_given? yield key else fail KeyError end end |
#inspect ⇒ Object
44 45 46 |
# File 'lib/hyperclient/resource.rb', line 44 def inspect "#<#{self.class.name} self_link:#{_self_link.inspect} attributes:#{@_attributes.inspect}>" end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean (private)
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.
Accessory method to allow the resource respond to methods that will hit method_missing.
98 99 100 101 102 103 |
# File 'lib/hyperclient/resource.rb', line 98 def respond_to_missing?(method, include_private = false) [:_attributes, :_embedded, :_links].each do |target| return true if send(target).respond_to?(method, include_private) end false end |