Sha256: 94dd6e39b12762c267787a0468b92b40d361124e22e3befb2838b285fea8f7f9

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

module Almodovar
  class ResourceCollection
    include HttpAccessor
    include Enumerable

    PAGINATION_ENTITIES = ["self::total-entries", "self::link[@rel='next']", "self::link[@rel='prev']"].join('|').freeze

    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
      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

    def total_entries
      @total_entries ||= xml.at_xpath("./total-entries").try(:text).try(:to_i) || resources.size
    end

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

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

    def next_page
      Resource.new(next_url, @auth) if next_url
    end

    def prev_page
      Resource.new(prev_url, @auth) if prev_url
    end

    private

    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

    def method_missing(meth, *args, &blk)
      resources.send(meth, *args, &blk)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
almodovar-2.0.2 lib/almodovar/resource_collection.rb
almodovar-2.0.1 lib/almodovar/resource_collection.rb
almodovar-1.8.1 lib/almodovar/resource_collection.rb
almodovar-2.0.0 lib/almodovar/resource_collection.rb
almodovar-1.8.0 lib/almodovar/resource_collection.rb
almodovar-1.7.8 lib/almodovar/resource_collection.rb