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(link, entry_point, uri_variables = nil) ⇒ Link

Initializes a new Link.

Parameters:

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



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

def initialize(link, entry_point, uri_variables = nil)
  @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.links.posts.embedded` instead of `api.links.posts.resource.embedded`



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

def method_missing(method, *args, &block)
  if resource.respond_to?(method)
    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.



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

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.



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

def delete
  connection.delete(url)
end

#deprecationObject

Returns the deprecation property of the Link



66
67
68
# File 'lib/hyperclient/link.rb', line 66

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.



33
34
35
# File 'lib/hyperclient/link.rb', line 33

def expand(uri_variables)
  self.class.new(@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.



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

def get
  connection.get(url)
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.



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

def head
  connection.head(url)
end

#hreflangObject

Returns the hreflang property of the Link



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

def hreflang
  @link['hreflang']
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.



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

def inspect
  "#<#{self.class.name} #{@link}>"
end

#nameObject

Returns the name property of the Link



61
62
63
# File 'lib/hyperclient/link.rb', line 61

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.



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

def options
  connection.run_request(:options, url, nil, nil)
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.



126
127
128
# File 'lib/hyperclient/link.rb', line 126

def patch(params = {})
  connection.patch(url, 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.



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

def post(params = {})
  connection.post(url, params)
end

#profileObject

Returns the profile property of the Link



71
72
73
# File 'lib/hyperclient/link.rb', line 71

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.



122
123
124
# File 'lib/hyperclient/link.rb', line 122

def put(params = {})
  connection.put(url, params)
end

#resourceObject

Returns the Resource which the Link is pointing to.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hyperclient/link.rb', line 86

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

#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)


149
150
151
# File 'lib/hyperclient/link.rb', line 149

def respond_to_missing?(method, include_private = false)
  resource.respond_to?(method.to_s)
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.



24
25
26
# File 'lib/hyperclient/link.rb', line 24

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

#titleObject

Returns the title property of the Link



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

def title
  @link['title']
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.



157
158
159
# File 'lib/hyperclient/link.rb', line 157

def to_ary
  nil
end

#typeObject

Returns the type property of the Link



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

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



162
163
164
# File 'lib/hyperclient/link.rb', line 162

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.



41
42
43
44
45
46
# File 'lib/hyperclient/link.rb', line 41

def url
  return @link['href'] unless templated?
  raise 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.



51
52
53
# File 'lib/hyperclient/link.rb', line 51

def variables
  uri_template.variables
end