Sha256: 0e9d41257f904a6917fe316bf811f7c76a57c7e723341a7381393066dddf82d1

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.

module AMEE
  class Collection < Array

    def v3
      collectionpath=~/^\/3/
    end

    def fetch_with_v3
      @options.merge! @pager.options if @pager
      retries = [1] * connection.retries
      begin
        if @use_v3_connection == true
          @response = @connection.v3_get(collectionpath, @options)
        else
          @response= @connection.get(collectionpath, @options).body
        end
        if @response.is_json?
          @json = true
          @doc = JSON.parse(@response)
        else
          @doc = load_xml_doc(@response)
        end
      rescue JSON::ParserError, Nokogiri::XML::SyntaxError
        @connection.expire(collectionpath)
        if delay = retries.shift
          sleep delay
          retry
        else
          raise
        end
      end
    end
    alias_method_chain :fetch, :v3

    def each_page_with_v3
      @pager=AMEE::Limiter.new(@options) if v3
      # in v3 need to specify limit to start with, not in v2
      begin           
        fetch
        yield
        if json
          @pager = v3 ? AMEE::Limiter.from_json(doc,@pager.options.merge(@options)) :  # json not built
            AMEE::Pager.from_json(doc['pager'])
        else
          @pager = v3 ? 
               AMEE::Limiter.from_xml(doc.xpath('/Representation/*').first,
               @pager.options.merge(@options)) :
               AMEE::Pager.from_xml(doc.xpath('/Resources//Pager').first)
        end
        break if @max && length>=@max
      end while @pager && @pager.next! #pager is nil if no pager in response,
      # pager.next! is false if @pager said current=last.
    end
    alias_method_chain :each_page, :v3

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
amee-3.2.1 lib/amee/v3/collection.rb
amee-3.2.0 lib/amee/v3/collection.rb
amee-3.1.2 lib/amee/v3/collection.rb
amee-3.1.1 lib/amee/v3/collection.rb
amee-3.0.1 lib/amee/v3/collection.rb