-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add checking ISBN, accepting 10 & 13 digits, ignoring hypens
- Loading branch information
1 parent
2eb6a68
commit cd46a9a
Showing
7 changed files
with
208 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module RelatonIsbn | ||
class Isbn | ||
# | ||
# Create ISBN object. | ||
# | ||
# @param [String] isbn ISBN 13 number | ||
# | ||
def initialize(isbn) | ||
@isbn = isbn&.delete("-")&.sub(/^ISBN\s/, "") | ||
end | ||
|
||
def parse | ||
convert_to13 | ||
end | ||
|
||
def check? | ||
case @isbn | ||
when /^\d{9}[\dX]$/i then check10? | ||
when /^\d{13}$/ then check13? | ||
else false | ||
end | ||
end | ||
|
||
def check10? | ||
@isbn[9] == calc_check_digit10 | ||
end | ||
|
||
def check13? | ||
@isbn[12] == calc_check_digit13 | ||
end | ||
|
||
def calc_check_digit10 | ||
sum = 0 | ||
@isbn[..-2].chars.each_with_index do |c, i| | ||
sum += c.to_i * (10 - i) | ||
end | ||
chk = (11 - sum % 11) % 11 | ||
chk == 10 ? "X" : chk.to_s | ||
end | ||
|
||
def calc_check_digit13 | ||
sum = 0 | ||
@isbn[..-2].chars.each_with_index do |c, i| | ||
sum += c.to_i * (i.even? ? 1 : 3) | ||
end | ||
((10 - sum % 10) % 10).to_s | ||
end | ||
|
||
def convert_to13 | ||
return unless check? | ||
|
||
return @isbn if @isbn.size == 13 | ||
|
||
@isbn = "978#{@isbn}" | ||
@isbn[12] = calc_check_digit13 | ||
@isbn | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module RelatonIsbn | ||
VERSION = "1.17.0".freeze | ||
VERSION = "1.17.1".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
describe RelatonIsbn::Isbn do | ||
it "creates ISBN object" do | ||
subj = described_class.new("ISBN 0-12-064481-9") | ||
expect(subj).to be_instance_of RelatonIsbn::Isbn | ||
expect(subj.instance_variable_get(:@isbn)).to eq "0120644819" | ||
end | ||
|
||
context "parse ISBN" do | ||
it "with 10-digit ISBN" do | ||
expect(described_class.new("ISBN 0-12-064481-9").parse).to eq "9780120644810" | ||
end | ||
|
||
it "with 13-digit ISBN" do | ||
expect(described_class.new("978-0-12-064481-0").parse).to eq "9780120644810" | ||
end | ||
|
||
it "with incorrect ISBN" do | ||
expect(described_class.new("978-0-12-064481-").parse).to be_nil | ||
end | ||
|
||
it "with incorrect 10-digits ISBN" do | ||
expect(described_class.new("0-12-064481-8").parse).to be_nil | ||
end | ||
|
||
it "with incorrect 13-digits ISBN" do | ||
expect(described_class.new("978-0-12-064481-1").parse).to be_nil | ||
end | ||
|
||
it "with nil" do | ||
expect(described_class.new(nil).parse).to be_nil | ||
end | ||
end | ||
|
||
context "check ISBN" do | ||
it "10-digit ISBN" do | ||
expect(described_class.new("0-12-064481-9").check?).to be true | ||
end | ||
|
||
it "13-digit ISBN" do | ||
expect(described_class.new("978-0-12-064481-0").check?).to be true | ||
end | ||
|
||
it "incorrect 10-digits ISBN" do | ||
expect(described_class.new("0-12-064481-8").check?).to be false | ||
end | ||
|
||
it "incorrect 13-digits ISBN" do | ||
expect(described_class.new("978-0-12-064481-1").check?).to be false | ||
end | ||
|
||
it "incorrect ISBN" do | ||
expect(described_class.new("978-0-12-064481-").check?).to be false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters