Class: AbiquoAPIClient::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/abiquo-api/link.rb

Overview

Represents a link on the Abiquo API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Link

Constructor.

Accepts a hash reprsenting a link, usually returned after parsing the JSON response.

If the hash contains :client key, the value will be used as an AbiquoAPI client allowing the get method to retrieve the target resource.



35
36
37
38
39
40
41
42
43
44
# File 'lib/abiquo-api/link.rb', line 35

def initialize(hash)
  @client = hash.delete(:client) if hash.keys.include?(:client)

  h = Hash[hash.map {|k, v| [k.to_sym, v ] }]

  @href = h[:href].nil? ? '' : h[:href]
  @rel = h[:rel].nil? ? '' : h[:rel]
  @title = h[:title].nil? ? '' : h[:title]
  @type = h[:type].nil? ? '' : h[:type]
end

Instance Attribute Details

#hrefObject

The target URL of the link



11
12
13
# File 'lib/abiquo-api/link.rb', line 11

def href
  @href
end

#relObject

The ‘rel’ attribute of the link



15
16
17
# File 'lib/abiquo-api/link.rb', line 15

def rel
  @rel
end

#titleObject

The title of the link



19
20
21
# File 'lib/abiquo-api/link.rb', line 19

def title
  @title
end

#typeObject

The media type of the link



23
24
25
# File 'lib/abiquo-api/link.rb', line 23

def type
  @type
end

Instance Method Details

#get(options = {}) ⇒ Object

If the :client attribute is not nil, will retrieve the resource or collection that this link represents, or nil otherwise



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/abiquo-api/link.rb', line 51

def get(options = {})
  if @client.nil?
    return nil
  else
    r = @client.get(self, options)
    if r.is_a? Hash
      AbiquoAPI::LinkCollection.new(r, self.type, @client)
    else
      r
    end
  end
end

#inspectObject

Pretty print the object.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/abiquo-api/link.rb', line 85

def inspect
  Thread.current[:formatador] ||= Formatador.new
  data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
  Thread.current[:formatador].indent do
    unless self.instance_variables.empty?
      vars = self.instance_variables.clone
      vars.delete(:@client)
      data << " "
      data << vars.map { |v| "#{v}=#{instance_variable_get(v.to_s).inspect}" }.join(", ")
    end
  end
  data << " >"
  data
end

#to_hashObject

Converts an instance to its hash form, so it can be serialized as JSON.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/abiquo-api/link.rb', line 68

def to_hash
  h = self.href.nil? ? '' : self.href
  r = self.rel.nil? ? '' : self.rel
  t = self.title.nil? ? '' : self.title
  y = self.type.nil? ? '' : self.type

  { 
    "href"  => h,
    "type"  => y,
    "rel"   => r,
    "title" => t
  }
end