Class: WorldCat
- Inherits:
-
Object
- Object
- WorldCat
- 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
-
#api_key ⇒ Object
writeonly
The WorldCat webservices API key.
-
#raw_response ⇒ Object
readonly
The raw response from WorldCat.
-
#raw_url ⇒ Object
readonly
The raw url used to fetch the response.
Instance Method Summary collapse
-
#formatted_citations(options) ⇒ Object
Formatted Citations.
-
#initialize(api_key = nil) ⇒ WorldCat
constructor
The constructor.
-
#library_catalog_url(options) ⇒ Object
Libray Catalog URL for a Record.
-
#library_locations(options) ⇒ Object
Library locations method.
-
#open_search(options) ⇒ Object
OpenSearch method.
-
#single_record(options) ⇒ Object
Single Bibliographic Record.
-
#sru_search(options) ⇒ Object
SRU search method.
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_response ⇒ Object (readonly)
The raw response from WorldCat.
38 39 40 |
# File 'lib/worldcat.rb', line 38 def raw_response @raw_response end |
#raw_url ⇒ Object (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() url_comp = "content/citations/" # Check aliases .keys.each do |k| case k when :citation_format then [:cformat] = .delete(k) when :oclc then url_comp << .delete(k).to_s end end fetch(url_comp, ) if .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() url_comp = "content/libraries/" # Check aliases .keys.each do |k| case k when :oclc then url_comp << .delete(k).to_s when :isbn then url_comp << "isbn/" << .delete(k).to_s end end #TODO get diagnostic for "no holdings found" instead of raising it. fetch(url_comp, ) 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() url_comp = "content/libraries/" # Check aliases .keys.each do |k| case k when :count, :max then [:maximum_libraries] = .delete(k) when :start then [:start_library] = .delete(k) when :latitude then [:lat] = .delete(k) when :longitude then [:lon] = .delete(k) when :format then .delete(k) if [k].to_s == "xml" when :libtype libtype = [k].to_s [k] = 1 if libtype == "academic" [k] = 2 if libtype == "public" [k] = 3 if libtype == "government" [k] = 4 if libtype == "other" when :oclc then url_comp << .delete(k).to_s when :isbn then url_comp << "isbn/" << .delete(k).to_s when :issn then url_comp << "issn/" << .delete(k).to_s when :sn then url_comp << "sn/" << .delete(k).to_s end end if .has_key? :format fetch(url_comp, ) json_diagnostic response = JSON.parse(@raw_response) else fetch(url_comp, ) 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() # Check aliases .keys.each do |k| case k when :query then [:q] = .delete(k) when :max then [:count] = .delete(k) when :citation_format then [:cformat] = .delete(k) end end fetch("search/opensearch", ) #TODO diagnostic # Add tags SimpleRSS. << :"opensearch:totalResults" SimpleRSS. << :"opensearch:startIndex" SimpleRSS. << :"opensearch:itemsPerPage" SimpleRSS. << :"dc:identifier" SimpleRSS. << :"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() url_comp = "content/" # Check aliases .keys.each do |k| case k when :oclc then url_comp << .delete(k).to_s when :isbn then url_comp << "isbn/" << .delete(k).to_s when :issn then url_comp << "issn/" << .delete(k).to_s end end fetch(url_comp, ) 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() #TODO add other control_tags? # Check aliases .keys.each do |k| case k when :q then [:query] = .delete(k) when :count, :max then [:maximum_records] = .delete(k) when :start then [:start_record] = .delete(k) when :citation_format then [:cformat] = .delete(k) when :format format = .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 [:record_schema] = format end end fetch("search/sru", ) xml_diagnostic format = [:record_schema] if format.nil? || format == "info:srw/schema/1/marcxml" marc_to_array else REXML::Document.new @raw_response end end |