Class: SruClient

Inherits:
Object
  • Object
show all
Includes:
XISBN
Defined in:
lib/sru_client.rb

Overview

SruClient is used by the Opac service model to grab records from SRU servers

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SruClient) initialize(service)

Returns a new instance of SruClient



8
9
10
11
12
13
14
15
# File 'lib/sru_client.rb', line 8

def initialize(service)
  @client = SRU::Client.new(service.sru_url)
  # set client defaults
  @start_record = 1
  @maximum_records = 5
  @record_schema = 'marcxml'
  @results = []
end

Instance Attribute Details

- (Object) accuracy (readonly)

Returns the value of attribute accuracy



6
7
8
# File 'lib/sru_client.rb', line 6

def accuracy
  @accuracy
end

- (Object) maximum_records

Returns the value of attribute maximum_records



7
8
9
# File 'lib/sru_client.rb', line 7

def maximum_records
  @maximum_records
end

- (Object) number_of_results (readonly)

Returns the value of attribute number_of_results



6
7
8
# File 'lib/sru_client.rb', line 6

def number_of_results
  @number_of_results
end

- (Object) record_schema

Returns the value of attribute record_schema



7
8
9
# File 'lib/sru_client.rb', line 7

def record_schema
  @record_schema
end

- (Object) results (readonly)

Returns the value of attribute results



6
7
8
# File 'lib/sru_client.rb', line 6

def results
  @results
end

- (Object) start_record

Returns the value of attribute start_record



7
8
9
# File 'lib/sru_client.rb', line 7

def start_record
  @start_record
end

Instance Method Details

- (Object) construct_conference_search(rft)

Determine if a Referent is probably a conference and construct a CQL query string based on it



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sru_client.rb', line 107

def construct_conference_search(rft)
   = rft.
  query = []
  title = ""
  if ["btitle"]
    title = ["btitle"]
  elsif ["jtitle"]
    title = ["jtitle"]       
  elsif ["title"]
    title = ["title"]
  end
  unless self.is_conference?(title, rft)      
    return false
  end    
  query << 'dc.title all "'+title.gsub(/[^A-z0-9\s]/, ' ')+'"'
    
  if ["volume"]
    query << 'bib.volume = '+["volume"]
  elsif ["date"]
    query << 'dc.date = '+["date"]
  end      
  if query.length < 2
    return false
  end  
  @accuracy = 4
  return query.join(" and ")
end

- (Object) construct_isbn_query(rft)

Create a CQL query string based on ISBN



45
46
47
48
49
50
# File 'lib/sru_client.rb', line 45

def construct_isbn_query(rft)
   = rft.
  return false unless ["isbn"]
  @accuracy = 5
  return "bath.isbn = "+["isbn"].gsub(/[^0-9X]/,'')
end

- (Object) construct_issn_query(rft)

Construct a CQL query string based on ISSN or EISSN



96
97
98
99
100
101
102
103
# File 'lib/sru_client.rb', line 96

def construct_issn_query(rft)
  issn = rft.["issn"]
  issn = rft.["eissn"] unless issn
  return false if issn.nil? or issn.blank?
  issn = issn.insert(4, '-') unless issn[4,1] == "-"   
  @accuracy = 5   
  return "bath.issn = "+issn
end

- (Object) construct_subject_search(subs, sub_ors = nil)

Construct a CQL query string based on subject headings



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sru_client.rb', line 150

def construct_subject_search(subs, sub_ors=nil)
  qsubs = []
  qsubParts = []
  subs.each { |s|
    s.split("--").each { |part|
      qsubParts << 'dc.subject = "'+part+'"'
    }
    qsubs << "("+qsubParts.join(" and ")+")"
  }
  query = qsubs.join(" and ")
  if sub_ors.length > 0
    query += " or " + sub_ors.join(" or ")
  end 
  return query 
end

- (Object) construct_title_author_search(rft)

Create a CQL query string based on title and author



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sru_client.rb', line 70

def construct_title_author_search(rft)
   = rft.
  query = []
  if ["btitle"]
    query << 'dc.title = "'+["btitle"]+'"'      
  elsif ["title"]
    query << 'dc.title = "'+["title"]+'"'
  end
  if ["aulast"]
    author = ["aulast"]
    if ["aufirst"]
      author += ", "+["aufirst"]
    elsif ["auinit1"]
      author += ", "+["auinit1"]
    elsif ["auinit"]
      author += ", "+["auinit"][0,1]
    end
    query << 'dc.author = "'+author+'"'
  end
  return false if query.length < 2   
  @accuracy = 3   
  return query.join(" and ")
end

- (Object) construct_xisbn_query(rft)

Create a CQL query string based on ISBNs provided by OCLC's xISBN service



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sru_client.rb', line 54

