class Almodovar::ResourceCollection

Constants

PAGINATION_ENTITIES

Public Class Methods

new(url, auth, xml = nil, options = {}) click to toggle source
# 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

Public Instance Methods

create(attrs = {}) click to toggle source
# 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
  response = http.post(@url, body.to_xml(root: root, convert_links: true, skip_links_one_level: true), 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() click to toggle source
# File lib/almodovar/resource_collection.rb, line 37
def next_page
  Resource.new(next_url, @auth) if next_url
end
next_url() click to toggle source
# File lib/almodovar/resource_collection.rb, line 29
def next_url
  @next_url ||= xml.at_xpath("./link[@rel='next']").try(:[], "href")
end
prev_page() click to toggle source
# File lib/almodovar/resource_collection.rb, line 41
def prev_page
  Resource.new(prev_url, @auth) if prev_url
end
prev_url() click to toggle source
# File lib/almodovar/resource_collection.rb, line 33
def prev_url
  @prev_url ||= xml.at_xpath("./link[@rel='prev']").try(:[], "href")
end
total_entries() click to toggle source
# File lib/almodovar/resource_collection.rb, line 25
def total_entries
  @total_entries ||= xml.at_xpath("./total-entries").try(:text).try(:to_i) || resources.size
end

Private Instance Methods

method_missing(meth, *args, &blk) click to toggle source
# File lib/almodovar/resource_collection.rb, line 54
def method_missing(meth, *args, &blk)
  resources.send(meth, *args, &blk)
end
resources() click to toggle source
# File lib/almodovar/resource_collection.rb, line 47
def resources
  @resources ||= begin
    xml.xpath("./*[not(#{PAGINATION_ENTITIES})]").
      map { |subnode| Resource.new(subnode.at_xpath("./link[@rel='self']").try(:[], "href"), @auth, subnode, @options) }
  end
end