Class: Almodovar::SingleResource
- Inherits:
-
Object
- Object
- Almodovar::SingleResource
show all
- Includes:
- HttpAccessor
- Defined in:
- lib/almodovar/single_resource.rb
Instance Method Summary
collapse
#check_errors, #http, #query_params, #xml
Constructor Details
#initialize(url, auth, xml = nil, options = {}) ⇒ SingleResource
Returns a new instance of SingleResource.
8
9
10
11
12
13
|
# File 'lib/almodovar/single_resource.rb', line 8
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
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/almodovar/single_resource.rb', line 58
def method_missing(meth, *args, &blk)
if node = node(meth)
return node['type'] == 'document' ? Resource.from_xml(node.to_xml) : node_text(node)
end
link = link(meth)
return Resource.new(link["href"], @auth, link.at_xpath("./*"), *args) if link
super
end
|
Instance Method Details
#[](key) ⇒ Object
for resources with type “document”
47
48
49
50
|
# File 'lib/almodovar/single_resource.rb', line 47
def [](key)
return super unless xml.at_xpath("/*[@type='document']")
Hash.from_xml(xml.to_xml).values.first[key]
end
|
#delete(extra_query_params = {}) ⇒ Object
23
24
25
|
# File 'lib/almodovar/single_resource.rb', line 23
def delete( = {})
check_errors(http.delete(@url, ), @url, )
end
|
#respond_to?(meth, include_all = false) ⇒ Boolean
52
53
54
|
# File 'lib/almodovar/single_resource.rb', line 52
def respond_to?(meth, include_all=false)
super || (node(meth) != nil) || (link(meth) != nil)
end
|
#to_hash ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/almodovar/single_resource.rb', line 31
def to_hash
xml_without_type = xml.dup.tap do |xml|
xml.delete("type")
end
if xml_without_type.content.strip.empty?
{}
else
xml_hash = Hash.from_xml(xml_without_type.to_s)
xml_hash[xml_without_type.name]
end
end
|
#update(attrs = {}) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/almodovar/single_resource.rb', line 15
def update(attrs = {})
raise ArgumentError.new("You must specify one only root element which is the type of resource (e.g. `:project => { :name => 'Wadus' }` instead of just `:name => 'Wadus'`)") if attrs.size > 1
root, body = attrs.first
response = http.put(@url, body.to_xml(root: root), {}, { "Content-Type" => "application/xml" })
check_errors(response, @url)
@xml = Nokogiri::XML.parse(response.body).root
end
|
#url ⇒ Object
27
28
29
|
# File 'lib/almodovar/single_resource.rb', line 27
def url
@url ||= xml.at_xpath("./link[@rel='self']").try(:[], "href")
end
|