Class: Fieldhand::Paginator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fieldhand/paginator.rb

Overview

An abstraction over interactions with an OAI-PMH repository, handling requests, responses and paginating over results using a resumption token.

See www.openarchives.org/OAI/openarchivesprotocol.html#FlowControl

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, logger_or_options = {}) ⇒ Paginator

Return a new paginator for the given repository base URI and optional logger, timeout, maximum number of retries, retry interval, bearer token and headers.

The URI can be passed as either a ‘URI` or something that can be parsed as a URI such as a string.

The logger will default to a null logger appropriate to this platform, timeout will default to 60 seconds, maximum number of retries will default to 0, the retry interval will default to 10 seconds, the bearer token will default to nil and headers will default to empty hash.



32
33
34
35
36
37
38
39
40
# File 'lib/fieldhand/paginator.rb', line 32

def initialize(uri, logger_or_options = {})
  @uri = uri.is_a?(::URI) ? uri : URI(uri)
  @options = Options.new(logger_or_options)

  @http = ::Net::HTTP.new(@uri.host, @uri.port)
  @http.read_timeout = @options.timeout
  @http.open_timeout = @options.timeout
  @http.use_ssl = true if @uri.scheme == 'https'
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



17
18
19
# File 'lib/fieldhand/paginator.rb', line 17

def http
  @http
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/fieldhand/paginator.rb', line 17

def options
  @options
end

#uriObject (readonly)

Returns the value of attribute uri.



17
18
19
# File 'lib/fieldhand/paginator.rb', line 17

def uri
  @uri
end

Instance Method Details

#items(verb, parser_class, query = {}) ⇒ Object

Return an ‘Enumerator` of items retrieved from the repository with the given `verb` and `query`, parsed with the given `parser_class`.

The query defaults to an empty hash but will be merged with the given ‘verb` when making requests to the repository.

Expects the ‘parser_class` to respond to `items`, returning an `Enumerable` list of items that will be yielded to the caller.

Raises a ‘ProtocolError` for any errors in the response.

Fieldhand attempts to handle all flow control for the user using resumption tokens from the response so they only need handle lazy enumerators and not worry about pagination and underlying network requests.

# Examples

“‘ paginator = Fieldhand::Paginator.new(’www.example.com/oai’) paginator.items(‘ListRecords’, Fieldhand::ListRecordsParser).take(10_000) #=> [#<Fieldhand::Record: …>, …] “‘

See www.openarchives.org/OAI/openarchivesprotocol.html#FlowControl



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fieldhand/paginator.rb', line 65

def items(verb, parser_class, query = {})
  return enum_for(:items, verb, parser_class, query) unless block_given?

  loop do
    response_parser = parse_response(query.merge('verb' => verb))
    parser_class.new(response_parser).items.each do |item|
      yield item
    end

    break unless response_parser.resumption_token

    logger.debug('Fieldhand') { "Resumption token for #{verb}: #{response_parser.resumption_token}" }
    query = { 'resumptionToken' => response_parser.resumption_token }
  end
end