Class: Fieldhand::Paginator
- Inherits:
-
Object
- Object
- Fieldhand::Paginator
- 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
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#initialize(uri, logger_or_options = {}) ⇒ Paginator
constructor
Return a new paginator for the given repository base URI and optional logger, timeout, bearer token and headers.
-
#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`.
Constructor Details
#initialize(uri, logger_or_options = {}) ⇒ Paginator
Return a new paginator for the given repository base URI and optional logger, timeout, 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, the bearer token will default to nil and headers will default to empty hash.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fieldhand/paginator.rb', line 22 def initialize(uri, = {}) @uri = uri.is_a?(::URI) ? uri : URI(uri) = Options.new() @logger = .logger @timeout = .timeout @headers = .headers @http = ::Net::HTTP.new(@uri.host, @uri.port) @http.read_timeout = @timeout @http.open_timeout = @timeout @http.use_ssl = true if @uri.scheme == 'https' end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
14 15 16 |
# File 'lib/fieldhand/paginator.rb', line 14 def headers @headers end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
14 15 16 |
# File 'lib/fieldhand/paginator.rb', line 14 def http @http end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/fieldhand/paginator.rb', line 14 def logger @logger end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
14 15 16 |
# File 'lib/fieldhand/paginator.rb', line 14 def timeout @timeout end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
14 15 16 |
# File 'lib/fieldhand/paginator.rb', line 14 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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fieldhand/paginator.rb', line 59 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 |