Class: Fieldhand::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/fieldhand/repository.rb

Overview

A repository is a network accessible server that can process the 6 OAI-PMH requests.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Return a new repository with the given base URL and an optional logger, timeout, bearer token and headers.

The base URL can be passed as a ‘URI` or anything that can be parsed as a URI such as a string.

For backward compatibility, the second argument can either be a logger or a hash containing a logger, timeout, bearer token and headers.

Defaults to using a null logger specific to this platform, a timeout of 60 seconds, no bearer token and no headers.



27
28
29
30
31
32
33
34
# File 'lib/fieldhand/repository.rb', line 27

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

  options = Options.new(logger_or_options)
  @logger = options.logger
  @timeout = options.timeout
  @headers = options.headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#get(identifier, arguments = {}) ⇒ Object

Send a GetRecord request to the repository with the given identifier and optional metadata prefix and return a ‘Record`.

Supports passing a :metadata_prefix argument with a given metadata prefix which otherwise defaults to “oai_dc”.

Raises a ‘NetworkError` if there is an issue contacting the repository or a `ProtocolError` if received in response.

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



125
126
127
128
129
130
131
132
# File 'lib/fieldhand/repository.rb', line 125

def get(identifier, arguments = {})
  query = {
    'identifier' => identifier,
    'metadataPrefix' => arguments.fetch(:metadata_prefix, 'oai_dc')
  }

  paginator.items('GetRecord', GetRecordParser, query).first
end

#identifiers(arguments = {}) ⇒ Object

Send a ListIdentifiers request to the repository with optional arguments and return an ‘Enumerator` of `Header`s.

This supports the same arguments as ‘Fieldhand::Repository#records` but only returns record headers.

Raises a ‘NetworkError` if there is an issue contacting the repository or a `ProtocolError` if received in response.

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



110
111
112
113
114
# File 'lib/fieldhand/repository.rb', line 110

def identifiers(arguments = {})
  query = Arguments.new(arguments).to_query

  paginator.items('ListIdentifiers', ListIdentifiersParser, query)
end

#identifyObject

Send an Identify request to the repository and return an ‘Identify` response.

Raises a ‘NetworkError` if there is an issue contacting the repository or a `ProtocolError` if received in response.

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



42
43
44
# File 'lib/fieldhand/repository.rb', line 42

def identify
  paginator.items('Identify', IdentifyParser).first
end

#metadata_formats(identifier = nil) ⇒ Object

Send a ListMetadataFormats request to the repository (with an optional identifier) and return an ‘Enumerator` of `MetadataFormat`s.

Raises a ‘NetworkError` if there is an issue contacting the repository or a `ProtocolError` if received in response.

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



53
54
55
56
57
58
# File 'lib/fieldhand/repository.rb', line 53

def (identifier = nil)
  query = {}
  query['identifier'] = identifier if identifier

  paginator.items('ListMetadataFormats', ListMetadataFormatsParser, query)
end

#records(arguments = {}) ⇒ Object

Send a ListRecords request to the repository with optional arguments and return an ‘Enumerator` of `Records`s.

The following arguments can be used:

  • :metadata_prefix - The prefix of the metadata format to be used for record metadata, defaults to “oai_dc”

  • :from - A ‘Date`, `Time` or formatted string specifying a lower bound for datestamp-based selective harvesting

  • :until - A ‘Date`, `Time` or formatted string specifying an upper bound for datestamp-based selective harvesting

  • :set - A ‘Set` or string set spec which specifies set criteria for selective harvesting

  • :resumption_token - A valid resumption token for resuming a previous request (note that Fieldhand typically

    handles resumption internally so this should not be normally used)
    

Raises a ‘NetworkError` if there is an issue contacting the repository or a `ProtocolError` if received in response.

# Examples

“‘ repository = Fieldhand::Repository.new(’www.example.com/oai’) repository.records.each do |record|

next if record.deleted?

puts record.

end “‘

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



96
97
98
99
100
# File 'lib/fieldhand/repository.rb', line 96

def records(arguments = {})
  query = Arguments.new(arguments).to_query

  paginator.items('ListRecords', ListRecordsParser, query)
end

#setsObject

Send a ListSets request to the repository and return an ‘Enumerator` of `Set`s.

Raises a ‘NetworkError` if there is an issue contacting the repository or a `ProtocolError` if received in response.

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



66
67
68
# File 'lib/fieldhand/repository.rb', line 66

def sets
  paginator.items('ListSets', ListSetsParser)
end