def construct_xisbn_query(rft)
  return false unless rft.["isbn"]
  query = []      
  begin
    xisbn(rft.["isbn"], :timeout=>2).each do | isbn|
      query.push("bath.isbn = "+isbn) unless isbn == rft.["isbn"].gsub(/[^0-9X]/,'')          
    end
 rescue TimeoutError, Errno::ECONNREFUSED
 	return false
 end
  return false if query.empty?
  @accuracy = 5
  return query.join(" or ")              
end

- (Object) get_holdings(bib_numbers)

Given an array of bib numbers, will fetch the holdings as Holding objects



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/sru_client.rb', line 167

def get_holdings(bib_numbers)		
	@results = []
	holdings_query = []
	bib_numbers.each do | bib_num |
    holdings_query << 'rec.id = '+bib_num
	end
	holdings_query.each do |hquery |
    begin
      @client.search_retrieve(hquery, :recordSchema=>'opacxml',:recordPacking=>'xml', :startRecord=>1, :maximumRecords=>5).each do | record |            
     bib_num = REXML::XPath.first(record, "./bibliographicRecord/marc:record/controlfield[@tag='001']", {"marc"=>"http://www.loc.gov/MARC21/slim"}).get_text.value        
        new_holding = true
        unless hld = self.search_holdings(bib_num)
          hld = Holding.new
          hld.identifier = bib_num
        else
          new_holding = false
        end
     REXML::XPath.each(record, "./holdings/holding") do |holding| 
          if holding.elements["localLocation"]
            location = holding.elements["localLocation"].get_text.value
          else
            location = ""
          end 	
    if holding.elements["nucCode"]
      nuc_code = holding.elements["nucCode"].get_text.value
    else
      nuc_code = ""
    end               
          unless loc = hld.find_location(location)
            loc = HoldingLocation.new
            loc.name = location
            loc.code = nuc_code
            hld.locations << loc
          end  
          item = HoldingItem.new                	
    if holding.elements["callNumber"]
      call_num = holding.elements["callNumber"].get_text.value
    else
      call_num = "Click for more information"
    end  
    item.call_number = call_num  		
               	
    REXML::XPath.each(holding, "./circulations/circulation") do | circ |
      item.status_date = nil
      if circ.elements["availableNow"].attributes["value"] == "1"
        item.status = "Available"
		item.code = 1
      else
      	item.status = "Not Available  "
		item.code = 2
           if circ.elements["availabilityDate"] and circ.elements["availabilityDate"].has_text?
            item.status_date = circ.elements["availabilityDate"].get_text.value
           end	         
      end
         if circ.elements["enumAndChron"] and circ.elements["enumAndChron"].has_text?
         	item.enumeration = circ.elements["enumAndChron"].get_text.value
         end		      
      if circ.elements["itemId"]
        item.identifier = circ.elements["itemId"].get_text.value
      end
    end
    loc.items << item
  end
  @results << hld if new_holding
  	end
    rescue SRU::Exception
    end
	end
end

- (Boolean) is_conference?(title, metadata)

Checks the Referent's metadata to see if the request is explicitly defined as a conference, otherwise check the title against the Keyword model to see if any matches to conference (i.e. IEEE, SPIE, etc.)

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
# File 'lib/sru_client.rb', line 138

def is_conference?(title, )
  return true if ["genre"] == 'conference' or ["genre"] == 'proceeding'
    
  Keyword.find_all_by_keyword_type('conference').each do |k|      
    r = Regexp.new('\b'+k.term+'\b', true)
    return true if title.match(r)                
  end
  return false      
end

- (Object) search_by_referent(rft)

Find records by passing a Referent model



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sru_client.rb', line 18

def search_by_referent(rft)      
  query_types = {
    'journal'=>[
      'construct_issn_query',
      'construct_isbn_query',
      'construct_conference_search'],
    'book'=>[
      'construct_isbn_query', 
      'construct_xisbn_query',
      'construct_title_author_search',
      'construct_conference_search'
      ]
    }    
  query_types[rft.format].each { | qry |       	       
    if query = self.send(qry, rft)                
      records = @client.search_retrieve(query, {:version=>'1.1', :recordSchema=>@record_schema,:recordPacking=>'xml', :startRecord=>@start_record, :maximumRecords=>@maximum_records})                

      @number_of_results = records.number_of_records
      records.each do | record |
        @results << record
      end
      return if @number_of_results > 0          
    end
  }   
end

- (Object) search_holdings(bib_id)



237
238
239
240
241
242
# File 'lib/sru_client.rb', line 237

def search_holdings(bib_id)
  @results.each do | holding |
    return holding if holding.identifier == bib_id
  end
  return nil
end