Class: Fieldhand::ResponseParser

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

Overview

A parser for elements common to all OAI-PMH HTTP responses.

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

Constant Summary collapse

ERROR_CODES =
{
  'badArgument' => BadArgumentError,
  'badResumptionToken' => BadResumptionTokenError,
  'badVerb' => BadVerbError,
  'cannotDisseminateFormat' => CannotDisseminateFormatError,
  'idDoesNotExist' => IdDoesNotExistError,
  'noRecordsMatch' => NoRecordsMatchError,
  'noMetadataFormats' => NoMetadataFormatsError,
  'noSetHierarchy' => NoSetHierarchyError
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ResponseParser

Return a new parser for the given response body.



35
36
37
# File 'lib/fieldhand/response_parser.rb', line 35

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



32
33
34
# File 'lib/fieldhand/response_parser.rb', line 32

def response
  @response
end

Instance Method Details

#errorsObject

Return any errors found in the response as ‘ProtocolError`s.

Note that this does not raise the errors but simply returns them.



47
48
49
# File 'lib/fieldhand/response_parser.rb', line 47

def errors
  @errors ||= root.locate('error').map { |error| convert_error(error) }
end

#response_dateObject

Return the response date as a ‘Date` or `Time` depending on the granularity of the repository.



40
41
42
# File 'lib/fieldhand/response_parser.rb', line 40

def response_date
  @response_date ||= root.locate('responseDate[0]/^String').map { |date| Datestamp.parse(date) }.first
end

#resumption_tokenObject

Return the resumption token from the response, if present.



52
53
54
# File 'lib/fieldhand/response_parser.rb', line 52

def resumption_token
  @resumption_token ||= root.locate('?/resumptionToken[0]/^String').first
end

#rootObject

Return the root element of the parsed document.



57
58
59
# File 'lib/fieldhand/response_parser.rb', line 57

def root
  @root ||= ::Ox.load(response, :strip_namespace => 'oai').root
end