rbook 0.4.3 → 0.5
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/Rakefile +13 -176
- metadata +57 -117
- data/COPYING +0 -340
- data/LICENSE +0 -13
- data/README +0 -17
- data/examples/gbip.rb +0 -12
- data/examples/onix/stream_reader.rb +0 -13
- data/examples/pacstream.rb +0 -13
- data/examples/titlepage.rb +0 -14
- data/examples/titlepage_with_proxy.rb +0 -14
- data/examples/titlepage_www.rb +0 -18
- data/examples/www/find_all.rb +0 -23
- data/examples/www/find_cover_from_amazon.rb +0 -16
- data/examples/www/find_url_from_rainbow.rb +0 -12
- data/examples/www/list.rb +0 -13
- data/lib/rbook/bisac.rb +0 -31
- data/lib/rbook/bisac/message.rb +0 -99
- data/lib/rbook/bisac/po.rb +0 -97
- data/lib/rbook/bisac/po_line_item.rb +0 -33
- data/lib/rbook/bisac/product.rb +0 -176
- data/lib/rbook/errors.rb +0 -8
- data/lib/rbook/gbip.rb +0 -21
- data/lib/rbook/gbip/pos.rb +0 -118
- data/lib/rbook/gbip/title.rb +0 -36
- data/lib/rbook/gbip/warehouse.rb +0 -27
- data/lib/rbook/isbn.rb +0 -255
- data/lib/rbook/onix.rb +0 -70
- data/lib/rbook/onix/contributor.rb +0 -60
- data/lib/rbook/onix/lists.rb +0 -2
- data/lib/rbook/onix/lists/contributor_role.rb +0 -10
- data/lib/rbook/onix/lists/product_form.rb +0 -100
- data/lib/rbook/onix/message.rb +0 -112
- data/lib/rbook/onix/product.rb +0 -189
- data/lib/rbook/onix/sales_restriction.rb +0 -51
- data/lib/rbook/onix/stream_reader.rb +0 -120
- data/lib/rbook/onix/stream_writer.rb +0 -40
- data/lib/rbook/onix/supply_detail.rb +0 -68
- data/lib/rbook/onix/xchar.rb +0 -98
- data/lib/rbook/pacstream.rb +0 -64
- data/lib/rbook/titlepage.rb +0 -37
- data/lib/rbook/titlepage/client.rb +0 -126
- data/lib/rbook/titlepage/titlepage_driver.rb +0 -137
- data/lib/rbook/titlepage/titlepage_utils.rb +0 -379
- data/lib/rbook/titlepage/wwwclient.rb +0 -96
- data/lib/rbook/www.rb +0 -172
- data/lib/rbook/www/aau_scraper.rb +0 -76
- data/lib/rbook/www/amazon_uk_scraper.rb +0 -44
- data/lib/rbook/www/ban_scraper.rb +0 -62
- data/lib/rbook/www/base.rb +0 -87
- data/lib/rbook/www/harper_au_scraper.rb +0 -56
- data/lib/rbook/www/harper_us_scraper.rb +0 -55
- data/lib/rbook/www/hha_scraper.rb +0 -50
- data/lib/rbook/www/macmillan_scraper.rb +0 -62
- data/lib/rbook/www/orbis_scraper.rb +0 -48
- data/lib/rbook/www/oup_scraper.rb +0 -64
- data/lib/rbook/www/paulist_scraper.rb +0 -53
- data/lib/rbook/www/pearson_au_scraper.rb +0 -52
- data/lib/rbook/www/penguin_scraper.rb +0 -45
- data/lib/rbook/www/random_au_scraper.rb +0 -90
- data/lib/rbook/www/random_us_scraper.rb +0 -59
- data/lib/rbook/www/sas_scraper.rb +0 -54
- data/lib/rbook/www/unireps_scraper.rb +0 -58
- data/lib/rbook/www/wiley_us_scraper.rb +0 -54
- data/test/data/abingdon.xml +0 -38931
- data/test/data/augsburg.xml +0 -39009
- data/test/data/bisac_po.txt +0 -112
- data/test/data/chalice.xml +0 -10851
- data/test/data/eerdsman.xml +0 -36942
- data/test/data/invalid_no_product.xml +0 -9
- data/test/data/not_xml.csv +0 -1
- data/test/data/single_product.xml +0 -50
- data/test/data/valid_bisac.txt +0 -213
- data/test/data/xml_not_onix.xml +0 -7
- data/test/mocks/titlepage_driver.rb +0 -111
- data/test/unit/bisac/bisac_test.rb +0 -96
- data/test/unit/bisac/po_line_item_test.rb +0 -38
- data/test/unit/bisac/po_test.rb +0 -82
- data/test/unit/isbn_test.rb +0 -153
- data/test/unit/onix/contributor_test.rb +0 -50
- data/test/unit/onix/message_test.rb +0 -119
- data/test/unit/onix/product_test.rb +0 -101
- data/test/unit/onix/sales_restriction_test.rb +0 -48
- data/test/unit/onix/stream_reader_test.rb +0 -22
- data/test/unit/onix/stream_writer_test.rb +0 -32
- data/test/unit/onix/supply_detail_test.rb +0 -53
- data/test/unit/onix/xchar_test.rb +0 -37
- data/test/unit/titlepage_test.rb +0 -140
data/lib/rbook/bisac/product.rb
DELETED
@@ -1,176 +0,0 @@
|
|
1
|
-
module RBook
|
2
|
-
module Bisac
|
3
|
-
|
4
|
-
# Class to represent a single product line in a Bisac File. See
|
5
|
-
# RBook::Bisac for basic usage instructions.
|
6
|
-
class Product
|
7
|
-
|
8
|
-
attr_reader :isbn, :title, :author, :price, :pubdate, :publisher
|
9
|
-
attr_reader :imprint, :volumes, :edition, :binding, :volume, :status
|
10
|
-
|
11
|
-
# Creates a new product object with the requested ISBN. Must be a 10 digit ISBN.
|
12
|
-
def initialize(isbn)
|
13
|
-
raise ArgumentError, 'isbn must 10 chars or less' if isbn.to_s.length > 10
|
14
|
-
|
15
|
-
@isbn = isbn.to_s
|
16
|
-
@title = ""
|
17
|
-
@author = ""
|
18
|
-
@price = ""
|
19
|
-
@pubdate = ""
|
20
|
-
@publisher = ""
|
21
|
-
@imprint = ""
|
22
|
-
@volumes = ""
|
23
|
-
@edition = ""
|
24
|
-
@binding = ""
|
25
|
-
@volume = ""
|
26
|
-
@status = ""
|
27
|
-
end
|
28
|
-
|
29
|
-
# sets the products author. Maximum of 30 chars.
|
30
|
-
def author=(author)
|
31
|
-
@author = author.to_s
|
32
|
-
end
|
33
|
-
|
34
|
-
# sets the products binding. Maximum of 2 chars.
|
35
|
-
def binding=(binding)
|
36
|
-
@binding = binding.to_s
|
37
|
-
end
|
38
|
-
|
39
|
-
# sets the products edition number. Maximum of 3 chars.
|
40
|
-
def edition=(edition)
|
41
|
-
@edition = edition.to_s
|
42
|
-
end
|
43
|
-
|
44
|
-
# takes a single line from a BISAC file and attempts to convert
|
45
|
-
# it to a Product object
|
46
|
-
def self.from_string(s)
|
47
|
-
s = s.to_s
|
48
|
-
return nil if s.length < 259
|
49
|
-
product = self.new(s[0,10])
|
50
|
-
product.title = s[15,30].strip
|
51
|
-
product.author = s[46,30].strip
|
52
|
-
product.price = s[79,7].strip if s[79,7].strip.match(/\A\d{0,7}\Z/)
|
53
|
-
product.pubdate = s[87,6].strip if s[87,6].strip.match(/\A\d{6}\Z/)
|
54
|
-
product.publisher = s[94,10].strip
|
55
|
-
product.imprint = s[105,14].strip
|
56
|
-
product.volumes = s[120,3].strip
|
57
|
-
product.edition = s[124,2].strip
|
58
|
-
product.binding = s[127,2].strip
|
59
|
-
product.volume = s[130,3].strip
|
60
|
-
product.status = s[153,3].strip
|
61
|
-
return product
|
62
|
-
end
|
63
|
-
|
64
|
-
# Sets the products imprint. Maximum of 14 chars.
|
65
|
-
def imprint=(imprint)
|
66
|
-
@imprint = imprint.to_s
|
67
|
-
end
|
68
|
-
|
69
|
-
# Sets the price imprint. Maximum of 7 chars. Must by a whole
|
70
|
-
# number (represent price in cents).
|
71
|
-
def price=(price)
|
72
|
-
unless price.to_s.match(/\A\d{0,7}\Z/)
|
73
|
-
raise ArgumentError, 'price should be a whole number with no more than 7 digits. (price in cents)'
|
74
|
-
end
|
75
|
-
@price = price.to_s
|
76
|
-
end
|
77
|
-
|
78
|
-
# Sets the products pubdate. Must be in the form YYMMDD
|
79
|
-
def pubdate=(pubdate)
|
80
|
-
unless pubdate.to_s.match(/\A\d{6}\Z/)
|
81
|
-
raise ArgumentError, 'pubdate should be a date in the form YYMMDD.'
|
82
|
-
end
|
83
|
-
@pubdate = pubdate.to_s
|
84
|
-
end
|
85
|
-
|
86
|
-
# sets the products publisher. Maximum of 10 chars.
|
87
|
-
def publisher=(publisher)
|
88
|
-
@publisher = publisher.to_s
|
89
|
-
end
|
90
|
-
|
91
|
-
# sets the products status code. Maximum of 3 chars.
|
92
|
-
def status=(status)
|
93
|
-
@status = status.to_s
|
94
|
-
end
|
95
|
-
|
96
|
-
# sets the products title. Maximum of 30 chars.
|
97
|
-
def title=(title)
|
98
|
-
@title = title.to_s
|
99
|
-
end
|
100
|
-
|
101
|
-
# Returns the product as a single line ready for inserting into a BISAC file.
|
102
|
-
# Doesn't have a \n on the end
|
103
|
-
def to_s
|
104
|
-
content = ""
|
105
|
-
content << @isbn[0,10].ljust(10) # 10 digit isbn
|
106
|
-
content << "1"
|
107
|
-
content << "N"
|
108
|
-
content << "N"
|
109
|
-
content << "B"
|
110
|
-
content << "N"
|
111
|
-
content << @title[0,30].ljust(30)
|
112
|
-
content << "N"
|
113
|
-
content << @author[0,30].ljust(30)
|
114
|
-
content << "N"
|
115
|
-
content << "A" # author role
|
116
|
-
content << "N"
|
117
|
-
content << @price[0,7].rjust(7,"0") # current price
|
118
|
-
content << "N"
|
119
|
-
content << @pubdate[0,6].ljust(6) # published date
|
120
|
-
content << "N"
|
121
|
-
content << @publisher[0,10].ljust(10) # publisher
|
122
|
-
content << "N"
|
123
|
-
content << @imprint[0,14].ljust(14) #imprint
|
124
|
-
content << "N"
|
125
|
-
content << @volumes[0,3].rjust(3,"0") # volumes included in this isbn
|
126
|
-
content << "N"
|
127
|
-
content << @edition[0,2].rjust(2,"0") # edition
|
128
|
-
content << "N"
|
129
|
-
content << @binding[0,2].rjust(2,"0") # binding
|
130
|
-
content << "N"
|
131
|
-
content << @volume[0,3].rjust(3,"0") # volume number
|
132
|
-
content << "N"
|
133
|
-
content << "0000000" # new price
|
134
|
-
content << "N"
|
135
|
-
content << "000000" # new price effective date
|
136
|
-
content << "N"
|
137
|
-
content << " " # audience type
|
138
|
-
content << "N"
|
139
|
-
content << @status[0,3].rjust(3) # status
|
140
|
-
content << "N"
|
141
|
-
content << " " # available date. only use for status' like NYP
|
142
|
-
content << "N"
|
143
|
-
content << " " # alternate isbn
|
144
|
-
content << "N"
|
145
|
-
content << "999999" # out of print date. only use for status == OP
|
146
|
-
content << "N"
|
147
|
-
content << " " # geographic restrictions
|
148
|
-
content << "N"
|
149
|
-
content << " " # library of congress catalogue number
|
150
|
-
content << "N"
|
151
|
-
content << "".ljust(40) # series title
|
152
|
-
content << "N"
|
153
|
-
content << "0" # price code for current price
|
154
|
-
content << "N"
|
155
|
-
content << "0" # price code for new price
|
156
|
-
content << "N"
|
157
|
-
content << "0000000" # freight pass through price
|
158
|
-
content << "N"
|
159
|
-
content << "000000" # new freight pass through price
|
160
|
-
content << "00000" # last changed date
|
161
|
-
|
162
|
-
return content
|
163
|
-
end
|
164
|
-
|
165
|
-
# sets the products volume number. Maximum of 3 chars.
|
166
|
-
def volume=(volume)
|
167
|
-
@volume = volume.to_s
|
168
|
-
end
|
169
|
-
|
170
|
-
# sets the number of volumes in the set this product belongs to. Maximum of 3 chars.
|
171
|
-
def volumes=(volumes)
|
172
|
-
@volumes = volumes.to_s
|
173
|
-
end
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
data/lib/rbook/errors.rb
DELETED
data/lib/rbook/gbip.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../")
|
2
|
-
|
3
|
-
require 'rbook/isbn'
|
4
|
-
require 'rbook/gbip/pos'
|
5
|
-
require 'rbook/gbip/title'
|
6
|
-
require 'rbook/gbip/warehouse'
|
7
|
-
|
8
|
-
module RBook
|
9
|
-
|
10
|
-
# Ruby classes for searching the globalbooksinprint.com API. This is a
|
11
|
-
# commercial service and requires a registered account to access. More
|
12
|
-
# information on the service can be found at the website.
|
13
|
-
#
|
14
|
-
# = Basic Usage
|
15
|
-
# gbip = RBook::GBIP::POS.new("username", "password")
|
16
|
-
# puts gbip.find("0091835135").inspect
|
17
|
-
#
|
18
|
-
module GBIP
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
data/lib/rbook/gbip/pos.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
require 'socket'
|
2
|
-
require 'enumerator'
|
3
|
-
|
4
|
-
module RBook
|
5
|
-
|
6
|
-
module GBIP
|
7
|
-
|
8
|
-
# Provides easy access to the globalbooksinprint.com search API.
|
9
|
-
# RBook::GBIP has basic usage examples.
|
10
|
-
class POS
|
11
|
-
|
12
|
-
# don't think we really need these
|
13
|
-
#attr_accessor :username, :password
|
14
|
-
|
15
|
-
POS_SERVER = "pos.bowker.com"
|
16
|
-
POS_PORT = 7052
|
17
|
-
|
18
|
-
# creates a new POS object ready to perform a search
|
19
|
-
def initialize(username, password)
|
20
|
-
@username = username
|
21
|
-
@password = password
|
22
|
-
end
|
23
|
-
|
24
|
-
# search for the specified ISBN on globalbooksinprint.com.
|
25
|
-
# Accepts both ISBN10 and ISBN13's.
|
26
|
-
def find(type, isbn, options = {})
|
27
|
-
if type != :first && type != :all
|
28
|
-
raise ArgumentError, 'type must by :first or :all'
|
29
|
-
end
|
30
|
-
|
31
|
-
# global only accepts ISBNs as 10 digits at this stage
|
32
|
-
isbn = RBook::ISBN::convert_to_isbn10(isbn)
|
33
|
-
return nil if isbn.nil?
|
34
|
-
|
35
|
-
request_format = "POS"
|
36
|
-
account_type = "3"
|
37
|
-
product = "2"
|
38
|
-
username = "#{@username}"
|
39
|
-
password = "#{@password}"
|
40
|
-
version = "2"
|
41
|
-
supplier = ""
|
42
|
-
request = "bn=#{isbn}"
|
43
|
-
filters = "1|1|1|1|1|1|1|1|1|1"
|
44
|
-
if type.eql?(:first)
|
45
|
-
records = "1,0"
|
46
|
-
else
|
47
|
-
records = "10,0"
|
48
|
-
end
|
49
|
-
sort_order = "1"
|
50
|
-
markets = options[:markets] || ""
|
51
|
-
|
52
|
-
request_string = "#{request_format}\t#{account_type}\t#{product}\t#{username}\t#{password}\t"
|
53
|
-
request_string << "#{version}\t#{supplier}\t#{request}\t#{filters}\t#{records}\t#{sort_order}\t"
|
54
|
-
request_string << "#{markets}\t"
|
55
|
-
|
56
|
-
gbip = TCPSocket.new(POS_SERVER, POS_PORT)
|
57
|
-
gbip.print request_string
|
58
|
-
response = gbip.gets(nil).split("#")
|
59
|
-
gbip.close
|
60
|
-
|
61
|
-
header = nil
|
62
|
-
titles_arr = []
|
63
|
-
response.each do |arr|
|
64
|
-
if header.nil?
|
65
|
-
header = arr.split("\t")
|
66
|
-
else
|
67
|
-
titles_arr << arr.split("\t")
|
68
|
-
titles_arr[-1] = titles_arr.last[1,titles_arr.last.size-1]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# raise an exception if incorrect login details were provided
|
73
|
-
if header.first.eql?("66")
|
74
|
-
raise ArgumentError, "Invalid username or password"
|
75
|
-
end
|
76
|
-
|
77
|
-
if titles_arr.size == 0
|
78
|
-
# return nil if no matching records found
|
79
|
-
return nil
|
80
|
-
else
|
81
|
-
|
82
|
-
titles = []
|
83
|
-
titles_arr.each do |arr|
|
84
|
-
title = build_object_tree(arr)
|
85
|
-
titles << title unless title.nil?
|
86
|
-
end
|
87
|
-
|
88
|
-
if type.eql?(:first)
|
89
|
-
return titles.first
|
90
|
-
else
|
91
|
-
return titles
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
private
|
98
|
-
|
99
|
-
def build_object_tree(arr)
|
100
|
-
raise ArgumentError, 'arr must be an array' unless arr.class == Array
|
101
|
-
return nil if arr.size < 18
|
102
|
-
|
103
|
-
title_arr = arr[0, 15]
|
104
|
-
warehouse_arr = arr[15,arr.size - 15]
|
105
|
-
title = Title.new(title_arr)
|
106
|
-
|
107
|
-
warehouse_arr.each_slice(5) do |slice|
|
108
|
-
warehouse_obj = Warehouse.new(slice)
|
109
|
-
title.warehouses << warehouse_obj unless warehouse_obj.nil?
|
110
|
-
end
|
111
|
-
|
112
|
-
return title
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
data/lib/rbook/gbip/title.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'bigdecimal'
|
2
|
-
|
3
|
-
module RBook
|
4
|
-
module GBIP
|
5
|
-
|
6
|
-
# models a warehouse supply detail from the Global Books in Print API
|
7
|
-
class Title
|
8
|
-
|
9
|
-
|
10
|
-
attr_accessor :market, :title, :isbn, :binding, :status, :edition
|
11
|
-
attr_accessor :contributor, :publisher, :publication_date
|
12
|
-
attr_accessor :rrp, :supplier, :suppliers_with_stock
|
13
|
-
attr_accessor :warehouses
|
14
|
-
|
15
|
-
def initialize(arr)
|
16
|
-
raise ArgumentError, "arr must be an array" unless arr.class == Array
|
17
|
-
#raise ArgumentError, "Invalid number of array elements" unless arr.size == 14
|
18
|
-
|
19
|
-
self.market = arr[2].strip
|
20
|
-
self.title = arr[3].strip
|
21
|
-
self.isbn = RBook::ISBN.convert_to_isbn13(arr[4].strip) || arr[4].strip
|
22
|
-
self.binding = arr[5].strip
|
23
|
-
self.status = arr[6].strip
|
24
|
-
self.edition = arr[7].strip
|
25
|
-
self.contributor = arr[8].strip
|
26
|
-
self.publisher = arr[9].strip
|
27
|
-
self.publication_date = arr[10].strip
|
28
|
-
self.rrp = BigDecimal.new(arr[11].strip)
|
29
|
-
self.supplier = arr[12].strip
|
30
|
-
self.suppliers_with_stock = arr[13].strip
|
31
|
-
|
32
|
-
@warehouses = []
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/lib/rbook/gbip/warehouse.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
|
-
module RBook
|
4
|
-
module GBIP
|
5
|
-
|
6
|
-
# models a warehouse supply detail from the Global Books in Print API
|
7
|
-
class Warehouse
|
8
|
-
|
9
|
-
|
10
|
-
attr_accessor :name, :status, :onhand, :onorder, :updated_at
|
11
|
-
|
12
|
-
def initialize(arr)
|
13
|
-
raise ArgumentError, "arr must be an array" unless arr.class == Array
|
14
|
-
raise ArgumentError, "Invalid number of array elements" unless arr.size == 5
|
15
|
-
|
16
|
-
puts arr.inspect
|
17
|
-
self.name = arr[0].strip
|
18
|
-
self.status = arr[1].strip
|
19
|
-
self.onhand = arr[2].to_i
|
20
|
-
self.onorder = arr[3].to_i
|
21
|
-
tmpdate = arr[4]
|
22
|
-
puts tmpdate
|
23
|
-
self.updated_at = Date.civil(tmpdate[6,4].to_i, tmpdate[0,2].to_i, tmpdate[3,2].to_i)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/rbook/isbn.rb
DELETED
@@ -1,255 +0,0 @@
|
|
1
|
-
module RBook
|
2
|
-
# A collection of useful functions for dealing with ISBNs. Each
|
3
|
-
# function can be used directly - examples listed below.
|
4
|
-
# To use these examples you will need the following 2 lines
|
5
|
-
# at the top of your script:
|
6
|
-
# require 'rubygems'
|
7
|
-
# require 'rbook/isbn'
|
8
|
-
module ISBN
|
9
|
-
|
10
|
-
# adds hyphens to a valid isbn in the appropriate spots
|
11
|
-
#
|
12
|
-
# ISBN::add_groups("020161622X")
|
13
|
-
# #=> 0-201-61622-X
|
14
|
-
# ISBN::add_groups("9780201616224")
|
15
|
-
# #=> 978-0-201-61622-4
|
16
|
-
def ISBN::add_groups(isbn)
|
17
|
-
|
18
|
-
if !valid_isbn?(isbn)
|
19
|
-
isbn
|
20
|
-
elsif isbn.length == 10
|
21
|
-
calc = isbn * 1
|
22
|
-
calc = calc.insert(1, "-")
|
23
|
-
calc = calc.insert(7, "-")
|
24
|
-
calc = calc.insert(11, "-")
|
25
|
-
return calc
|
26
|
-
elsif isbn.length == 13
|
27
|
-
calc = isbn * 1
|
28
|
-
calc = calc.insert(3, "-")
|
29
|
-
calc = calc.insert(5, "-")
|
30
|
-
calc = calc.insert(11, "-")
|
31
|
-
calc = calc.insert(15, "-")
|
32
|
-
return calc
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# Returns true if the supplied string is a valid isbn10 or isbn13
|
37
|
-
#
|
38
|
-
# ISBN::valid_isbn?("020161622X")
|
39
|
-
# #=> true
|
40
|
-
# ISBN::valid_isbn?("9780201616224")
|
41
|
-
# #=> true
|
42
|
-
# ISBN::valid_isbn?("9780201616223")
|
43
|
-
# #=> false
|
44
|
-
# ISBN::valid_isbn?("not an isbn")
|
45
|
-
# #=> false
|
46
|
-
def ISBN::valid_isbn?(isbn)
|
47
|
-
valid_isbn10?(isbn) || valid_isbn13?(isbn)
|
48
|
-
end
|
49
|
-
|
50
|
-
# returns true if the supplied string is a valid isbn10
|
51
|
-
#
|
52
|
-
# ISBN::valid_isbn10?("020161622X")
|
53
|
-
# #=> true
|
54
|
-
# ISBN::valid_isbn10?("9780201616224")
|
55
|
-
# #=> false
|
56
|
-
# ISBN::valid_isbn10?("9780201616223")
|
57
|
-
# #=> false
|
58
|
-
# ISBN::valid_isbn10?("not an isbn")
|
59
|
-
# #=> false
|
60
|
-
def ISBN::valid_isbn10?(isbn)
|
61
|
-
isbn.gsub!("-","") if isbn.class == String
|
62
|
-
if isbn.nil?
|
63
|
-
false
|
64
|
-
elsif isbn.class != String
|
65
|
-
false
|
66
|
-
elsif isbn.length != 10
|
67
|
-
false
|
68
|
-
elsif isbn[/[^\dxX]/]
|
69
|
-
false
|
70
|
-
else
|
71
|
-
checkdigit = ISBN::calc_isbn10_check(isbn.to_s[0,9])
|
72
|
-
if checkdigit.eql?("X")
|
73
|
-
isbn == isbn[0,9] + "x" || isbn == isbn[0,9] + "X"
|
74
|
-
else
|
75
|
-
isbn == isbn[0,9] + checkdigit
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# returns true if the supplied string is a valid isbn13
|
81
|
-
#
|
82
|
-
# ISBN::valid_isbn13?("020161622X")
|
83
|
-
# #=> false
|
84
|
-
# ISBN::valid_isbn13?("9780201616224")
|
85
|
-
# #=> true
|
86
|
-
# ISBN::valid_isbn13?("9780201616223")
|
87
|
-
# #=> false
|
88
|
-
# ISBN::valid_isbn13?("not an isbn")
|
89
|
-
# #=> false
|
90
|
-
def ISBN::valid_isbn13?(isbn)
|
91
|
-
isbn.gsub!("-","") if isbn.class == String
|
92
|
-
if isbn.nil?
|
93
|
-
false
|
94
|
-
elsif isbn.class != String
|
95
|
-
false
|
96
|
-
elsif isbn.length != 13
|
97
|
-
false
|
98
|
-
elsif isbn[/[^\dxX]/]
|
99
|
-
false
|
100
|
-
else
|
101
|
-
isbn == isbn[0,12] + ISBN::calc_isbn13_check(isbn[0,12])
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
# Attempts to calculate the check digit on a supplied 9 digit string.
|
106
|
-
#
|
107
|
-
# If passed nil or a non string, nil is returned.
|
108
|
-
#
|
109
|
-
# If the supplied string length isn't chars 9 long, nil is returned.
|
110
|
-
#
|
111
|
-
# If the supplied string is 9 chars long, the check digit is calculated and returned
|
112
|
-
#
|
113
|
-
# ISBN::calc_isbn10_check("020161622")
|
114
|
-
# #=> X
|
115
|
-
# ISBN::calc_isbn10_check("020161622X")
|
116
|
-
# #=> nil
|
117
|
-
# ISBN::calc_isbn10_check("9780201616224")
|
118
|
-
# #=> nil
|
119
|
-
# ISBN::calc_isbn10_check("9780201616223")
|
120
|
-
# #=> nil
|
121
|
-
# ISBN::calc_isbn10_check("not an isbn")
|
122
|
-
# #=> nil
|
123
|
-
def ISBN::calc_isbn10_check(isbn)
|
124
|
-
if isbn.nil? || isbn.class != String || isbn.length != 9
|
125
|
-
return nil
|
126
|
-
else
|
127
|
-
check = isbn[0,1].to_i
|
128
|
-
check = check + isbn[1,1].to_i * 2
|
129
|
-
check = check + isbn[2,1].to_i * 3
|
130
|
-
check = check + isbn[3,1].to_i * 4
|
131
|
-
check = check + isbn[4,1].to_i * 5
|
132
|
-
check = check + isbn[5,1].to_i * 6
|
133
|
-
check = check + isbn[6,1].to_i * 7
|
134
|
-
check = check + isbn[7,1].to_i * 8
|
135
|
-
check = check + isbn[8,1].to_i * 9
|
136
|
-
check = check % 11
|
137
|
-
|
138
|
-
if check == 10
|
139
|
-
return "X"
|
140
|
-
else
|
141
|
-
return check.to_s
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
# Attempts to calculate the check digit on a supplied 12 digit string.
|
147
|
-
#
|
148
|
-
# If passed nil or a non String, nil is returned.
|
149
|
-
#
|
150
|
-
# If the supplied string isn't 12 digits long, nil is returned.
|
151
|
-
#
|
152
|
-
# For all other strings the check digit will be returned
|
153
|
-
#
|
154
|
-
# ISBN::calc_isbn13_check("978020161622")
|
155
|
-
# #=> 4
|
156
|
-
# ISBN::calc_isbn13_check("9780201616224")
|
157
|
-
# #=> nil
|
158
|
-
# ISBN::calc_isbn13_check("020161622X")
|
159
|
-
# #=> nil
|
160
|
-
# ISBN::calc_isbn13_check("9780201616223")
|
161
|
-
# #=> nil
|
162
|
-
# ISBN::calc_isbn13_check("not an isbn")
|
163
|
-
# #=> nil
|
164
|
-
def ISBN::calc_isbn13_check(isbn)
|
165
|
-
if isbn.nil? || isbn.class != String || isbn.length != 12
|
166
|
-
return nil
|
167
|
-
else
|
168
|
-
checkdigit = isbn[0,1].to_i * 1
|
169
|
-
checkdigit = checkdigit + isbn[1,1].to_i * 3
|
170
|
-
checkdigit = checkdigit + isbn[2,1].to_i * 1
|
171
|
-
checkdigit = checkdigit + isbn[3,1].to_i * 3
|
172
|
-
checkdigit = checkdigit + isbn[4,1].to_i * 1
|
173
|
-
checkdigit = checkdigit + isbn[5,1].to_i * 3
|
174
|
-
checkdigit = checkdigit + isbn[6,1].to_i * 1
|
175
|
-
checkdigit = checkdigit + isbn[7,1].to_i * 3
|
176
|
-
checkdigit = checkdigit + isbn[8,1].to_i * 1
|
177
|
-
checkdigit = checkdigit + isbn[9,1].to_i * 3
|
178
|
-
checkdigit = checkdigit + isbn[10,1].to_i * 1
|
179
|
-
checkdigit = checkdigit + isbn[11,1].to_i * 3
|
180
|
-
|
181
|
-
remainder = checkdigit % 10
|
182
|
-
|
183
|
-
if remainder == 0
|
184
|
-
checkdigit = 0
|
185
|
-
else
|
186
|
-
checkdigit = (10 - remainder)
|
187
|
-
end
|
188
|
-
|
189
|
-
if checkdigit == 10
|
190
|
-
return "X"
|
191
|
-
else
|
192
|
-
return checkdigit.to_s
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
# Attempts to convert a valid isbn13 into an isbn10.
|
199
|
-
#
|
200
|
-
# If supplied string is a valid isbn10, it is returned unchanged.
|
201
|
-
#
|
202
|
-
# If if the string is a valid isbn13 and starts with 978, it is converted to an isbn10.
|
203
|
-
#
|
204
|
-
# All other strings return nil.
|
205
|
-
#
|
206
|
-
# ISBN::convert_to_isbn10("9780201616224")
|
207
|
-
# #=> 020161622X
|
208
|
-
# ISBN::convert_to_isbn10("020161622X")
|
209
|
-
# #=> 020161622X
|
210
|
-
# ISBN::convert_to_isbn10("9793472834833")
|
211
|
-
# #=> nil
|
212
|
-
# ISBN::convert_to_isbn10("not an isbn")
|
213
|
-
# #=> nil
|
214
|
-
def ISBN::convert_to_isbn10(convert_me)
|
215
|
-
if convert_me.nil? || convert_me.class != String
|
216
|
-
nil
|
217
|
-
elsif ISBN::valid_isbn10?(convert_me)
|
218
|
-
convert_me
|
219
|
-
elsif ISBN::valid_isbn13?(convert_me) && convert_me[0,3] == '978'
|
220
|
-
convert_me[3,9] + ISBN::calc_isbn10_check(convert_me[3,9])
|
221
|
-
else
|
222
|
-
nil
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
# Attempts to convert the supplied string into an ISBN13.
|
227
|
-
#
|
228
|
-
# If the string is a valid isbn13, it is returned unchanged.
|
229
|
-
#
|
230
|
-
# If the string is a valid isbn10, it will be converted to isbn13.
|
231
|
-
#
|
232
|
-
# All other strings will return nil.
|
233
|
-
#
|
234
|
-
# ISBN::convert_to_isbn13("020161622X")
|
235
|
-
# #=> 9780201616224
|
236
|
-
# ISBN::convert_to_isbn13("9780201616224")
|
237
|
-
# #=> 9780201616224
|
238
|
-
# ISBN::convert_to_isbn13("9793472834833")
|
239
|
-
# #=> nil
|
240
|
-
# ISBN::convert_to_isbn13("not an isbn")
|
241
|
-
# #=> nil
|
242
|
-
def ISBN::convert_to_isbn13(convert_me)
|
243
|
-
if convert_me.nil? || convert_me.class != String
|
244
|
-
nil
|
245
|
-
elsif ISBN::valid_isbn13?(convert_me)
|
246
|
-
convert_me
|
247
|
-
elsif ISBN::valid_isbn10?(convert_me)
|
248
|
-
'978'+convert_me[0,9] + ISBN::calc_isbn13_check('978'+convert_me[0,9])
|
249
|
-
else
|
250
|
-
nil
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
end
|
255
|
-
end
|