lib/petrarca/isbn13.rb in petrarca-0.2.0 vs lib/petrarca/isbn13.rb in petrarca-0.3.0
- old
+ new
@@ -2,12 +2,10 @@
module Petrarca
module ISBN13
- include Helpers
-
extend self
def valid?(isbn)
correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
end
@@ -16,17 +14,23 @@
isbn = isbn.delete("-")
!!(/\A97[89]\d{9}\d\z/ =~ isbn)
end
def calc_check_digit(isbn)
- nums = isbn.delete("-").split("")[0..11].map{|x| x.to_i }
+ nums = isbn.delete("-").split("")[0, 12].map{|x| x.to_i }
sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
check_digit = 10 - (sum % 10)
check_digit == 10 ? "0" : check_digit.to_s
end
def hyphenate(isbn)
- hyphenate13(isbn)
+ ean_prefix = isbn[0, 3]
+ body = isbn[3, 9]
+ check_digit = isbn[12, 1]
+ registration_group, body = Helpers.split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
+ prefix = "#{ean_prefix}-#{registration_group}"
+ registrant, publication = Helpers.split_to_parts(body, REGISTRANT_RANGES[prefix])
+ [ean_prefix, registration_group, registrant, publication, check_digit].join("-")
end
end
end