Module: ISBN
- Extended by:
- ISBN
- Included in:
- ISBN
- Defined in:
- lib/isbn.rb
Defined Under Namespace
Classes: Invalid10DigitISBN, Invalid13DigitISBN, InvalidISBNError, InvalidSourceString, No10DigitISBNAvailable
Instance Method Summary
collapse
Instance Method Details
#as_new(isbn) ⇒ Object
Also known as:
unused
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/isbn.rb', line 49
def as_new(isbn)
case isbn.size
when 13
case isbn
when /^978/ then isbn
when /^290/ then thirteen("978#{isbn[3..-1]}")
when /^979/ then isbn
when /^291/ then thirteen("979#{isbn[3..-1]}")
end
when 10 then ten(isbn)
else raise ISBN::InvalidISBNError
end
end
|
#as_used(isbn) ⇒ Object
Also known as:
used
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/isbn.rb', line 34
def as_used(isbn)
case isbn.size
when 13
case isbn
when /^978/ then thirteen("290#{isbn[3..-1]}")
when /^290/ then isbn
when /^979/ then thirteen("291#{isbn[3..-1]}")
when /^291/ then isbn
end
when 10 then thirteen("290#{isbn}")
else raise ISBN::InvalidISBNError
end
end
|
#from_image(url) ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/isbn.rb', line 75
def from_image(url)
require "open-uri"
require "tempfile"
tmp = Tempfile.new("tmp")
tmp.write(open(url, "rb:binary").read)
tmp.close
isbn = %x{djpeg -pnm #{tmp.path} | gocr -}
isbn.strip.gsub(" ", "").gsub(/o/i, "0").gsub("_", "2").gsub(/2J$/, "45")
end
|
#from_string(source) ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/isbn.rb', line 85
def from_string(source)
regex = /(?:ISBN[- ]*13|ISBN[- ]*10|)\s*((?:(?:97[89])?[ -]?(?:[0-9][ -]*){9})[ -]*(?:[0-9xX]))/
match = source.scan(regex).flatten
match.map! { |i| i.gsub(/[\s-]+/, "-") }
match = match.find {|i| ISBN.valid?(i) }
raise InvalidSourceString unless match
match
end
|
#ten(isbn) ⇒ Object
Also known as:
as_ten
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/isbn.rb', line 4
def ten(isbn)
raise InvalidISBNError unless isbn.is_a? String
isbn = isbn.delete("-")
raise No10DigitISBNAvailable if isbn =~ /^979/
case isbn.size
when 10 then isbn = isbn[0..8]
when 13 then isbn = isbn[/(?:^978|^290)*(.{9})\w/,1]
else raise Invalid10DigitISBN
end
case ck = (11 - (isbn.split(//).zip((2..10).to_a.reverse).inject(0) {|s,n| s += n[0].to_i * n[1]} % 11))
when 10 then isbn << "X"
when 11 then isbn << "0"
else isbn << ck.to_s
end
end
|
#thirteen(isbn) ⇒ Object
Also known as:
as_thirteen
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/isbn.rb', line 21
def thirteen(isbn)
raise InvalidISBNError unless isbn.is_a? String
isbn = isbn.delete("-")
raise Invalid13DigitISBN unless (isbn.size == 10 || isbn.size == 13)
isbn = isbn.rjust(13,"978")[/(.+)\w/,1] raise Invalid13DigitISBN unless isbn.size == 12 case ck = (10 - (isbn.split(//).zip([1,3]*6).inject(0) {|s,n| s += n[0].to_i * n[1]} % 10))
when 10 then isbn << "0"
else isbn << ck.to_s
end
end
|
#valid?(isbn) ⇒ Boolean
65
66
67
68
69
70
71
72
73
|
# File 'lib/isbn.rb', line 65
def valid?(isbn)
return false unless isbn.is_a?(String)
isbn = isbn.delete("-")
case isbn.size
when 13 then isbn[-1] == thirteen(isbn)[-1]
when 10 then isbn[-1].upcase == ten(isbn)[-1]
else false
end
end
|
#with_dashes(isbn) ⇒ Object
94
95
96
97
98
99
|
# File 'lib/isbn.rb', line 94
def with_dashes(isbn)
s = isbn.gsub("-", "")
return s.scan(/(...)(.)(....)(....)(.)/).join('-') if s.size == 13
return s.scan(/(.)(....)(....)(.)/).join('-') if s.size == 10
raise InvalidSourceString
end
|