class Fieldhand::Arguments

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

Specifically:

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

Constants

VALID_KEYS

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source

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

# File lib/fieldhand/arguments.rb, line 29
def initialize(options = {})
  @options = options
end

Public Instance Methods

to_query() click to toggle source

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 “`

# 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

Private Instance Methods

convert_value(key, value) click to toggle source
# File lib/fieldhand/arguments.rb, line 70
def convert_value(key, value)
  return value.to_s unless key == :from || key == :until

  Datestamp.unparse(value)
end
defaults() click to toggle source
# File lib/fieldhand/arguments.rb, line 66
def defaults
  { 'metadataPrefix' => 'oai_dc' }
end