Class: Hyperclient::Link Private
- Inherits:
-
Object
- Object
- Hyperclient::Link
- Includes:
- Enumerable
- Defined in:
- lib/hyperclient/link.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The Link is used to let a Resource interact with the API.
Direct Known Subclasses
Instance Method Summary collapse
- #_delete ⇒ Object private
-
#_deprecation ⇒ Object
Returns the deprecation property of the Link.
-
#_expand(uri_variables = {}) ⇒ Object
Expands the Link when is templated with the given variables.
-
#_get ⇒ Object
Returns the Resource which the Link is pointing to.
- #_head ⇒ Object private
-
#_hreflang ⇒ Object
Returns the hreflang property of the Link.
-
#_name ⇒ Object
Returns the name property of the Link.
- #_options ⇒ Object private
- #_patch(params = {}) ⇒ Object private
- #_post(params = {}) ⇒ Object private
-
#_profile ⇒ Object
Returns the profile property of the Link.
- #_put(params = {}) ⇒ Object private
- #_resource ⇒ Object private
-
#_templated? ⇒ Boolean
Indicates if the link is an URITemplate or a regular URI.
-
#_title ⇒ Object
Returns the title property of the Link.
-
#_type ⇒ Object
Returns the type property of the Link.
-
#_uri_template ⇒ Object
private
private
Memoization for a URITemplate instance.
-
#_url ⇒ Object
Returns the url of the Link.
-
#_variables ⇒ Object
Returns an array of variables from the URITemplate.
-
#delegate_method(method, *args, &block) ⇒ Object
private
private
Delegate the method to the API if the resource cannot serve it.
-
#each(&block) ⇒ Object
Each implementation to allow the class to use the Enumerable benefits for paginated, embedded items.
- #http_method(method, body = nil) ⇒ Object private private
-
#initialize(key, link, entry_point, uri_variables = nil) ⇒ Link
constructor
Initializes a new Link.
- #inspect ⇒ Object private
-
#method_missing(method, *args, &block) ⇒ Object
private
private
Delegate the method further down the API if the resource cannot serve it.
-
#respond_to_missing?(method, _include_private = false) ⇒ Boolean
private
private
Accessory method to allow the link respond to the methods that will hit method_missing.
-
#to_ary ⇒ Object
private
private
avoid delegating to resource.
- #to_s ⇒ Object private
Constructor Details
#initialize(key, link, entry_point, uri_variables = nil) ⇒ Link
Initializes a new Link.
16 17 18 19 20 21 22 |
# File 'lib/hyperclient/link.rb', line 16 def initialize(key, link, entry_point, uri_variables = nil) @key = key @link = link @entry_point = entry_point @uri_variables = uri_variables @resource = nil 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 further down the API if the resource cannot serve it.
148 149 150 151 152 153 154 155 |
# File 'lib/hyperclient/link.rb', line 148 def method_missing(method, *args, &block) if _resource.respond_to?(method.to_s) result = _resource.send(method, *args, &block) result.nil? ? delegate_method(method, *args, &block) : result else super end end |
Instance Method Details
#_delete ⇒ Object
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.
121 122 123 |
# File 'lib/hyperclient/link.rb', line 121 def _delete http_method(:delete) end |
#_deprecation ⇒ Object
Returns the deprecation property of the Link
85 86 87 |
# File 'lib/hyperclient/link.rb', line 85 def _deprecation @link['deprecation'] end |
#_expand(uri_variables = {}) ⇒ Object
Expands the Link when is templated with the given variables.
56 57 58 |
# File 'lib/hyperclient/link.rb', line 56 def (uri_variables = {}) self.class.new(@key, @link, @entry_point, uri_variables) end |
#_get ⇒ Object
Returns the Resource which the Link is pointing to.
109 110 111 |
# File 'lib/hyperclient/link.rb', line 109 def _get http_method(:get) end |
#_head ⇒ Object
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.
117 118 119 |
# File 'lib/hyperclient/link.rb', line 117 def _head http_method(:head) end |
#_hreflang ⇒ Object
Returns the hreflang property of the Link
100 101 102 |
# File 'lib/hyperclient/link.rb', line 100 def _hreflang @link['hreflang'] end |
#_name ⇒ Object
Returns the name property of the Link
80 81 82 |
# File 'lib/hyperclient/link.rb', line 80 def _name @link['name'] end |
#_options ⇒ Object
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.
113 114 115 |
# File 'lib/hyperclient/link.rb', line 113 def http_method(:options) end |
#_patch(params = {}) ⇒ Object
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.
133 134 135 |
# File 'lib/hyperclient/link.rb', line 133 def _patch(params = {}) http_method(:patch, params) end |
#_post(params = {}) ⇒ Object
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.
125 126 127 |
# File 'lib/hyperclient/link.rb', line 125 def _post(params = {}) http_method(:post, params) end |
#_profile ⇒ Object
Returns the profile property of the Link
90 91 92 |
# File 'lib/hyperclient/link.rb', line 90 def _profile @link['profile'] end |
#_put(params = {}) ⇒ Object
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.
129 130 131 |
# File 'lib/hyperclient/link.rb', line 129 def _put(params = {}) http_method(:put, params) end |
#_resource ⇒ Object
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.
104 105 106 |
# File 'lib/hyperclient/link.rb', line 104 def _resource @resource || _get end |
#_templated? ⇒ Boolean
Indicates if the link is an URITemplate or a regular URI.
47 48 49 |
# File 'lib/hyperclient/link.rb', line 47 def _templated? !!@link['templated'] end |
#_title ⇒ Object
Returns the title property of the Link
95 96 97 |
# File 'lib/hyperclient/link.rb', line 95 def _title @link['title'] end |
#_type ⇒ Object
Returns the type property of the Link
75 76 77 |
# File 'lib/hyperclient/link.rb', line 75 def _type @link['type'] end |
#_uri_template ⇒ 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.
Memoization for a URITemplate instance
188 189 190 |
# File 'lib/hyperclient/link.rb', line 188 def _uri_template @uri_template ||= Addressable::Template.new(@link['href']) end |
#_url ⇒ Object
Returns the url of the Link.
61 62 63 64 65 |
# File 'lib/hyperclient/link.rb', line 61 def _url return @link['href'] unless _templated? @url ||= _uri_template.(@uri_variables || {}).to_s end |
#_variables ⇒ Object
Returns an array of variables from the URITemplate.
70 71 72 |
# File 'lib/hyperclient/link.rb', line 70 def _variables _uri_template.variables end |
#delegate_method(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 the API if the resource cannot serve it.
This allows ‘api.posts` instead of `api._links.posts.embedded.posts`
160 161 162 163 164 165 166 167 |
# File 'lib/hyperclient/link.rb', line 160 def delegate_method(method, *args, &block) return unless @key && _resource.respond_to?(@key) @delegate ||= _resource.send(@key) return unless @delegate&.respond_to?(method.to_s) @delegate.send(method, *args, &block) end |
#each(&block) ⇒ Object
Each implementation to allow the class to use the Enumerable benefits for paginated, embedded items.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/hyperclient/link.rb', line 28 def each(&block) if block_given? current = self while current coll = current.respond_to?(@key) ? current.send(@key) : _resource coll.each(&block) break unless current._links[:next] current = current._links.next end else to_enum(:each) end end |
#http_method(method, body = nil) ⇒ 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.
192 193 194 195 196 197 |
# File 'lib/hyperclient/link.rb', line 192 def http_method(method, body = nil) @resource = begin response = @entry_point.connection.run_request(method, _url, body, nil) Resource.new(response.body, @entry_point, response) end end |
#inspect ⇒ Object
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.
137 138 139 |
# File 'lib/hyperclient/link.rb', line 137 def inspect "#<#{self.class.name}(#{@key}) #{@link}>" 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 link respond to the methods that will hit method_missing.
171 172 173 174 175 176 177 |
# File 'lib/hyperclient/link.rb', line 171 def respond_to_missing?(method, _include_private = false) if @key && _resource.respond_to?(@key) && (delegate = _resource.send(@key)) && delegate.respond_to?(method.to_s) true else _resource.respond_to?(method.to_s) end end |
#to_ary ⇒ 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.
avoid delegating to resource
#to_ary is called for implicit array coercion (such as parallel assignment or Array#flatten). Returning nil tells Ruby that this record is not Array-like.
183 184 185 |
# File 'lib/hyperclient/link.rb', line 183 def to_ary nil end |
#to_s ⇒ Object
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.
141 142 143 |
# File 'lib/hyperclient/link.rb', line 141 def to_s _url end |