Class: WorldCat

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

Overview

The WorldCat class, used to interact with the WorldCat search webservices.

Defined Under Namespace

Classes: WorldCatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ WorldCat

The constructor. The API key can be given here or later.



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

def initialize(api_key = nil)
  @api_key = api_key
  @raw_url = nil
  @raw_response = nil
end

Instance Attribute Details

#api_key=(value) ⇒ Object (writeonly)

The WorldCat webservices API key.



35
36
37
# File 'lib/worldcat.rb', line 35

def api_key=(value)
  @api_key = value
end

#raw_responseObject (readonly)

The raw response from WorldCat.



38
39
40
# File 'lib/worldcat.rb', line 38

def raw_response
  @raw_response
end

#raw_urlObject (readonly)

The raw url used to fetch the response.



41
42
43
# File 'lib/worldcat.rb', line 41

def raw_url
  @raw_url
end

Instance Method Details

#formatted_citations(options) ⇒ Object

Formatted Citations.

aliases:

  • :citation_format is an alias for :cformat

  • record identifier should be given as: :oclc => [oclc_number]

this method returns a HTML formatted String.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/worldcat.rb', line 234

def formatted_citations(options)
  url_comp = "content/citations/"

  # Check aliases
  options.keys.each do |k|
    case k
    when :citation_format then options[:cformat] = options.delete(k)
    when :oclc then url_comp << options.delete(k).to_s
    end
  end

  fetch(url_comp, options)
  if options.has_key? :cformat
    xml_diagnostic
  else
    str_diagnostic
  end

  @raw_response
end

#library_catalog_url(options) ⇒ Object

Libray Catalog URL for a Record.

aliases:

  • record identifier should be given as type => id. e.g.: :isbn => “014330223X”

this method returns a MARC::Record.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/worldcat.rb', line 209

def library_catalog_url(options)
  url_comp = "content/libraries/"

  # Check aliases
  options.keys.each do |k|
    case k
    when :oclc then url_comp << options.delete(k).to_s
    when :isbn then url_comp << "isbn/" << options.delete(k).to_s
    end
  end

  #TODO get diagnostic for "no holdings found" instead of raising it.
  fetch(url_comp, options)
  xml_diagnostic
  REXML::Document.new(@raw_response)
end

#library_locations(options) ⇒ Object

Library locations method.

aliases:

  • :start is an alias for :start_library

  • :count and :max are aliases for :maximum_libraries

  • :latitude is an alias for :lat

  • :longitude is an alias for :lon

  • libtype can be given as text value as well. e.g.: :libtype => :academic

  • record identifier should be given as type => id. e.g.: :isbn => “014330223X”

this method returns a REXML::Document for XML format, or a Hash for JSON format.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/worldcat.rb', line 141

def library_locations(options)
  url_comp = "content/libraries/"

  # Check aliases
  options.keys.each do |k|
    case k
    when :count, :max then options[:maximum_libraries] = options.delete(k)
    when :start then options[:start_library] = options.delete(k)
    when :latitude then options[:lat] = options.delete(k)
    when :longitude then options[:lon] = options.delete(k)
    when :format then options.delete(k) if options[k].to_s == "xml"
    when :libtype
      libtype = options[k].to_s
      options[k] = 1 if libtype == "academic"
      options[k] = 2 if libtype == "public"
      options[k] = 3 if libtype == "government"
      options[k] = 4 if libtype == "other"
    when :oclc then url_comp << options.delete(k).to_s
    when :isbn then url_comp << "isbn/" << options.delete(k).to_s
    when :issn then url_comp << "issn/" << options.delete(k).to_s
    when :sn then url_comp << "sn/" << options.delete(k).to_s
    end
  end

  if options.has_key? :format
    fetch(url_comp, options)
    json_diagnostic
    response = JSON.parse(@raw_response)
  else
    fetch(url_comp, options)
    xml_diagnostic
    response = REXML::Document.new(@raw_response)
  end

  response
end

#open_search(options) ⇒ Object

OpenSearch method.

Aliases:

  • :query is an alias for :q

  • :max is an alias for :count

  • :citation_format is an alias for :cformat

This method returns a SimpleRSS object. You can see the usage on: simple-rss.rubyforge.org/



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/worldcat.rb', line 60

def open_search(options)
  # Check aliases
  options.keys.each do |k|
    case k
    when :query then options[:q] = options.delete(k)
    when :max then options[:count] = options.delete(k)
    when :citation_format then options[:cformat] = options.delete(k)
    end
  end

  fetch("search/opensearch", options)
  #TODO diagnostic

  # Add tags
  SimpleRSS.feed_tags << :"opensearch:totalResults"
  SimpleRSS.feed_tags << :"opensearch:startIndex"
  SimpleRSS.feed_tags << :"opensearch:itemsPerPage"
  SimpleRSS.item_tags << :"dc:identifier"
  SimpleRSS.item_tags << :"oclcterms:recordIdentifier"

  SimpleRSS.parse @raw_response
  #TODO rescue SimpleRSS Error? (i.e. response too small)
end

#single_record(options) ⇒ Object

Single Bibliographic Record.

aliases:

  • record identifier should be given as type => id. e.g.: :isbn => “014330223X”

this method returns a MARC::Record.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/worldcat.rb', line 185

def single_record(options)
  url_comp = "content/"

  # Check aliases
  options.keys.each do |k|
    case k
    when :oclc then url_comp << options.delete(k).to_s
    when :isbn then url_comp << "isbn/" << options.delete(k).to_s
    when :issn then url_comp << "issn/" << options.delete(k).to_s
    end
  end

  fetch(url_comp, options)
  xml_diagnostic
  marc_to_array.first
end

#sru_search(options) ⇒ Object

SRU search method.

aliases:

  • :q is an alias for :query

  • :format is an alias for :record_schema

and its value can match “marc” or “dublin”, or can be the exact value. e.g.

:format => :marcxml
  • :citation_format is an alias for :cformat

  • :start is an alias for :start_record

  • :count and :max are aliases for :maximum_records

this method returns an array of MARC::Record objects for marc format (you can see the usage on marc.rubyforge.org), or a REXML::Document for Dublin Core format.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/worldcat.rb', line 98

def sru_search(options)
  #TODO add other control_tags?

  # Check aliases
  options.keys.each do |k|
    case k
    when :q then options[:query] = options.delete(k)
    when :count, :max then options[:maximum_records] = options.delete(k)
    when :start then options[:start_record] = options.delete(k)
    when :citation_format then options[:cformat] = options.delete(k)
    when :format
      format = options.delete(k).to_s
      if format =~ /marc/ then format = "info:srw/schema/1/marcxml" end
      if format =~ /dublin/ then format = "info:srw/schema/1/dc" end
      options[:record_schema] = format
    end
  end

  fetch("search/sru", options)
  xml_diagnostic

  format = options[:record_schema]
  if format.nil? || format == "info:srw/schema/1/marcxml"
    marc_to_array
  else
    REXML::Document.new @raw_response
  end
end