Sha256: 8770a3b618de8fe78b3c01688fff3d6fef1c214cfbbab2d007f608e621eab317

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module ArchivesSpace
  # Handle API Pagination using enumerator
  module Pagination
    # TODO: get via lookup of endpoints that support pagination? (nice-to-have)
    ENDPOINTS = %w[
      accessions
      agents/corporate_entities
      agents/families
      agents/people
      agents/software
      archival_objects
      digital_objects
      groups
      repositories
      resources
      subjects
      users
    ]

    ENDPOINTS.each do |endpoint|
      method_name = endpoint.split("/").last # remove prefix
      define_method(method_name) do |options = {}|
        all(endpoint, options)
      end
    end

    def all(path, options = {})
      Enumerator.new do |yielder|
        page = 1
        unlimited_listing = false
        loop do
          options[:query] ||= {}
          options[:query][:page] = page
          result = get(path, options)
          results = []

          if result.parsed.respond_to?(:key) && result.parsed.key?("results")
            results = result.parsed["results"]
          else
            results = result.parsed
            unlimited_listing = true
          end

          if results.any?
            results.each do |i|
              yielder << i
            end
            raise StopIteration if unlimited_listing

            page += 1
          else
            raise StopIteration
          end
        end
      end.lazy
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
archivesspace-client-0.4.1 lib/archivesspace/client/pagination.rb
archivesspace-client-0.4.0 lib/archivesspace/client/pagination.rb
archivesspace-client-0.3.0 lib/archivesspace/client/pagination.rb
archivesspace-client-0.2.0 lib/archivesspace/client/pagination.rb