Class: Hyperclient::Link Private

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



14
15
16
17
18
19
20
# File 'lib/hyperclient/link.rb', line 14

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.



126
127
128
129
130
131
132
133
# File 'lib/hyperclient/link.rb', line 126

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.



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

def _delete
  http_method(:delete)
end

#_deprecationObject

Returns the deprecation property of the Link



63
64
65
# File 'lib/hyperclient/link.rb', line 63

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.



35
36
37
# File 'lib/hyperclient/link.rb', line 35

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

#_getObject

Returns the Resource which the Link is pointing to.



87
88
89
# File 'lib/hyperclient/link.rb', line 87

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.



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

def _head
  http_method(:head)
end

#_hreflangObject

Returns the hreflang property of the Link



78
79
80
# File 'lib/hyperclient/link.rb', line 78

def _hreflang
  @link['hreflang']
end

#_nameObject

Returns the name property of the Link



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

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.



91
92
93
# File 'lib/hyperclient/link.rb', line 91

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.



111
112
113
# File 'lib/hyperclient/link.rb', line 111

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.



103
104
105
# File 'lib/hyperclient/link.rb', line 103

def _post(params = {})
  http_method(:post, params)
end

#_profileObject

Returns the profile property of the Link



68
69
70
# File 'lib/hyperclient/link.rb', line 68

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.



107
108
109
# File 'lib/hyperclient/link.rb', line 107

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.



82
83
84
# File 'lib/hyperclient/link.rb', line 82

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.



26
27
28
# File 'lib/hyperclient/link.rb', line 26

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

#_titleObject

Returns the title property of the Link



73
74
75
# File 'lib/hyperclient/link.rb', line 73

def _title
  @link['title']
end

#_typeObject

Returns the type property of the Link



53
54
55
# File 'lib/hyperclient/link.rb', line 53

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



164
165
166
# File 'lib/hyperclient/link.rb', line 164

def _uri_template
  @uri_template ||= URITemplate.new(@link['href'])
end

#_urlObject

Returns the url of the Link.



40
41
42
43
# File 'lib/hyperclient/link.rb', line 40

def _url
  return @link['href'] unless _templated?
  @url ||= _uri_template.expand(@uri_variables || {})
end

#_variablesObject

Returns an array of variables from the URITemplate.

Returns:

  • an empty array for regular URIs.



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

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`



138
139
140
141
142
143
# File 'lib/hyperclient/link.rb', line 138

def delegate_method(method, *args, &block)
  return unless @key && _resource.respond_to?(@key)
  @delegate ||= _resource.send(@key)
  return unless @delegate && @delegate.respond_to?(method.to_s)
  @delegate.send(method, *args, &block)
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.



168
169
170
171
172
173
# File 'lib/hyperclient/link.rb', line 168

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.



115
116
117
# File 'lib/hyperclient/link.rb', line 115

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)


147
148
149
150
151
152
153
# File 'lib/hyperclient/link.rb', line 147

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.



159
160
161
# File 'lib/hyperclient/link.rb', line 159

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.



119
120
121
# File 'lib/hyperclient/link.rb', line 119

def to_s
  _url
end