mroch-campusbooks 0.1.0 → 0.2.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.
- data/VERSION.yml +1 -1
- metadata +12 -6
- data/lib/isbn/LICENCE +0 -20
- data/lib/isbn/README +0 -60
- data/lib/isbn/tools.rb +0 -173
data/VERSION.yml
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mroch-campusbooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marshall Roch
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,6 +22,16 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: isbn-tools
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
25
35
|
description: A Ruby library for accessing the CampusBooks.com API
|
26
36
|
email: marshall@mroch.com
|
27
37
|
executables: []
|
@@ -39,10 +49,6 @@ files:
|
|
39
49
|
- lib/campusbooks/book.rb
|
40
50
|
- lib/campusbooks/offer.rb
|
41
51
|
- lib/campusbooks.rb
|
42
|
-
- lib/isbn
|
43
|
-
- lib/isbn/LICENCE
|
44
|
-
- lib/isbn/README
|
45
|
-
- lib/isbn/tools.rb
|
46
52
|
- test/base_test.rb
|
47
53
|
- test/book_test.rb
|
48
54
|
- test/offer_test.rb
|
data/lib/isbn/LICENCE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright 2006, Thierry Godfroid
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
5
|
-
in the Software without restriction, including without limitation the rights
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
8
|
-
furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
* The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
|
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 IMPLIED,
|
16
|
-
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
17
|
-
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
18
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/isbn/README
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
== ISBN-Tools for Ruby
|
2
|
-
|
3
|
-
This library provides the ability to manipulate ISBN numbers.
|
4
|
-
It knows about ISBN-10 and ISBN-13 numbers, has the ability to
|
5
|
-
check if they are valid and convert from one format to the other.
|
6
|
-
It can, of course, compute the check digit of both formats.
|
7
|
-
|
8
|
-
Finally, it has the ability to hyphenate ISBN numbers for the
|
9
|
-
following group identifiers: 0, 1 and 2. Note that only hyphenation
|
10
|
-
methods need to know about ranges, so all others methods (validity,
|
11
|
-
checksum computations and number conversions) can be used with ISBN
|
12
|
-
numbers from any group.
|
13
|
-
|
14
|
-
Other ranges could be added on request but I would need samples
|
15
|
-
ISBN to check the result.
|
16
|
-
|
17
|
-
== Usage
|
18
|
-
|
19
|
-
require 'rubygems'
|
20
|
-
require 'isbn/tools'
|
21
|
-
|
22
|
-
isbn_good = "2800107766"
|
23
|
-
isbn_bad = "2810107766"
|
24
|
-
|
25
|
-
def check_and_hyphenate(isbn)
|
26
|
-
if ISBN_Tools.is_valid?(isbn)
|
27
|
-
puts ISBN_Tools.hyphenate(isbn)
|
28
|
-
else
|
29
|
-
cksum = ISBN_Tools.compute_check_digit(isbn)
|
30
|
-
puts "Invalid ISBN number [#{isbn}]. Checksum should be #{cksum}"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
check_and_hyphenate isbn_good # => 2-8001-0776-6
|
35
|
-
check_and_hyphenate isbn_bad # => Invalid ISBN number [2810107766]. Checksum should be 9
|
36
|
-
|
37
|
-
== Copyright
|
38
|
-
|
39
|
-
Copyright:: 2006, Thierry Godfroid
|
40
|
-
|
41
|
-
The following sources were used in order to understand ISBN numbers and
|
42
|
-
how to manipulate them. No books were harmed in the process.
|
43
|
-
- http://www.isbn-international.org
|
44
|
-
- "Are You Ready for ISBN-13?" http://www.isbn.org/standards/home/isbn/transition.asp. Note that at the bottom of this page, you can find a link towards a small book "ISBN-13 for Dummies", available as PDF (http://www.bisg.org/isbn-13/ISBN13_For_Dummies.pdf)
|
45
|
-
- Structure of an ISBN number http://www.isbn.org/standards/home/isbn/international/html/usm4.htm
|
46
|
-
|
47
|
-
Ranges information was found at http://www.isbn-international.org/en/identifiers.html.
|
48
|
-
|
49
|
-
== LICENCE NOTE
|
50
|
-
|
51
|
-
MIT-Style license. See LICENCE file in this distribution.
|
52
|
-
|
53
|
-
== Requirements
|
54
|
-
|
55
|
-
ISBN-Tools requires Ruby 1.8.2 or better.
|
56
|
-
|
57
|
-
== Known bugs/Limitations
|
58
|
-
|
59
|
-
- This release code only allows for one-digit groups.
|
60
|
-
- See also the TODO file in this distribution
|
data/lib/isbn/tools.rb
DELETED
@@ -1,173 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright 2006, Thierry Godfroid
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
-
# of this software and associated documentation files (the "Software"), to deal
|
6
|
-
# in the Software without restriction, including without limitation the rights
|
7
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
-
# copies of the Software, and to permit persons to whom the Software is
|
9
|
-
# furnished to do so, subject to the following conditions:
|
10
|
-
#
|
11
|
-
# * The name of the author may not be used to endorse or promote products derived
|
12
|
-
# from this software without specific prior written permission.
|
13
|
-
#
|
14
|
-
# The above copyright notice and this permission notice shall be included in all
|
15
|
-
# copies or substantial portions of the Software.
|
16
|
-
#
|
17
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
18
|
-
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
19
|
-
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20
|
-
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
-
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
-
#++
|
24
|
-
|
25
|
-
# This module provides all the methods of the ISBN-Tools library.
|
26
|
-
# Methods have no state but the library reads the file data/ranges.dat
|
27
|
-
# and fills up the RNG hash when loaded.
|
28
|
-
module ISBN_Tools
|
29
|
-
# Supported groups and associated ranges. Data is read from data/ranges.dat
|
30
|
-
# (provided in gem) at module load.
|
31
|
-
RNG = {}
|
32
|
-
|
33
|
-
File.open(File.join(File.dirname(__FILE__), "../../data/ranges.dat")) do |file|
|
34
|
-
file.each do |line|
|
35
|
-
line.chomp!
|
36
|
-
break if line.empty?
|
37
|
-
ar_line = line.split(/,/)
|
38
|
-
ndx = ar_line.delete_at(0)
|
39
|
-
RNG[ndx] = []
|
40
|
-
ar_line.each { |item|
|
41
|
-
r = item.split(/\.\./)
|
42
|
-
RNG[ndx].push(Range.new(r[0],r[1]))
|
43
|
-
}
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# Clear all useless characters from an ISBN number and upcase the 'X' sign when
|
48
|
-
# present. Also does the basic check that 'X' must be the last sign of the number,
|
49
|
-
# if present. Returns nil if provided string is nil or X is not at the last position.
|
50
|
-
#
|
51
|
-
# No length check is done: no matter what string is passed in, all characters that
|
52
|
-
# not in the range [0-9xX] are removed.
|
53
|
-
def ISBN_Tools.cleanup(isbn_)
|
54
|
-
isbn_.gsub(/[^0-9xX]/,'').gsub(/x/,'X') unless isbn_.nil? or isbn_.scan(/([xX])/).length > 1
|
55
|
-
end
|
56
|
-
|
57
|
-
# Same as cleanup but alters the argument.
|
58
|
-
def ISBN_Tools.cleanup!(isbn_)
|
59
|
-
isbn_.replace(cleanup(isbn_))
|
60
|
-
end
|
61
|
-
|
62
|
-
# Check that the value is a valid ISBN-10 number. Returns true if it is, false otherwise.
|
63
|
-
# The method will check that the number is exactly 10 digits long and that the tenth digit is
|
64
|
-
# the correct checksum for the number.
|
65
|
-
def ISBN_Tools.is_valid_isbn10?(isbn_)
|
66
|
-
isbn = cleanup(isbn_)
|
67
|
-
return false if isbn.nil? or isbn.match(/^[0-9]{9}[0-9X]$/).nil?
|
68
|
-
sum = 0;
|
69
|
-
0.upto(9) { |ndx| sum += (isbn[ndx]!= 88 ? isbn[ndx].chr.to_i : 10) * (10-ndx) } # 88 is ascii of X
|
70
|
-
sum % 11 == 0
|
71
|
-
end
|
72
|
-
|
73
|
-
# Check that the value is a valid ISBN-13 number. Returns true if it is, false otherwise.
|
74
|
-
# The method will check that the number is exactly 13 digits long and that the thirteenth digit is
|
75
|
-
# the correct checksum for the number.
|
76
|
-
def ISBN_Tools.is_valid_isbn13?(isbn_)
|
77
|
-
isbn = cleanup(isbn_)
|
78
|
-
return false if isbn.nil? or isbn.length!=13 or isbn.match(/^97[8|9][0-9]{10}$/).nil?
|
79
|
-
sum = 0
|
80
|
-
0.upto(12) { |ndx| sum += isbn[ndx].chr.to_i * (ndx % 2 == 0 ? 1 : 3) }
|
81
|
-
sum.remainder(10) == 0
|
82
|
-
end
|
83
|
-
|
84
|
-
# Check that an ISBN is valid or not. Returns true if is, false otherwise. This method will
|
85
|
-
# first call is_valid_isbn10() and, on failure, try is_valid_isbn13(). Returns true if it is
|
86
|
-
# a valid number, false otherwise.
|
87
|
-
# This method is handy if you don't want to be bothered by checking the length of your
|
88
|
-
# isbn before checking its validity. It is a bit slower since cleanup will be called twice.
|
89
|
-
def ISBN_Tools.is_valid?(isbn_)
|
90
|
-
is_valid_isbn10?(isbn_) || is_valid_isbn13?(isbn_)
|
91
|
-
end
|
92
|
-
|
93
|
-
# Computes the check digit of an ISBN-10 number. It will ignore the tenth sign if present
|
94
|
-
# and accepts a number with only 9 digits. Returns the checksum digit or nil. Please note
|
95
|
-
# that the checksum digit of an ISBN-10 may be the character 'X'.
|
96
|
-
def ISBN_Tools.compute_isbn10_check_digit(isbn_)
|
97
|
-
isbn = cleanup(isbn_)
|
98
|
-
return nil if isbn.nil? or isbn.length > 10 or isbn.length < 9
|
99
|
-
sum = 0;
|
100
|
-
0.upto(8) { |ndx| sum += isbn[ndx].chr.to_i * (10-ndx) }
|
101
|
-
(11-sum) % 11 == 10 ? "X" : ((11-sum) % 11).to_s
|
102
|
-
end
|
103
|
-
|
104
|
-
# Computes the check digit of an ISBN-13 number. It will ignore the thirteenth sign if present
|
105
|
-
# and accepts a number with only 12 digits. Returns the checksum digit or nil. Please note
|
106
|
-
# that the checksum digit of an ISBN-13 is always in the range [0-9].
|
107
|
-
def ISBN_Tools.compute_isbn13_check_digit(isbn_)
|
108
|
-
isbn = cleanup(isbn_)
|
109
|
-
return nil if isbn.nil? or isbn.length > 13 or isbn.length < 12
|
110
|
-
sum = 0
|
111
|
-
0.upto(11) { |ndx| sum += isbn[ndx].chr.to_i * (ndx % 2 == 0 ? 1 : 3) }
|
112
|
-
(10-sum.remainder(10)) == 10 ? "0" : (10-sum.remainder(10)).to_s
|
113
|
-
end
|
114
|
-
|
115
|
-
# Compute the check digit of an ISBN number. Try as an ISBN-10 number
|
116
|
-
# first, and if it failed, as an ISBN-13 number. Returns the check digit or
|
117
|
-
# nil if a processing error occured.
|
118
|
-
# This method is a helper for compute_isbn10_check_digit and
|
119
|
-
# compute_isbn13_check_digit.
|
120
|
-
def ISBN_Tools.compute_check_digit(isbn_)
|
121
|
-
compute_isbn10_check_digit(isbn_) || compute_isbn13_check_digit(isbn_)
|
122
|
-
end
|
123
|
-
|
124
|
-
# Convert an ISBN-10 number to its equivalent ISBN-13 number. Returns the converted number or nil
|
125
|
-
# if the provided ISBN-10 number is nil or non valid.
|
126
|
-
def ISBN_Tools.isbn10_to_isbn13(isbn_)
|
127
|
-
isbn = cleanup(isbn_)
|
128
|
-
"978" + isbn[0..8] + compute_isbn13_check_digit("978" + isbn[0..8]) unless isbn.nil? or ! is_valid_isbn10?(isbn)
|
129
|
-
end
|
130
|
-
|
131
|
-
# Convert an ISBN-13 number to its equivalent ISBN-10 number. Returns the converted number or nil
|
132
|
-
# if the provided ISBN-13 number is nil or non valid. Please note that only ISBN-13 numbers starting
|
133
|
-
# with 978 can be converted.
|
134
|
-
def ISBN_Tools.isbn13_to_isbn10(isbn_)
|
135
|
-
isbn = cleanup(isbn_)
|
136
|
-
isbn[3..11] + compute_isbn10_check_digit(isbn[3..11]) unless isbn.nil? or ! is_valid_isbn13?(isbn) or ! isbn_.match(/^978.*/)
|
137
|
-
end
|
138
|
-
|
139
|
-
# Hyphenate a valid ISBN-10 number. Returns nil if the number is invalid or if the group range is
|
140
|
-
# unknown. Works only for groups 0,1 and 2.
|
141
|
-
def ISBN_Tools.hyphenate_isbn10(isbn_)
|
142
|
-
isbn = cleanup(isbn_)
|
143
|
-
group = isbn[0..0]
|
144
|
-
if RNG.has_key?(group) and is_valid_isbn10?(isbn)
|
145
|
-
RNG[group].each { |r| return isbn.sub(Regexp.new("(.{1})(.{#{r.last.length}})(.{#{8-r.last.length}})(.)"),'\1-\2-\3-\4') if r.member?(isbn[1..r.last.length]) }
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
# Hyphenate a valid ISBN-13 number. Returns nil if the number is invalid or if the group range is
|
150
|
-
# unknown. Works only for groups 0,1 and 2.
|
151
|
-
def ISBN_Tools.hyphenate_isbn13(isbn_)
|
152
|
-
isbn = cleanup(isbn_)
|
153
|
-
if is_valid_isbn13?(isbn)
|
154
|
-
group = isbn[3..3]
|
155
|
-
if RNG.has_key?(group)
|
156
|
-
RNG[group].each { |r| return isbn.sub(Regexp.new("(.{3})(.{1})(.{#{r.last.length}})(.{#{8-r.last.length}})(.)"),'\1-\2-\3-\4-\5') if r.member?(isbn[1..r.last.length]) }
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
# This method takes an ISBN then tries to hyphenate it as an ISBN 10 then an ISBN 13. A bit slower
|
162
|
-
# than calling the right one directly but saves you the length checking. Returns an hyphenated value
|
163
|
-
# or nil.
|
164
|
-
def ISBN_Tools.hyphenate(isbn_)
|
165
|
-
hyphenate_isbn10(isbn_) || hyphenate_isbn13(isbn_)
|
166
|
-
end
|
167
|
-
|
168
|
-
# Same as hyphenate() but alters the argument.
|
169
|
-
def ISBN_Tools.hyphenate!(isbn_)
|
170
|
-
isbn_.replace(hyphenate(isbn_))
|
171
|
-
end
|
172
|
-
|
173
|
-
end
|