Class: Almodovar::ResourceCollection
- Inherits:
-
Object
- Object
- Almodovar::ResourceCollection
show all
- Includes:
- HttpAccessor, Enumerable
- Defined in:
- lib/almodovar/resource_collection.rb
Constant Summary
collapse
["self::total-entries", "self::link[@rel='next']", "self::link[@rel='prev']"].join('|').freeze
Instance Method Summary
collapse
#check_errors, #http, #query_params, #xml
Constructor Details
#initialize(url, auth, xml = nil, options = {}) ⇒ ResourceCollection
Returns a new instance of ResourceCollection.
10
11
12
13
14
15
|
# File 'lib/almodovar/resource_collection.rb', line 10
def initialize(url, auth, xml = nil, options = {})
@url = url
@auth = auth
@xml = xml if options.empty?
@options = options
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
59
60
61
|
# File 'lib/almodovar/resource_collection.rb', line 59
def method_missing(meth, *args, &blk)
resources.send(meth, *args, &blk)
end
|
Instance Method Details
#create(attrs = {}) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/almodovar/resource_collection.rb', line 17
def create(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
if body.is_a?(Array)
body = body.to_xml(root: root)
else
body = body.to_xml(root: root, convert_links: true, skip_links_one_level: true)
end
response = http.post(@url, body, query_params, { "Content-Type" => "application/xml" })
check_errors(response, @url, query_params)
Resource.new(nil, @auth, Nokogiri::XML.parse(response.body).root)
end
|
#next_page ⇒ Object
42
43
44
|
# File 'lib/almodovar/resource_collection.rb', line 42
def next_page
Resource.new(next_url, @auth) if next_url
end
|
#next_url ⇒ Object
34
35
36
|
# File 'lib/almodovar/resource_collection.rb', line 34
def next_url
@next_url ||= xml.at_xpath("./link[@rel='next']").try(:[], "href")
end
|
#prev_page ⇒ Object
46
47
48
|
# File 'lib/almodovar/resource_collection.rb', line 46
def prev_page
Resource.new(prev_url, @auth) if prev_url
end
|
#prev_url ⇒ Object
38
39
40
|
# File 'lib/almodovar/resource_collection.rb', line 38
def prev_url
@prev_url ||= xml.at_xpath("./link[@rel='prev']").try(:[], "href")
end
|
#total_entries ⇒ Object
30
31
32
|
# File 'lib/almodovar/resource_collection.rb', line 30
def total_entries
@total_entries ||= xml.at_xpath("./total-entries").try(:text).try(:to_i) || resources.size
end
|