Class: SRU::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sru/client.rb

Overview

A client for issuing requests to a particular SRU server. SRU is a RESTlike information retrieval protocol detailed at www.loc.gov/standards/sru/

require 'sru'

client = SRU::Client.new 'http://sru.example.com'

# fetch a SRU::ExplainResponse object from the server
explain = client.explain

# issue a search and get back a SRU::SearchRetrieveResponse object 
# which serves as an iterator 
records = client.search_retrieve 'rosetta stone', :maximumRecords => 5
records.each {|record| puts record}

# issue a scan request and print out each term
client.scan('king tut', :maximumTerms => 12).each {|term| puts term}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, options = {}) ⇒ Client

creates a client object which will automatically perform an explain request to determine the version to be used in subsequent requests.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sru/client.rb', line 32

def initialize(base,options={})
  @server = URI.parse base
  @parser = options.fetch(:parser, 'rexml')
  case @parser
     when 'libxml'
 begin
           require 'rubygems'
           require 'libxml'
  rescue
          raise SRU::Exception, "unknown parser: #{@parser}", caller 
 end
     when 'rexml'
  require 'rexml/document'
         require 'rexml/xpath'
     else
          raise SRU::Exception, "unknown parser: #{@parser}", caller
     end
    
  # stash this away for future requests
  @version = self.explain.version
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



27
28
29
# File 'lib/sru/client.rb', line 27

def version
  @version
end

Instance Method Details

#explainObject

Send an explain request to the SRU server and return a SRU::ExplainResponse object.

client = SRU::Client.new ‘sru.example.com’ explain = client.explain



61
62
63
64
# File 'lib/sru/client.rb', line 61

def explain
  doc = get_doc(:operation => 'explain')
  return ExplainResponse.new(doc, @parser)
end

#scan(clause, options = {}) ⇒ Object

Send a scan request to the SRU server and return a SRU::ScanResponse object. You must supply the first parameter which is the searchClause. Other SRU options can be sent in a hash as the seond argument.

scan_response = client.scan 'title', :maximumTerms => 5


91
92
93
94
95
96
97
# File 'lib/sru/client.rb', line 91

def scan(clause, options={})
  options[:scanClause] = clause
  options[:operation] = 'scan'
  options[:maximumTerms] = 5 unless options.has_key? :maximumTerms
  doc = get_doc(options)
  return ScanResponse.new(doc, @parser)
end

#search_retrieve(query, options = {}) ⇒ Object

Send a searchRetrieve request to the SRU server and return a SRU::SearchResponse object. The first argument is the required query option. Any remaining searchRetrieve options can be passed as an optional second argument.

client = SRU::Client.new 'http://example.com/sru'
response = client.search_retrieve 'mark twain', maximumRecords => 1


75
76
77
78
79
80
81
82
# File 'lib/sru/client.rb', line 75

def search_retrieve(query, options={})
  options[:query] = query
  options[:operation] = 'searchRetrieve'
  options[:maximumRecords] = 10 unless options.has_key? :maximumRecords
  options[:recordSchema] = 'dc' unless options.has_key? :recordSchema
  doc = get_doc(options)
  return SearchResponse.new(doc, @parser)
end