Class: AbiquoAPIClient::LinkModel
- Inherits:
-
Object
- Object
- AbiquoAPIClient::LinkModel
- Defined in:
- lib/abiquo-api/model.rb
Overview
Represents a resource in the Abiquo API.
Instance Method Summary collapse
-
#delete(options = {}) ⇒ Object
Executes an HTTP DELETE over the resource in Abiquo API, deleting the current resource.
-
#has_link?(link_rel) ⇒ Boolean
Checks if the object has a link with the ‘rel’ attribute specified as parameter.
-
#initialize(attrs = {}) ⇒ LinkModel
constructor
Constructor.
-
#inspect ⇒ Object
Pretty print an instance object.
-
#link(link_rel) ⇒ Object
Retrieves the link or links that hve the ‘rel’ attribute specified as parameter.
-
#links ⇒ Object
Returns an array of Link for the resource.
-
#refresh(options = {}) ⇒ Object
Executes an HTTP GET over the resource in Abiquo API.
-
#to_json ⇒ Object
Serializes the object into a valid JSON for the Abiquo API.
-
#update(options = {}) ⇒ Object
Executes an HTTP PUT over the resource in Abiquo API, sending the current attributes as data.
Constructor Details
#initialize(attrs = {}) ⇒ LinkModel
Constructor
Accepts a hash of key values representing the resource, plus an instance of the AbiquoAPI class to be used to execute the HTTP requests, specified as the :client attribute.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/abiquo-api/model.rb', line 15 def initialize(attrs={}) raise "Needs a connection!" if attrs[:client].nil? @client = attrs.delete(:client) attributes = Hash[attrs.clone.map {|k, v| [k.to_s, v ] }] if not attributes['links'].nil? links = [] attributes['links'].each do |link| link = link.to_hash if link.is_a? AbiquoAPIClient::Link new_lnk = {} if 'edit'.eql?(link['rel']) or 'self'.eql?(link['rel']) # Create a URL string attribute rel = 'url' create_attr(rel, true) instance_variable_set("@#{rel}", link['href']) end # Create new getters and setters # Also sets value to a Link object rel = "#{link['rel'].gsub(/\//, '_')}" new_lnk[rel.to_sym] = Link.new(link.merge({:client => @client})) links << new_lnk # For every link that points to an ID # create a getter if link['href'].split('/').last.is_a? Integer idrel = "#{link['rel'].gsub(/\//, '_')}_id" create_attr(idrel, true) instance_variable_set("@#{idrel}", link['href'].split('/').last.to_i) end end attributes.delete('links') create_attr("links") instance_variable_set("@links", links) # Now create getters and setters for every method attributes.keys.each do |k| create_attr(k) instance_variable_set("@#{k}", attributes[k]) end end end |
Instance Method Details
#delete(options = {}) ⇒ Object
Executes an HTTP DELETE over the resource in Abiquo API, deleting the current resource.
Returns nil on success.
163 164 165 |
# File 'lib/abiquo-api/model.rb', line 163 def delete( = {}) @client.delete(self.link(:edit), ) end |
#has_link?(link_rel) ⇒ Boolean
Checks if the object has a link with the ‘rel’ attribute specified as parameter.
Parameters:
- link_rel
-
The ‘rel’ value to look for, symbolized.
Returns the true if the object has a link with the specified ‘rel’ or false otherwhise.
142 143 144 145 |
# File 'lib/abiquo-api/model.rb', line 142 def has_link?(link_rel) c = self.links.select {|l| l[link_rel] }.count c == 0 ? false : true end |
#inspect ⇒ Object
Pretty print an instance object.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/abiquo-api/model.rb', line 88 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 << "\n" data << vars.map { |v| "#{v}=#{instance_variable_get(v.to_s).inspect}" }.join(",\n#{Thread.current[:formatador].indentation}") end end data << "\n#{Thread.current[:formatador].indentation}>" data end |
#link(link_rel) ⇒ Object
Retrieves the link or links that hve the ‘rel’ attribute specified as parameter.
Parameters:
- link_rel
-
The ‘rel’ value to look for, symbolized.
Returns the link the ‘rel’ attribute specified or nil if not found.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/abiquo-api/model.rb', line 120 def link(link_rel) ls = self.links.select {|l| l[link_rel] }.map { |t| t.values }.flatten case ls.count when 1 ls.first when 0 nil else ls end end |
#links ⇒ Object
Returns an array of AbiquoAPIClient::Link for the resource
106 107 108 |
# File 'lib/abiquo-api/model.rb', line 106 def links self.links.map {|l| l.values }.flatten end |
#refresh(options = {}) ⇒ Object
Executes an HTTP GET over the resource in Abiquo API.
Returns a new instance representing resource.
172 173 174 |
# File 'lib/abiquo-api/model.rb', line 172 def refresh( = {}) self.link(:edit).get end |
#to_json ⇒ Object
Serializes the object into a valid JSON for the Abiquo API.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/abiquo-api/model.rb', line 65 def to_json att = self.instance_variables.map {|v| v.to_s } links = [] data = {} att.delete("@url") att.delete("@client") self.links.each do |l| links << l.values.first.to_hash end att.delete("@links") att.each do |opt| data[opt.delete("@")] = instance_variable_get(opt) end data['links'] = links data.to_json end |
#update(options = {}) ⇒ Object
Executes an HTTP PUT over the resource in Abiquo API, sending the current attributes as data.
Returns a new instance representing the updated resource.
153 154 155 |
# File 'lib/abiquo-api/model.rb', line 153 def update( = {}) @client.put(self.link(:edit), self.to_json, ) end |