Class: Fieldhand::Arguments

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

Overview

A class for converting Fieldhand arguments into OAI-PMH query parameters.

Specifically:

  • :metadata_prefix

  • :resumption_token

  • :from

  • :until

  • :set

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

Constant Summary collapse

VALID_KEYS =
{
  :metadata_prefix => 'metadataPrefix',
  :resumption_token => 'resumptionToken',
  :from => 'from',
  :until => 'until',
  :set => 'set'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Arguments

Return a new ‘Arguments` with the given `Hash`.



29
30
31
# File 'lib/fieldhand/arguments.rb', line 29

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/fieldhand/arguments.rb', line 26

def options
  @options
end

Instance Method Details

#to_queryObject

Return a query as a ‘Hash` suitable for encoding as a query string in an OAI-PMH request.

Converts arguments passed with symbol keys into the corresponding strings as defined in the OAI-PMH protocol, converting values into the appropriate format (e.g. ‘Time`s, `Date`s, `MetadataFormat`s and `Set`s into strings).

Defaults to returning a metadata prefix of “oai_dc”.

Raises an ‘ArgumentError` if an unknown argument is encountered.

# Examples

“‘ Fieldhand::Arguments.new(:metadata_prefix => ’xoai’, :from => Date.new(2001, 1, 1)).to_query #=> { “metadataPrefix” => “xoai”, “from” => “2001-01-01” }

Fieldhand::Arguments.new(:until => Time.utc(2001, 1, 1, 12, 0, 0)).to_query #=> { “metadataPrefix”=>“oai_dc”, “until” => “2001-01-01T12:00:00Z” }

Fieldhand::Arguments.new(:foo => “bar”).to_query # ArgumentError: unknown argument: foo “‘



54
55
56
57
58
59
60
61
62
# File 'lib/fieldhand/arguments.rb', line 54

def to_query
  options.inject(defaults) do |query, (key, value)|
    raise ::ArgumentError, "unknown argument: #{key}" unless VALID_KEYS.key?(key)

    query[VALID_KEYS.fetch(key)] = convert_value(key, value)

    query
  end
end