Class: Hyperclient::Link Private

Inherits:
Object
  • Object
show all
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

EntryPoint

Instance Method Summary collapse

Constructor Details

#initialize(key, link, entry_point, uri_variables = nil) ⇒ Link

Initializes a new Link.

Parameters:

  • key

    The key or name of the link.

  • link

    The String with the URI of the link.

  • entry_point

    The EntryPoint object to inject the configuration.

  • uri_variables (defaults to: nil)

    The optional Hash with the variables to expand the link if it is templated.



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

#_deleteObject

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

#_deprecationObject

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.

Parameters:

  • uri_variables (defaults to: {})

    The Hash with the variables to expand the URITemplate.

Returns:

  • a new Link with the expanded variables.



56
57
58
# File 'lib/hyperclient/link.rb', line 56

def _expand(uri_variables = {})
  self.class.new(@key, @link, @entry_point, uri_variables)
end

#_getObject

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

#_headObject

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

#_hreflangObject

Returns the hreflang property of the Link



100
101
102
# File 'lib/hyperclient/link.rb', line 100

def _hreflang
  @link['hreflang']
end

#_nameObject

Returns the name property of the Link



80
81
82
# File 'lib/hyperclient/link.rb', line 80

def _name
  @link['name']
end

#_optionsObject

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 _options
  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

#_profileObject

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

#_resourceObject

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.

Returns:

  • (Boolean)

    true if it is templated.

  • false if it not templated.



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

def _templated?
  !!@link['templated']
end

#_titleObject

Returns the title property of the Link



95
96
97
# File 'lib/hyperclient/link.rb', line 95

def _title
  @link['title']
end

#_typeObject

Returns the type property of the Link



75
76
77
# File 'lib/hyperclient/link.rb', line 75

def _type
  @link['type']
end

#_uri_templateObject (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

#_urlObject

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.expand(@uri_variables || {}).to_s
end

#_variablesObject

Returns an array of variables from the URITemplate.

Returns:

  • an empty array for regular URIs.



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.

Returns:

  • an Enumerator.



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

#inspectObject

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.

Returns:

  • (Boolean)


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_aryObject (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_sObject

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