worldcatapi 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +20 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +69 -0
- data/Guardfile +9 -0
- data/LICENSE +20 -0
- data/README.md +35 -0
- data/Rakefile +9 -0
- data/example.rb +56 -0
- data/lib/worldcatapi/client.rb +131 -0
- data/lib/worldcatapi/get_location_response.rb +43 -0
- data/lib/worldcatapi/get_record_response.rb +63 -0
- data/lib/worldcatapi/open_search_response.rb +130 -0
- data/lib/worldcatapi/record.rb +11 -0
- data/lib/worldcatapi/sru_search_response.rb +71 -0
- data/lib/worldcatapi/version.rb +3 -0
- data/lib/worldcatapi.rb +15 -0
- data/test/lib/worldcatapi_test.rb +9 -0
- data/test/test_helper.rb +3 -0
- data/worldcatapi.gemspec +26 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MTdhZjEzN2E4YTNlZDNmYmU4YTU0NGNlNzgwZGRiOTkzMjczMzU1OQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OTdjMWVmOGRkNzk1MmNkY2QxOGQyNmE5MGY4MzZjYmI4OGY5MmM3Ng==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OGMyOGJmNDJlZDZmOGI2NGE4Nzk4MTNhYWFiMDcwOTVlZDRiZjUwNDM3NDZk
|
10
|
+
NWU5NGYzOGNmMjQ5YmJlZmM5MTU1NTkwZmYyZjhmNWExNTkwZGZmNmYwMzUy
|
11
|
+
NDE3Y2IwNTAyMWY1OTAwYjcxMjI0Yjk3ODcxMGI5MTM2YWMyN2M=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODk5OWQ3ZGEwNGQ5ZTRiODdmZmRlZWJiM2E2ZWU5YTBhZDY5ZjRiMDg4MjA1
|
14
|
+
OTRlY2U5YTMzNWRiZjE5Y2QwMWMzYTE4MTA3NWIyYmFhZTNlOTExZmRmMTNi
|
15
|
+
NzZjNWRkMzc3NTVmZTAxZGFmYzMyMmQzZDY0ZTQ0MGQ1MWI3MjQ=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
worldcatapi (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (4.0.1)
|
10
|
+
i18n (~> 0.6, >= 0.6.4)
|
11
|
+
minitest (~> 4.2)
|
12
|
+
multi_json (~> 1.3)
|
13
|
+
thread_safe (~> 0.1)
|
14
|
+
tzinfo (~> 0.3.37)
|
15
|
+
atomic (1.1.14)
|
16
|
+
celluloid (0.15.2)
|
17
|
+
timers (~> 1.1.0)
|
18
|
+
coderay (1.1.0)
|
19
|
+
ffi (1.9.3)
|
20
|
+
formatador (0.2.4)
|
21
|
+
guard (2.2.4)
|
22
|
+
formatador (>= 0.2.4)
|
23
|
+
listen (~> 2.1)
|
24
|
+
lumberjack (~> 1.0)
|
25
|
+
pry (>= 0.9.12)
|
26
|
+
thor (>= 0.18.1)
|
27
|
+
guard-test (2.0.3)
|
28
|
+
guard (~> 2.0)
|
29
|
+
test-unit (~> 2.2)
|
30
|
+
i18n (0.6.5)
|
31
|
+
listen (2.3.1)
|
32
|
+
celluloid (>= 0.15.2)
|
33
|
+
rb-fsevent (>= 0.9.3)
|
34
|
+
rb-inotify (>= 0.9)
|
35
|
+
lumberjack (1.0.4)
|
36
|
+
method_source (0.8.2)
|
37
|
+
minitest (4.7.5)
|
38
|
+
multi_json (1.8.2)
|
39
|
+
pry (0.9.12.4)
|
40
|
+
coderay (~> 1.0)
|
41
|
+
method_source (~> 0.8)
|
42
|
+
slop (~> 3.4)
|
43
|
+
rake (10.1.0)
|
44
|
+
rb-fsevent (0.9.3)
|
45
|
+
rb-inotify (0.9.2)
|
46
|
+
ffi (>= 0.5.0)
|
47
|
+
shoulda (3.5.0)
|
48
|
+
shoulda-context (~> 1.0, >= 1.0.1)
|
49
|
+
shoulda-matchers (>= 1.4.1, < 3.0)
|
50
|
+
shoulda-context (1.1.6)
|
51
|
+
shoulda-matchers (2.4.0)
|
52
|
+
activesupport (>= 3.0.0)
|
53
|
+
slop (3.4.7)
|
54
|
+
test-unit (2.5.5)
|
55
|
+
thor (0.18.1)
|
56
|
+
thread_safe (0.1.3)
|
57
|
+
atomic
|
58
|
+
timers (1.1.0)
|
59
|
+
tzinfo (0.3.38)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
bundler (~> 1.3)
|
66
|
+
guard-test
|
67
|
+
rake
|
68
|
+
shoulda
|
69
|
+
worldcatapi!
|
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 tdak
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
worldcat-api
|
2
|
+
============
|
3
|
+
|
4
|
+
The RUBY API to access WorldCat.org webservices. Inspired by original wcapi implementation
|
5
|
+
|
6
|
+
|
7
|
+
# Worldcatapi
|
8
|
+
|
9
|
+
Forked from RubyForge WCAPI project.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'worldcatapi'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install worldcatapi
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/example.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'worldcatapi'
|
3
|
+
|
4
|
+
@key = ENV["WORLDCAT_KEY"]
|
5
|
+
|
6
|
+
puts "KEY=#{@key}"
|
7
|
+
|
8
|
+
client = WORLDCATAPI::Client.new(key: @key, debug: true)
|
9
|
+
|
10
|
+
response = client.OpenSearch(:q=>'building digital libraries', :format=>'atom', :start => '1', :count => '25', :cformat => 'all')
|
11
|
+
|
12
|
+
puts "Total Results: " + response.header["totalResults"]
|
13
|
+
response.records.each {|rec|
|
14
|
+
puts "Title: " + rec[:title] + "\n"
|
15
|
+
puts "URL: " + rec[:link] + "\n"
|
16
|
+
puts "Authors: " + rec[:author].join(" ") + "\n"
|
17
|
+
puts "OCLC #: " + rec[:id] + "\n"
|
18
|
+
puts "Description: " + rec[:summary] + "\n"
|
19
|
+
puts "citation: " + rec[:citation] + "\n"
|
20
|
+
|
21
|
+
}
|
22
|
+
|
23
|
+
puts "\n\n\n"
|
24
|
+
|
25
|
+
puts "Get Record Test: " + "\n\n"
|
26
|
+
record = client.GetRecord(:type => "oclc", :id => "15550774")
|
27
|
+
|
28
|
+
puts record.record[:title] + "\n"
|
29
|
+
puts record.record[:link] + "\n"
|
30
|
+
|
31
|
+
puts "\n\n\n"
|
32
|
+
puts "Get Location Test: " + "\n\n"
|
33
|
+
info = client.GetLocations(:type=>"oclc", :id => "15550774")
|
34
|
+
|
35
|
+
info.institutions.each {|info|
|
36
|
+
puts "Institutional Identifier: " + info[:institutionIdentifier] + "\n"
|
37
|
+
puts "OPAC Link: " + info[:link] + "\n"
|
38
|
+
puts "Copies: " + info[:copies] + "\n\n\n"
|
39
|
+
}
|
40
|
+
|
41
|
+
puts "\n\n\n"
|
42
|
+
puts "Citation Example: " + "\n\n"
|
43
|
+
|
44
|
+
citation = client.GetCitation(:type=>"oclc", :id=>"15550774", :cformat => 'all')
|
45
|
+
puts citation
|
46
|
+
puts "\n\n"
|
47
|
+
|
48
|
+
|
49
|
+
puts "\n\n\n"
|
50
|
+
puts "SRU Search Example: " + "\n\n"
|
51
|
+
records = client.SRUSearch(:query => '"civil war"')
|
52
|
+
puts "Total Records: " + records.header["numberOfRecords"] + "\n"
|
53
|
+
records.records.each {|rec|
|
54
|
+
puts "Title: " + rec[:title] + "\n"
|
55
|
+
puts "URL: " + rec[:link] + "\n\n\n"
|
56
|
+
}
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module WORLDCATAPI
|
6
|
+
|
7
|
+
# The WOLDCATAPI::Client object provides a public facing interface to interacting
|
8
|
+
# with the various WorldCat API Grid Services.
|
9
|
+
#
|
10
|
+
# client = WORLDCATAPI::Client.new :query => 'query', :format => [atom|rss], :start => [position], :count => [max records], :cformat => [mla|apa], :wskey => [your world cat key
|
11
|
+
# options:
|
12
|
+
# wskey
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# More information can be found at:
|
16
|
+
# http://worldcat.org/devnet/wiki/SearchAPIDetails
|
17
|
+
|
18
|
+
class Client
|
19
|
+
|
20
|
+
# The constructor which must be passed a valid base url for an oai
|
21
|
+
# service:
|
22
|
+
#
|
23
|
+
# If you want to see debugging messages on STDERR use:
|
24
|
+
# :debug => true
|
25
|
+
|
26
|
+
def initialize(options={})
|
27
|
+
@debug = options[:debug]
|
28
|
+
@wskey = options[:key]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Equivalent to a Identify request. You'll get back a OAI::IdentifyResponse
|
32
|
+
# object which is essentially just a wrapper around a REXML::Document
|
33
|
+
# for the response.
|
34
|
+
|
35
|
+
def OpenSearch(opts={})
|
36
|
+
@base = URI.parse WORLDCAT_OPENSEARCH
|
37
|
+
opts["wskey"] = @wskey
|
38
|
+
xml = do_request(opts)
|
39
|
+
return OpenSearchResponse.new(xml)
|
40
|
+
end
|
41
|
+
|
42
|
+
def GetRecord(opts={})
|
43
|
+
if opts[:type] == 'oclc'
|
44
|
+
@base = URI.parse "http://www.worldcat.org/webservices/catalog/content/" + opts[:id]
|
45
|
+
else
|
46
|
+
@base = URI.parse 'http://www.worldcat.org/webservices/catalog/content/isbn/' + opts[:id]
|
47
|
+
end
|
48
|
+
opts.delete("type")
|
49
|
+
opts["wskey"] = @wskey
|
50
|
+
xml = do_request(opts)
|
51
|
+
return GetRecordResponse.new(xml)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def GetLocations(opts={})
|
56
|
+
if opts[:type] == 'oclc'
|
57
|
+
@base = URI.parse "http://www.worldcat.org/webservices/catalog/content/libraries/" + opts[:id]
|
58
|
+
else
|
59
|
+
@base = URI.parse 'http://www.worldcat.org/webservices/catalog/content/libraries/isbn/' + opts[:id]
|
60
|
+
end
|
61
|
+
opts.delete("type")
|
62
|
+
opts["wskey"] = @wskey
|
63
|
+
xml = do_request(opts)
|
64
|
+
return GetLocationResponse.new(xml)
|
65
|
+
end
|
66
|
+
|
67
|
+
def GetCitation(opts = {})
|
68
|
+
if opts[:type] == 'oclc'
|
69
|
+
@base = URI.parse "http://www.worldcat.org/webservices/catalog/content/citations/" + opts[:id]
|
70
|
+
else
|
71
|
+
@base = URI.parse 'http://www.worldcat.org/webservices/catalog/content/citations/isbn/' + opts[:id]
|
72
|
+
end
|
73
|
+
opts.delete("type")
|
74
|
+
opts["wskey"] = @wskey
|
75
|
+
xml = do_request(opts)
|
76
|
+
#Returns an HTML representation
|
77
|
+
return xml
|
78
|
+
end
|
79
|
+
|
80
|
+
def SRUSearch(opts={})
|
81
|
+
@base = URI.parse WORLDCAT_SRU
|
82
|
+
opts["wskey"] = @wskey
|
83
|
+
xml = do_request(opts)
|
84
|
+
return SruSearchResponse.new(xml)
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def do_request(hash)
|
91
|
+
uri = @base.clone
|
92
|
+
|
93
|
+
# build up the query string
|
94
|
+
parts = hash.entries.map do |entry|
|
95
|
+
key = studly(entry[0].to_s)
|
96
|
+
value = entry[1]
|
97
|
+
value = CGI.escape(entry[1].to_s)
|
98
|
+
"#{key}=#{value}"
|
99
|
+
end
|
100
|
+
uri.query = parts.join('&')
|
101
|
+
debug("doing request: #{uri.to_s}")
|
102
|
+
|
103
|
+
# fire off the request and return an REXML::Document object
|
104
|
+
begin
|
105
|
+
xml = Net::HTTP.get(uri)
|
106
|
+
debug("got response: #{xml}")
|
107
|
+
return xml
|
108
|
+
rescue SystemCallError=> e
|
109
|
+
#raise WCAPI::Exception, 'HTTP level error during WCAPI request: '+e, caller
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# convert foo_bar to fooBar thus allowing our ruby code to use
|
114
|
+
# the typical underscore idiom
|
115
|
+
def studly(s)
|
116
|
+
s.gsub(/_(\w)/) do |match|
|
117
|
+
match.sub! '_', ''
|
118
|
+
match.upcase
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def debug(msg)
|
123
|
+
$stderr.print("#{msg}\n") if @debug
|
124
|
+
end
|
125
|
+
|
126
|
+
def to_h(default=nil)
|
127
|
+
Hash[ *inject([]) { |a, value| a.push value, default || yield(value) } ]
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module WORLDCATAPI
|
2
|
+
class GetLocationResponse
|
3
|
+
|
4
|
+
attr_accessor :institutions, :raw
|
5
|
+
|
6
|
+
def initialize(doc)
|
7
|
+
#super doc
|
8
|
+
@raw = doc
|
9
|
+
parse_holdings(doc)
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse_holdings(xml)
|
13
|
+
_oclc_symbol = ""
|
14
|
+
_link = ""
|
15
|
+
_copies = ""
|
16
|
+
_xml = xml
|
17
|
+
_instchash = {}
|
18
|
+
_records = Array.new()
|
19
|
+
_x = 0
|
20
|
+
|
21
|
+
doc = Nokogiri::XML(xml)
|
22
|
+
nodes = doc.xpath("//holding")
|
23
|
+
nodes.each { |item |
|
24
|
+
_oclc_symbol = item.xpath("institutionIdentifier/value[position()=1]").text
|
25
|
+
|
26
|
+
if item.xpath("electronicAddress/text") != nil
|
27
|
+
_link = item.xpath("electronicAddress/text[position()=1]").text
|
28
|
+
end
|
29
|
+
|
30
|
+
if item.xpath("holdingSimple/copiesSummary/copiesCount") != nil
|
31
|
+
_copies = item.xpath("holdingSimple/copiesSummary/copiesCount[position()=1]").text
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
_instchash = {:institutionIdentifier => _oclc_symbol, :link => _link, :copies => _copies ,
|
36
|
+
:xml => item.to_s}
|
37
|
+
_records.push(_instchash)
|
38
|
+
}
|
39
|
+
@institutions = _records
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module WORLDCATAPI
|
2
|
+
class GetRecordResponse
|
3
|
+
|
4
|
+
attr_accessor :record, :raw
|
5
|
+
|
6
|
+
def initialize(doc)
|
7
|
+
#super doc
|
8
|
+
@raw = doc
|
9
|
+
parse_marcxml(doc)
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse_marcxml(xml)
|
13
|
+
_title = ""
|
14
|
+
#this is an array
|
15
|
+
_author = Array.new()
|
16
|
+
_link = ""
|
17
|
+
_id = ""
|
18
|
+
_citation = ""
|
19
|
+
_summary = ""
|
20
|
+
_xml = xml
|
21
|
+
_rechash = {}
|
22
|
+
_x = 0
|
23
|
+
|
24
|
+
doc = Nokogiri::XML(xml)
|
25
|
+
doc.remove_namespaces!
|
26
|
+
nodes = doc.xpath("/record")
|
27
|
+
puts "NODE Count: " + nodes.length.to_s
|
28
|
+
nodes.each { |item |
|
29
|
+
_title = item.xpath("datafield[@tag='245']/subfield[@code='a'][position()=1]").text
|
30
|
+
|
31
|
+
if item.xpath("datafield[@tag='1*']") != nil
|
32
|
+
item.xpath("datafield[@tag='1*']").each {|i|
|
33
|
+
_author.push(i.xpath("subfield[@code='a']").text)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
if item.xpath("datafield[@tag='7*']") != nil
|
38
|
+
item.xpath("datafield[@tag='7*']").each {|i|
|
39
|
+
_author.push(i.xpath("subfield[@code='a']").text)
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
if item.xpath("controlfield[@tag='001']") != nil
|
44
|
+
_id = item.xpath("controlfield[@tag='001']").text
|
45
|
+
_link = 'http://www.worldcat.org/oclc/' + _id.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
if item.xpath("datafield[@tag='520']") != nil
|
49
|
+
_summary = item.xpath("datafield[@tag='520']/subfield[@code='a'][position()=1]").text
|
50
|
+
else
|
51
|
+
if item.xpath("datafield[@tag='500']") != nil
|
52
|
+
_summary = item.xpath("datafield[@tag='500']/subfield[@code='a'][position()=1]").text
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
_rechash = {:title => _title, :author => _author, :link => _link, :id => _id, :citation => _citation,
|
57
|
+
:summary => _summary, :xml => item.to_s}
|
58
|
+
}
|
59
|
+
@record = _rechash
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module WORLDCATAPI
|
2
|
+
class OpenSearchResponse
|
3
|
+
|
4
|
+
attr_accessor :header, :records, :raw
|
5
|
+
|
6
|
+
def initialize(doc)
|
7
|
+
#super doc
|
8
|
+
@raw = doc
|
9
|
+
if doc.index('rss')
|
10
|
+
parse_rss(doc)
|
11
|
+
else
|
12
|
+
parse_atom(doc)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse_rss(xml)
|
17
|
+
_title = ""
|
18
|
+
#this is an array
|
19
|
+
_author = Array.new()
|
20
|
+
_link = ""
|
21
|
+
_id = ""
|
22
|
+
_citation = ""
|
23
|
+
_summary = ""
|
24
|
+
_xml = xml
|
25
|
+
_record = Array.new()
|
26
|
+
_x = 0
|
27
|
+
|
28
|
+
doc = Nokogiri::XML(xml)
|
29
|
+
|
30
|
+
namespaces = {'content' => 'http://purl.org/rss/1.0/modules/content/',
|
31
|
+
'atom' => 'http://www.w3.org/2005/Atom',
|
32
|
+
'opensearch' => 'http://a9.com/-/spec/opensearch/1.1/',
|
33
|
+
'srw' => 'http://www.loc.gov/zing/srw/'
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
@header = {}
|
38
|
+
@header["totalResults"] = doc.xpath("//opensearch:totalResults").text
|
39
|
+
@header["startIndex"] = doc.xpath("//opensearch:startIndex").text
|
40
|
+
@header["itemsPerPage"] = doc.xpath("//opensearch:itemsPerPage").text
|
41
|
+
|
42
|
+
nodes = doc.xpath("//item", namespaces)
|
43
|
+
nodes.each { |item |
|
44
|
+
_title = item.xpath("title[position()=1]", namespaces).text
|
45
|
+
if item.xpath("author/name[position()=1]", namespaces) != nil
|
46
|
+
item.xpath("author/name", namespaces).each { |i|
|
47
|
+
_author.push(i.text)
|
48
|
+
}
|
49
|
+
end
|
50
|
+
if item.xpath("link[position()=1]") != nil
|
51
|
+
_link = item.xpath("link[position()=1]", namespaces).text
|
52
|
+
end
|
53
|
+
|
54
|
+
if _link != ''
|
55
|
+
_id = _link.slice(_link.rindex("/")+1, _link.length-_link.rindex("/"))
|
56
|
+
end
|
57
|
+
if item.xpath("content:encoded[position()=1]", namespaces) != nil
|
58
|
+
_citation = item.xpath("content:encoded[position()=1]", namespaces).text
|
59
|
+
end
|
60
|
+
|
61
|
+
if item.xpath("description[position()=1]", namespaces) != nil
|
62
|
+
_summary = item.xpath("description[position()=1]", namespaces).text
|
63
|
+
end
|
64
|
+
_rechash = {:title => _title, :author => _author, :link => _link, :id => _id, :citation => _citation,
|
65
|
+
:summary => _summary, :xml => item.to_s}
|
66
|
+
_record.push(_rechash)
|
67
|
+
}
|
68
|
+
@records = _record
|
69
|
+
end
|
70
|
+
|
71
|
+
def parse_atom(xml)
|
72
|
+
_title = ""
|
73
|
+
#this is an array
|
74
|
+
_author = Array.new()
|
75
|
+
_link = ""
|
76
|
+
_id = ""
|
77
|
+
_citation = ""
|
78
|
+
_summary = ""
|
79
|
+
_xml = xml
|
80
|
+
_record = Array.new()
|
81
|
+
_x = 0
|
82
|
+
|
83
|
+
doc = Nokogiri::XML(xml)
|
84
|
+
namespaces = {'n0' => 'http://www.w3.org/2005/Atom',
|
85
|
+
'opensearch' => 'http://a9.com/-/spec/opensearch/1.1/'
|
86
|
+
}
|
87
|
+
|
88
|
+
|
89
|
+
@header = {}
|
90
|
+
@header["totalResults"] = doc.xpath("//opensearch:totalResults").text
|
91
|
+
@header["startIndex"] = doc.xpath("//opensearch:startIndex").text
|
92
|
+
@header["itemsPerPage"] = doc.xpath("//opensearch:itemsPerPage").text
|
93
|
+
|
94
|
+
nodes = doc.xpath("//*[local-name()='entry']")
|
95
|
+
nodes.each { |item |
|
96
|
+
_author = []
|
97
|
+
_title = item.xpath("*[local-name() = 'title'][position()=1]").text
|
98
|
+
_tmpauthor = item.xpath("*[local-name() = 'author'][position()=1]")
|
99
|
+
|
100
|
+
if _tmpauthor != nil
|
101
|
+
if item.xpath("*[local-name() = 'author']/*[local-name() = 'name']") != nil
|
102
|
+
item.xpath("*[local-name() = 'author']/*[local-name() = 'name']").each { |i|
|
103
|
+
_author.push(i.text)
|
104
|
+
}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
if item.xpath("*[local-name() = 'id']") != nil
|
109
|
+
_link = item.xpath("*[local-name() = 'id']").text
|
110
|
+
end
|
111
|
+
|
112
|
+
if _link != ''
|
113
|
+
_id = _link.slice(_link.rindex("/")+1, _link.length-_link.rindex("/"))
|
114
|
+
end
|
115
|
+
if item.xpath("*[local-name() = 'content']") != nil
|
116
|
+
_citation = item.xpath("*[local-name() = 'content'][position()=1]").text
|
117
|
+
end
|
118
|
+
|
119
|
+
if item.xpath("*[local-name() = 'summary']") != nil
|
120
|
+
_summary = item.xpath("*[local-name() = 'summary'][position()=1]").text
|
121
|
+
end
|
122
|
+
_rechash = {:title => _title, :author => _author, :link => _link, :id => _id, :citation => _citation,
|
123
|
+
:summary => _summary, :xml => item.to_s}
|
124
|
+
_record.push(_rechash)
|
125
|
+
}
|
126
|
+
@records = _record
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module WORLDCATAPI
|
2
|
+
class SruSearchResponse
|
3
|
+
|
4
|
+
attr_accessor :header, :records, :raw
|
5
|
+
|
6
|
+
def initialize(doc)
|
7
|
+
#super doc
|
8
|
+
@raw = doc
|
9
|
+
parse_marcxml(doc)
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse_marcxml(xml)
|
13
|
+
@header = {}
|
14
|
+
_title = ""
|
15
|
+
#this is an array
|
16
|
+
_author = Array.new()
|
17
|
+
_link = ""
|
18
|
+
_id = ""
|
19
|
+
_citation = ""
|
20
|
+
_summary = ""
|
21
|
+
_xml = xml
|
22
|
+
_rechash = {}
|
23
|
+
_records = Array.new()
|
24
|
+
_x = 0
|
25
|
+
|
26
|
+
xml = xml.gsub('<?xml-stylesheet type="text/xsl" href="/webservices/catalog/xsl/searchRetrieveResponse.xsl"?>', "")
|
27
|
+
|
28
|
+
doc = Nokogiri::XML(xml)
|
29
|
+
doc.remove_namespaces!
|
30
|
+
@header["numberOfRecords"] = doc.xpath("//numberOfRecords").text
|
31
|
+
@header["recordSchema"] = doc.xpath("//recordSchema").text
|
32
|
+
@header["nextRecordPosition"] = doc.xpath("//nextRecordPosition").text
|
33
|
+
@header["maxiumumRecords"] = doc.xpath("//maximumRecords").text
|
34
|
+
@header["startRecord"] = doc.xpath("//startRecord").text
|
35
|
+
|
36
|
+
nodes = doc.xpath("//records/record/recordData/record")
|
37
|
+
nodes.each { |item |
|
38
|
+
_title = item.xpath("datafield[@tag='245']/subfield[@code='a'][position()=1]").text
|
39
|
+
if item.xpath("datafield[@tag='1*']") != nil
|
40
|
+
item.xpath("datafield[@tag='1*']/sufield[@code='a']").each { |i|
|
41
|
+
_author.push(i.text)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
if item.xpath("datafield[@tag='7*']") != nil
|
45
|
+
item.xpath("datafield[@tag='7*']/sufield[@code='a']").each { |i|
|
46
|
+
_author.push(i.text)
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
if item.xpath("controlfield[@tag='001']") != nil
|
51
|
+
_id = item.xpath("controlfield[@tag='001'][position()=1]").text
|
52
|
+
_link = 'http://www.worldcat.org/oclc/' + _id.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
if item.xpath("datafield[@tag='520']") != nil
|
56
|
+
_summary = item.xpath("datafield[@tag='520']/subfield[@code='a'][position()=1]").text
|
57
|
+
else
|
58
|
+
if item.xpath("datafield[@tag='500']") != nil
|
59
|
+
_summary = item.xpath("datafield[@tag='500']/subfield[@code='a'][position()=1]").text
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
_rechash = {:title => _title, :author => _author, :link => _link, :id => _id, :citation => _citation,
|
64
|
+
:summary => _summary, :xml => item.to_s}
|
65
|
+
_records.push(_rechash)
|
66
|
+
}
|
67
|
+
@records = _records
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
data/lib/worldcatapi.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "worldcatapi/version"
|
2
|
+
require 'worldcatapi/client'
|
3
|
+
require 'worldcatapi/open_search_response'
|
4
|
+
require 'worldcatapi/get_record_response'
|
5
|
+
require 'worldcatapi/get_location_response'
|
6
|
+
require 'worldcatapi/sru_search_response'
|
7
|
+
require 'nokogiri'
|
8
|
+
|
9
|
+
WORLDCAT_OPENSEARCH = 'http://www.worldcat.org/webservices/catalog/search/opensearch'
|
10
|
+
WORLDCAT_SRU = 'http://www.worldcat.org/webservices/catalog/search/sru'
|
11
|
+
|
12
|
+
|
13
|
+
module WOLDCATAPI
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
data/test/test_helper.rb
ADDED
data/worldcatapi.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'worldcatapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "worldcatapi"
|
8
|
+
spec.version = WORLDCATAPI::VERSION
|
9
|
+
spec.authors = ["Taras Danylak", "Terry Reese"]
|
10
|
+
spec.email = ["taras@danylak.com", 'terry.reese@oregonstate.edu']
|
11
|
+
spec.description = %q{Ruby API to access WorldCat Open Search webservice. Based on WCAPI Gem.}
|
12
|
+
spec.summary = %q{Ruby API to access WorldCat Open Search webserbice. Based on WCAPI Gem.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "nokogiri", "~>1.5.7"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: worldcatapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Taras Danylak
|
8
|
+
- Terry Reese
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-02-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 1.5.7
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 1.5.7
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.3'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.3'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Ruby API to access WorldCat Open Search webservice. Based on WCAPI Gem.
|
57
|
+
email:
|
58
|
+
- taras@danylak.com
|
59
|
+
- terry.reese@oregonstate.edu
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- Guardfile
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- example.rb
|
72
|
+
- lib/worldcatapi.rb
|
73
|
+
- lib/worldcatapi/client.rb
|
74
|
+
- lib/worldcatapi/get_location_response.rb
|
75
|
+
- lib/worldcatapi/get_record_response.rb
|
76
|
+
- lib/worldcatapi/open_search_response.rb
|
77
|
+
- lib/worldcatapi/record.rb
|
78
|
+
- lib/worldcatapi/sru_search_response.rb
|
79
|
+
- lib/worldcatapi/version.rb
|
80
|
+
- test/lib/worldcatapi_test.rb
|
81
|
+
- test/test_helper.rb
|
82
|
+
- worldcatapi.gemspec
|
83
|
+
homepage: ''
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.1.5
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Ruby API to access WorldCat Open Search webserbice. Based on WCAPI Gem.
|
107
|
+
test_files:
|
108
|
+
- test/lib/worldcatapi_test.rb
|
109
|
+
- test/test_helper.rb
|