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 cofnigutation.

  • 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
# 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
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 the API if it exists.

This allows ‘api.posts` instead of `api.links.posts.embedded`



160
161
162
163
164
165
166
167
168
169
# File 'lib/hyperclient/link.rb', line 160

def method_missing(method, *args, &block)
  if @key && _resource.respond_to?(@key) && (delegate = _resource.send(@key)) && delegate.respond_to?(method.to_s)
    # named.named becomes named
    delegate.send(method, *args, &block)
  elsif _resource.respond_to?(method.to_s)
    _resource.send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#_connectionObject

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.



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

def _connection
  @entry_point.connection
end

#_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.



123
124
125
126
127
# File 'lib/hyperclient/link.rb', line 123

def _delete
  Futuroscope::Future.new do
    _connection.delete(_url)
  end
end

#_deprecationObject

Returns the deprecation property of the Link



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

def _deprecation
  @link['deprecation']
end

#_expand(uri_variables) ⇒ Object

Expands the Link when is templated with the given variables.

Parameters:

  • uri_variables

    The Hash with the variables to expand the URITemplate.

Returns:

  • a new Link with the expanded variables.



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

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

#_getObject

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.



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

def _get
  Futuroscope::Future.new do
    _connection.get(_url)
  end
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
120
121
# File 'lib/hyperclient/link.rb', line 117

def _head
  Futuroscope::Future.new do
    _connection.head(_url)
  end
end

#_hreflangObject

Returns the hreflang property of the Link



84
85
86
# File 'lib/hyperclient/link.rb', line 84

def _hreflang
  @link['hreflang']
end

#_nameObject

Returns the name property of the Link



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

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.



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

def _options
  Futuroscope::Future.new do
    _connection.run_request(:options, _url, nil, nil)
  end
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.



141
142
143
144
145
# File 'lib/hyperclient/link.rb', line 141

def _patch(params = {})
  Futuroscope::Future.new do
    _connection.patch(_url, params)
  end
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.



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

def _post(params = {})
  Futuroscope::Future.new do
    _connection.post(_url, params)
  end
end

#_profileObject

Returns the profile property of the Link



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

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.



135
136
137
138
139
# File 'lib/hyperclient/link.rb', line 135

def _put(params = {})
  Futuroscope::Future.new do
    _connection.put(_url, params)
  end
end

#_resourceObject

Returns the Resource which the Link is pointing to.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hyperclient/link.rb', line 89

def _resource
  @resource ||= begin
    response = _get

    if response.success?
      Resource.new(response.body, @entry_point, response)
    else
      Resource.new(nil, @entry_point, response)
    end
  end
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.



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

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

#_titleObject

Returns the title property of the Link



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

def _title
  @link['title']
end

#_typeObject

Returns the type property of the Link



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

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



190
191
192
# File 'lib/hyperclient/link.rb', line 190

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

#_urlObject

Returns the url of the Link.

Raises:

  • MissingURITemplateVariables if the Link is templated but there are no uri variables to expand it.



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

def _url
  return @link['href'] unless _templated?
  fail MissingURITemplateVariablesException if @uri_variables.nil?

  @url ||= _uri_template.expand(@uri_variables)
end

#_variablesObject

Returns an array of variables from the URITemplate.

Returns:

  • an empty array for regular URIs.



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

def _variables
  _uri_template.variables
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.



147
148
149
# File 'lib/hyperclient/link.rb', line 147

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)


173
174
175
176
177
178
179
# File 'lib/hyperclient/link.rb', line 173

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 coersion (such as parallel assignment or Array#flatten). Returning nil tells Ruby that this record is not Array-like.



185
186
187
# File 'lib/hyperclient/link.rb', line 185

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.



151
152
153
# File 'lib/hyperclient/link.rb', line 151

def to_s
  _url
end