Class: Abiquo::SingleResource

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

Instance Method Summary collapse

Methods included from HttpAccessor

#xml

Constructor Details

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

Returns a new instance of SingleResource.



70
71
72
73
74
75
# File 'lib/abiquo.rb', line 70

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 (private)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/abiquo.rb', line 101

def method_missing(meth, *args, &blk)
  attribute = xml.at_xpath("./*[name()='#{meth}' or name()='#{attribute_name(meth)}']")
  return node_text(attribute) if attribute
  
  link = xml.at_xpath("./link[@rel='#{meth}' or @rel='#{attribute_name(meth)}']")
  return Resource.new(link["href"], @auth, link.at_xpath("./*"), *args) if link

  if xml.namespaces.has_key?('xmlns:ns2')
    @xml = Nokogiri.parse(xml.to_s.downcase).root
    link = xml.at_xpath("//ns2:collection/xmlns:title[. = '#{meth}']").try(:parent)
    return Resource.new(link['href'], @auth, nil, *args) if link
  end
  
  super
end

Instance Method Details

#deleteObject

Raises:



83
84
85
86
# File 'lib/abiquo.rb', line 83

def delete
  raise Abiquo::NotAllowed.new(:DELETE, url) unless rest_options.include?(:DELETE)
  http.resource(url).delete
end

#update(attrs = {}) ⇒ Object

Raises:



77
78
79
80
81
# File 'lib/abiquo.rb', line 77

def update(attrs = {})
  raise Abiquo::NotAllowed.new(:PUT, url) unless rest_options.include?(:PUT)
  response = http.resource(url).put(attrs.to_xml(:root => object_type), :content_type => "application/xml")
  @xml = Nokogiri.parse(response.body).root
end

#urlObject



88
89
90
# File 'lib/abiquo.rb', line 88

def url
  @url ||= xml.at_xpath("./link[@rel='edit']").try(:[], "href")
end