xisbn 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xisbn.rb +61 -18
- data/{test.rb → test/xisbn_test.rb} +35 -23
- metadata +20 -31
- data/lib/.svn/README.txt +0 -2
- data/lib/.svn/dir-wcprops +0 -5
- data/lib/.svn/empty-file +0 -0
- data/lib/.svn/entries +0 -22
- data/lib/.svn/format +0 -1
- data/lib/.svn/prop-base/xisbn.rb.svn-base +0 -1
- data/lib/.svn/props/xisbn.rb.svn-work +0 -1
- data/lib/.svn/text-base/xisbn.rb.svn-base +0 -18
- data/lib/.svn/wcprops/xisbn.rb.svn-work +0 -5
data/lib/xisbn.rb
CHANGED
@@ -1,18 +1,61 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'rexml/document'
|
3
|
-
|
4
|
-
#
|
5
|
-
# as defined by the OCLC xisbn webservice at
|
6
|
-
# http://www.oclc.org/research/projects/xisbn/
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
1
|
+
require 'net/http'
|
2
|
+
require 'rexml/document'
|
3
|
+
|
4
|
+
# Pass in an ISBN and get back a list of related isbns
|
5
|
+
# as defined by the OCLC xisbn webservice at
|
6
|
+
# http://www.oclc.org/research/projects/xisbn/
|
7
|
+
#
|
8
|
+
# require 'xisbn'
|
9
|
+
# include XISBN
|
10
|
+
# isbns = xisbn('0-9745140-8-X')
|
11
|
+
#
|
12
|
+
# If you want to guard against timeouts pass in the number
|
13
|
+
# of seconds you don't want to wait longer than:
|
14
|
+
#
|
15
|
+
# isbns = xisbn('0-94745140-8-X', :timeout => 1)
|
16
|
+
#
|
17
|
+
# If you want to use LibraryThing's xisbn service:
|
18
|
+
#
|
19
|
+
# isbns = thing_isbn('0-9745140-8-X')
|
20
|
+
|
21
|
+
module XISBN
|
22
|
+
require 'uri'
|
23
|
+
@@oclc_uri = URI.parse('http://labs.oclc.org/xisbn')
|
24
|
+
@@thing_uri = URI.parse('http://www.librarything.com/api/thingISBN')
|
25
|
+
|
26
|
+
def xisbn(isbn, opts={})
|
27
|
+
return get_isbns(@@oclc_uri, isbn, opts)
|
28
|
+
end
|
29
|
+
|
30
|
+
def thing_isbn(isbn, opts={})
|
31
|
+
return get_isbns(@@thing_uri, isbn, opts)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def get_isbns(uri, isbn, opts={})
|
37
|
+
timeout = opts[:timeout] || 60
|
38
|
+
redirects = opts[:redirects] || 10
|
39
|
+
clean_isbn = isbn.gsub(/[^0-9X]/,'')
|
40
|
+
|
41
|
+
# base case for http redirect recursion
|
42
|
+
raise ArgumentError, 'Too many redirects' if redirects == 0
|
43
|
+
|
44
|
+
http = Net::HTTP.new(uri.host)
|
45
|
+
http.read_timeout = timeout
|
46
|
+
http.open_timeout = timeout
|
47
|
+
response = http.get("#{uri.path}/#{clean_isbn}")
|
48
|
+
|
49
|
+
# follow HTTP redirects
|
50
|
+
if response.kind_of?(Net::HTTPRedirection)
|
51
|
+
return get_isbns(URI.parse(response['location']), isbn,
|
52
|
+
{:timeout => timeout, :redirects => (redirects-1)})
|
53
|
+
end
|
54
|
+
|
55
|
+
isbns = []
|
56
|
+
doc = REXML::Document.new(response.body)
|
57
|
+
doc.elements.each('idlist/isbn') {|e| isbns << e.text}
|
58
|
+
return isbns
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -1,23 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift 'lib'
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'xisbn'
|
7
|
+
include XISBN
|
8
|
+
|
9
|
+
class XISBNTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_lookup
|
12
|
+
isbns = xisbn('0192816640')
|
13
|
+
assert isbns.length > 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_lookup_with_dashes
|
17
|
+
isbns = xisbn('01928-16-640')
|
18
|
+
assert isbns.length > 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_bad_lookup
|
22
|
+
isbns = xisbn('foobar')
|
23
|
+
assert isbns.length == 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_timeout
|
27
|
+
isbns = xisbn('foobar', :timeout=>2)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_thing_isbn
|
31
|
+
isbns = thing_isbn('01928-16-640')
|
32
|
+
assert isbns.length > 0
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.0
|
3
3
|
specification_version: 1
|
4
4
|
name: xisbn
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date:
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2007-02-14 00:00:00 -05:00
|
8
8
|
summary: look up related isbns with OCLCs xisbn service
|
9
9
|
require_paths:
|
10
|
-
|
10
|
+
- lib
|
11
11
|
email: ehs@pobox.com
|
12
12
|
homepage: http://www.textualize.com/xisbn
|
13
13
|
rubyforge_project:
|
@@ -18,41 +18,30 @@ bindir: bin
|
|
18
18
|
has_rdoc: true
|
19
19
|
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
20
|
requirements:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
version: 0.0.0
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
25
24
|
version:
|
26
25
|
platform: ruby
|
27
26
|
signing_key:
|
28
27
|
cert_chain:
|
28
|
+
post_install_message:
|
29
29
|
authors:
|
30
|
-
|
30
|
+
- Ed Summers
|
31
31
|
files:
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
- lib/.svn/format
|
37
|
-
- lib/.svn/prop-base
|
38
|
-
- lib/.svn/props
|
39
|
-
- lib/.svn/README.txt
|
40
|
-
- lib/.svn/text-base
|
41
|
-
- lib/.svn/tmp
|
42
|
-
- lib/.svn/wcprops
|
43
|
-
- lib/.svn/prop-base/xisbn.rb.svn-base
|
44
|
-
- lib/.svn/props/xisbn.rb.svn-work
|
45
|
-
- lib/.svn/text-base/xisbn.rb.svn-base
|
46
|
-
- lib/.svn/tmp/prop-base
|
47
|
-
- lib/.svn/tmp/props
|
48
|
-
- lib/.svn/tmp/text-base
|
49
|
-
- lib/.svn/tmp/wcprops
|
50
|
-
- lib/.svn/wcprops/xisbn.rb.svn-work
|
51
|
-
test_files:
|
52
|
-
- test.rb
|
32
|
+
- lib/xisbn.rb
|
33
|
+
- test/xisbn_test.rb
|
34
|
+
test_files: []
|
35
|
+
|
53
36
|
rdoc_options: []
|
37
|
+
|
54
38
|
extra_rdoc_files: []
|
39
|
+
|
55
40
|
executables: []
|
41
|
+
|
56
42
|
extensions: []
|
43
|
+
|
57
44
|
requirements: []
|
58
|
-
|
45
|
+
|
46
|
+
dependencies: []
|
47
|
+
|
data/lib/.svn/README.txt
DELETED
data/lib/.svn/dir-wcprops
DELETED
data/lib/.svn/empty-file
DELETED
File without changes
|
data/lib/.svn/entries
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
2
|
-
<wc-entries
|
3
|
-
xmlns="svn:">
|
4
|
-
<entry
|
5
|
-
committed-rev="372"
|
6
|
-
name=""
|
7
|
-
committed-date="2006-02-28T06:34:26.176837Z"
|
8
|
-
url="http://www.textualize.com/svn/xisbn/trunk/lib"
|
9
|
-
last-author="ed"
|
10
|
-
kind="dir"
|
11
|
-
uuid="4dc5e89f-90f6-0310-ab54-a6a856e7c30e"
|
12
|
-
revision="372"/>
|
13
|
-
<entry
|
14
|
-
committed-rev="372"
|
15
|
-
name="xisbn.rb"
|
16
|
-
text-time="2006-02-28T15:20:47.000000Z"
|
17
|
-
committed-date="2006-02-28T06:34:26.176837Z"
|
18
|
-
checksum="2b6d73e6e65a5c3ab351fe1b4040693e"
|
19
|
-
last-author="ed"
|
20
|
-
kind="file"
|
21
|
-
prop-time="2006-02-28T15:20:47.000000Z"/>
|
22
|
-
</wc-entries>
|
data/lib/.svn/format
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
4
|
@@ -1 +0,0 @@
|
|
1
|
-
END
|
@@ -1 +0,0 @@
|
|
1
|
-
END
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'rexml/document'
|
3
|
-
|
4
|
-
# pass in an ISBN and get back a list of related isbns
|
5
|
-
# as defined by the OCLC xisbn webservice at
|
6
|
-
# http://www.oclc.org/research/projects/xisbn/
|
7
|
-
|
8
|
-
def xisbn(isbn)
|
9
|
-
clean_isbn = isbn.gsub(/[^0-9X]/,'')
|
10
|
-
|
11
|
-
xml = Net::HTTP.get('labs.oclc.org', "/xisbn/#{clean_isbn}")
|
12
|
-
doc = REXML::Document.new(xml)
|
13
|
-
|
14
|
-
isbns = []
|
15
|
-
doc.elements.each('idlist/isbn') {|e| isbns << e.text}
|
16
|
-
|
17
|
-
return isbns
|
18
|
-
end
|