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.



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

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



30
31
32
# File 'lib/fieldhand/response_parser.rb', line 30

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.



45
46
47
# File 'lib/fieldhand/response_parser.rb', line 45

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.



38
39
40
# File 'lib/fieldhand/response_parser.rb', line 38

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.



50
51
52
# File 'lib/fieldhand/response_parser.rb', line 50

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

#rootObject

Return the root element of the parsed document.



55
56
57
# File 'lib/fieldhand/response_parser.rb', line 55

def root
  @root ||= ::Ox.parse(response).root
end