Sha256: 902bc5aeaaaea29f8a04d4036f5328110bf2cea49b28b95ac8d53666d905e07d

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module Georeferencer
  # This is mixed into Her::Model::Relation and redefines how `fetch()` works, iterating over pages from the GR endpoint
  module CollectionIterator

    def fetch
      # only invoke this is the parent is a Georeferencer class
      if @parent.ancestors.include?(Georeferencer::Base)
        # get the collection
        @_collection = super
        metadata = @_collection.metadata.except(:start)

        # check the params don't specifically include a :start argument (in which case we don't need to loop),
        # and that the response metadata includes a :start key
        if !(@params.keys.include?(:start) || @params.keys.include?("start")) && @_collection.metadata[:start].present?
          @params.merge!(start: @_collection.metadata[:start])
          loop do
            clear_fetch_cache!
            new_collection = super
            @_collection += new_collection
            if new_collection.metadata[:start].present? && new_collection.metadata[:start] != @params[:start]
              @params.merge!(start: new_collection.metadata[:start])
            else
              break
            end
          end
        end

        if @parent.preload_resources == true
          Her::Collection.new(@_collection.collect(&:reload), metadata)
        else
          Her::Collection.new(@_collection, metadata)
        end


      else
        super
      end
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
georeferencer-0.1.5 lib/georeferencer/mixins/collection_iterator.rb
georeferencer-0.1.4 lib/georeferencer/mixins/collection_iterator.rb
georeferencer-0.1.3 lib/georeferencer/mixins/collection_iterator.rb
georeferencer-0.1.2 lib/georeferencer/mixins/collection_iterator.rb
georeferencer-0.1.1 lib/georeferencer/mixins/collection_iterator.rb