class Libis::Services::Oai

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/libis/services/oai.rb, line 34
def initialize(url, options = {})
  options[:debug] = true
  @oai_client = OAI::Client.new(url, options)
end

Public Instance Methods

identifiers(token_or_query = nil) click to toggle source
# File lib/libis/services/oai.rb, line 52
def identifiers(token_or_query = nil)
  do_oai_request(:list_identifiers, token_or_query_to_hash(token_or_query))
end
identify() click to toggle source
# File lib/libis/services/oai.rb, line 39
def identify
  do_oai_request(:identify)
end
metadata_formats(identifier = nil) click to toggle source
# File lib/libis/services/oai.rb, line 48
def metadata_formats(identifier = nil)
  do_oai_request(:list_metadata_formats, {identifier: identifier})
end
record(identifier, metadata_prefix = 'oai_dc') click to toggle source
# File lib/libis/services/oai.rb, line 60
def record(identifier, metadata_prefix = 'oai_dc')
  do_oai_request(:get_record, identifier: identifier, metadata_prefix: metadata_prefix)
end
records(token_or_query = nil) click to toggle source
# File lib/libis/services/oai.rb, line 56
def records(token_or_query = nil)
  do_oai_request(:list_records, token_or_query_to_hash(token_or_query))
end
sets(token = nil) click to toggle source
# File lib/libis/services/oai.rb, line 43
def sets(token = nil)
  options = { resumption_token: token }
  do_oai_request(:list_sets, options)
end

Protected Instance Methods

token_or_query_to_hash(token_or_query) click to toggle source
# File lib/libis/services/oai.rb, line 66
def token_or_query_to_hash(token_or_query)
  case token_or_query
  when Hash
    Query.new(token_or_query).to_hash
  when Query
    token_or_query.to_hash
  when String
    { resumption_token: token_or_query }
  else
    {}
  end
end

Private Instance Methods

do_oai_request(method, options = {}) click to toggle source
# File lib/libis/services/oai.rb, line 81
def do_oai_request(method, options = {})
  response = options.cleanup.empty? ? @oai_client.send(method): @oai_client.send(method, options.cleanup)
  object_to_hash(response)
rescue OAI::Exception => e
  raise Libis::Services::ServiceError, "OAI Error: #{e.code} - #{e.message}"
end
object_to_hash(obj) click to toggle source
# File lib/libis/services/oai.rb, line 88
def object_to_hash(obj)
  case obj
    when Array
      obj.map { |x| object_to_hash(x) }
    when Hash
      obj.each_with_object({}) do |k,v,h|
        h[k] = object_to_hash(v)
      end
    when REXML::Element
      Libis::Tools::XmlDocument.parse(obj.to_s).to_hash
    when OAI::Response, OAI::Header, OAI::Record, OAI::MetadataFormat, OAI::Set
      result = obj.instance_variables.map do |x|
        x[1..-1].to_sym
      end.select do |x|
        ![:_source, :doc, :resumption_block].include? x
      end.each_with_object({}) do |x, h|
        h[x] = object_to_hash obj.send(x)
      end
      if obj.methods.include?(:entries)
        result[:entries] = obj.entries.map do |entry|
          object_to_hash(entry)
        end
      end
      result
    else
      obj
  end
end