Class: Abiquo::Resource

Inherits:
Object show all
Includes:
HttpAccessor
Defined in:
lib/abiquo.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HttpAccessor

#xml

Constructor Details

#initialize(url, auth, xml = nil, options = {}) ⇒ Resource

Returns a new instance of Resource.



176
177
178
179
180
181
# File 'lib/abiquo.rb', line 176

def initialize(url, auth, xml = nil, options = {})
  @url = url
  @auth = auth
  @xml = xml
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



183
184
185
186
# File 'lib/abiquo.rb', line 183

def method_missing(meth, *args, &blk)
  @resource_object ||= resource_class(meth).new(@url, @auth, @xml, @options)
  @resource_object.send(meth, *args, &blk)
end

Class Method Details

.from_xml(xml, auth = nil) ⇒ Object



171
172
173
174
# File 'lib/abiquo.rb', line 171

def self.from_xml(xml, auth = nil)
  doc = Nokogiri.parse(xml)
  new(nil, auth, doc.root)
end

Instance Method Details

#get!Object



196
197
198
199
200
201
202
# File 'lib/abiquo.rb', line 196

def get!
  klass = SingleResource
  if (!xml.children.empty? && xml.name.singularize == xml.children.first.name)
    klass = ResourceCollection
  end
  @resource_object = klass.new(@url, @auth, @xml, @options)
end

#resource_class(meth) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/abiquo.rb', line 188

def resource_class(meth)
  @resource_class ||= if (Array.instance_methods + ["create"] - ["delete", "id"]).include?(meth.to_s)
    ResourceCollection
  else
    SingleResource
  end
end