Sha256: bfb24e9b81b8a0ccefc06e86a5c4967de29e3b4bc48e7fdb25801effaf03a34d

Contents?: true

Size: 1.4 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
archivesspace-client-0.1.12 lib/archivesspace/client/pagination.rb