module Classifoclc

Interface to OCLC's Classify service, “a FRBR-based prototype designed to support the assignment of classification numbers and subject headings for books, DVDs, CDs, and other types of materials.”

@see www.oclc.org/developer/develop/web-services/classify.en.html

OCLC Documentation

Constants

URL
VERSION

Public Class Methods

author(auth, hsh = {}) click to toggle source

Find works by author @param [String] author Author to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 73
def self.author(auth, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::AUTHOR, :value  => auth}))
end
authorAndTitle(author, title, hsh = {}) click to toggle source

Find works by author and title @param [String] author Author to search for @param [String] title Title to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 90
def self.authorAndTitle(author, title, hsh = {})
  lookup(default_options(hsh, {:identifier => [Id::AUTHOR, Id::TITLE], :value => [author, title], :summary => false}))
end
fast(ident, hsh = {}) click to toggle source

Find works by FAST subject heading @param [String] fast FAST subject ID to search for @param [String] title Title to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 99
def self.fast(ident, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::IDENT, :value  => ident}))
end
fetch_data(hsh) click to toggle source
# File lib/classifoclc.rb, line 196
def self.fetch_data(hsh)
  resp = open(URL % param_string(hsh.clone), "User-Agent" => "Classifoclc/#{Classifoclc::VERSION}",).read
  parsed = Nokogiri::XML(resp)
  return parsed
end
isbn(isbn, hsh = {}) click to toggle source

Find works by ISBN number @param [String] isbn ISBN number to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 41
def self.isbn(isbn, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::ISBN, :value  => isbn}))
end
lccn(lccn, hsh = {}) click to toggle source

Find works by Library of Congress Control Number @param [String] lccn LCCN to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 65
def self.lccn(lccn, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::LCCN, :value  => lccn}))
end
maxRecs() click to toggle source

@overload maxRecs

Get the max records per request
@return [Numeric]

@overload maxRecs=(value)

Set the max records
@param value [Numeric] the new value
@return [Numeric] the new value
# File lib/classifoclc.rb, line 29
def self.maxRecs
  @@maxRecs
end
maxRecs=(m) click to toggle source
# File lib/classifoclc.rb, line 33
def self.maxRecs= m
  @@maxRecs = m
end
oclc(oclc, hsh = {}) click to toggle source

Find works by OCLC number @param [String] oclc OCLC number to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 57
def self.oclc(oclc, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::OCLC, :value  => oclc}))
end
owi(owi, hsh = {}) click to toggle source

Find works by OCLC work ID @param [String] owi Work ID to search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 49
def self.owi(owi, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::OWI, :value  => owi, :summary => false}))
end
title(title, hsh = {}) click to toggle source

Find works by title @param [String] title Titleto search for @param [Hash] hsh more options @return [Enumerator<Classifoclc::Work>]

# File lib/classifoclc.rb, line 81
def self.title(title, hsh = {})
  lookup(default_options(hsh, {:identifier => Id::TITLE, :value  => title}))
end

Private Class Methods

api_params(hsh) click to toggle source
# File lib/classifoclc.rb, line 177
                     def self.api_params(hsh)
  params = {:orderBy => "%s %s" % [hsh.delete(:orderby), hsh.delete(:order)]}.merge(hsh)
  return params
end
default_options(hsh1, hsh2 = {}) click to toggle source
# File lib/classifoclc.rb, line 170
                     def self.default_options(hsh1, hsh2 = {})
  {:orderby => OrderBy::HOLDINGS,
   :order => Order::DESC,
   :maxRecs => maxRecs,
   :summary => true}.merge(hsh1).merge(hsh2)
end
lookup(hsh) click to toggle source
# File lib/classifoclc.rb, line 103
                     def self.lookup(hsh)
  parsed = fetch_data(hsh)
  resp_code = parsed.css('response').first['code']

  if resp_code == '0'
    owid = parsed.css('work').first['owi']
    return owi(owid)
  end

  if resp_code == '2'
    return Enumerator.new do |w|
      w << Work::new(parsed)
    end
  end

  if resp_code == "4"
    if hsh[:identifier] == :owi
      if parsed.css('work').map{|w| w['owi']}.include?(hsh[:value])
        raise Classifoclc::InfiniteLoopError.new("The record for owi %s contains records also with the owi %s. Cannot fetch data as it would lead to an infinite loop" % [hsh[:value], hsh[:value]])
      end
    end
    return multiwork(parsed, hsh)
  end
  
  if resp_code == '100'
    raise BadIdentifierError.
            new "%s is not allowed as an identifer" % hsh[:identifier]
  end

  if resp_code == '101'
    raise BadIdentifierFormatError.
            new "Invalid format (%s) for %s identifier" %
                [hsh[:value], hsh[:identifier]]
  end

  if resp_code =='102'
    return Enumerator.new do |w|
    end
  end

  if resp_code == '200'
    raise UnexpectedError.new "Unexpected error"
  end
  
  raise UnexpectedError.new "Unexpected response code %s" % resp_code
end
multiwork(parsed, hsh) click to toggle source
# File lib/classifoclc.rb, line 150
                     def self.multiwork(parsed, hsh)
  return Enumerator.new do |work|
    loop do
      if parsed.css("navigation next").empty?
        next_page = nil
      else
        next_page = parsed.css("navigation next").first.text
      end
      
      parsed.css('work').each do |multi|
        owi(multi['owi']).each do |w|
          work << w
        end
      end
      break if next_page.nil?
      parsed = fetch_data(hsh.clone.merge({:startRec => next_page}))
    end
  end
end
param_string(hsh) click to toggle source
# File lib/classifoclc.rb, line 182
                     def self.param_string(hsh)
  id = hsh.delete(:identifier)
  val = hsh.delete(:value)

  if id.is_a? Array
    params = default_options(Hash[id.zip(val)], hsh)
  else
    params = default_options({id => val}, hsh)
  end
  
  return api_params(params)
           .map{|k,v| "#{k}=#{URI.encode_www_form_component(v)}"}.join("&") 
end