Sha256: 4b40f37af204135dbe5c0843599d4d816a462f1e95e69fa8716db4b2d97a5329

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

module Almodovar
  class ResourceCollection
    include HttpAccessor
    
    delegate :inspect, :to => :resources
    
    def initialize(url, auth, xml = nil, options = {})
      @url = url
      @auth = auth
      @xml = xml if options.empty?
      @options = options
    end
    
    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_with_params, body.to_xml(:root => root, :convert_links => true, :skip_links_one_level => true), "Content-Type" => "application/xml")
      check_errors(response, url_with_params)
      Resource.new(nil, @auth, Nokogiri::XML.parse(response.body).root)
    end
    
    private
    
    def resources
      @resources ||= xml.xpath("./*").map { |subnode| Resource.new(subnode.at_xpath("./link[@rel='self']").try(:[], "href"), @auth, subnode, @options) }
    end
    
    def method_missing(meth, *args, &blk)
      resources.send(meth, *args, &blk)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
almodovar-0.9.7 lib/almodovar/resource_collection.rb
almodovar-0.9.6 lib/almodovar/resource_collection.rb
almodovar-0.9.5 lib/almodovar/resource_collection.rb
almodovar-0.9.4 lib/almodovar/resource_collection.rb
almodovar-0.9.3 lib/almodovar/resource_collection.rb