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.



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

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`



154
155
156
157
158
159
160
# File 'lib/hyperclient/link.rb', line 154

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.



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

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.



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

def delete
  Futuroscope::Future.new{
    connection.delete(url)
  }
end

#deprecationObject

Returns the deprecation property of the Link



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

def deprecation
  @link['deprecation']
end

#expand(uri_variables) ⇒ Object

Expands the Link when is templated with the given variables.



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

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.



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

def get
  Futuroscope::Future.new{
    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.



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

def head
  Futuroscope::Future.new{
    connection.head(url)
  }
end

#hreflangObject

Returns the hreflang property of the Link



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

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.



145
146
147
# File 'lib/hyperclient/link.rb', line 145

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

#nameObject

Returns the name property of the Link



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

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.



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

def options
  Futuroscope::Future.new{
    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.



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

def patch(params = {})
  Futuroscope::Future.new{
    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.



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

def post(params = {})
  Futuroscope::Future.new{
    connection.post(url, params)
  }
end

#profileObject

Returns the profile property of the Link



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

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.



133
134
135
136
137
# File 'lib/hyperclient/link.rb', line 133

def put(params = {})
  Futuroscope::Future.new{
    connection.put(url, params)
  }
end

#resourceObject

Returns the Resource which the Link is pointing to.



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

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.



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

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.



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

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

#titleObject

Returns the title property of the Link



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

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.



172
173
174
# File 'lib/hyperclient/link.rb', line 172

def to_ary
  nil
end

#typeObject

Returns the type property of the Link



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

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



177
178
179
# File 'lib/hyperclient/link.rb', line 177

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.



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

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.



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

def variables
  uri_template.variables
end