Class: Gtiny::ISBN

Inherits:
Encoding show all
Defined in:
lib/gtiny/isbn.rb

Overview

It seems like GTINs can also be encoded as ISBNs, which are either 10 or 13 digits.

ISBN-13s always have a 978 or 979 prefix, though that may be expanded in the future.

Prior to ISBNs, “SBNs” were used and were either 9 or 12 digits. A 9 digit SBN can be converted to an ISBN by prefixing it with a single zero. A 12 digit SBN can be converted to a 9 digit SBN by dropping the last three digits, which were often the price of the book.

Direct Known Subclasses

ISBN10, ISBN13

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Encoding

#initialize, matches?, regex, #to_s, #type, #zeroes?

Constructor Details

This class inherits a constructor from Gtiny::Encoding

Class Method Details

.normalize(string) ⇒ Object



17
18
19
# File 'lib/gtiny/isbn.rb', line 17

def self.normalize(string)
  string.downcase.gsub(" ", "-")
end

Instance Method Details

#check_digitObject



21
22
23
# File 'lib/gtiny/isbn.rb', line 21

def check_digit
  string[-1]
end

#group_identifierObject



25
26
27
28
29
# File 'lib/gtiny/isbn.rb', line 25

def group_identifier
  check_number_of_groups!

  groups[-4]
end

#publisher_identifierObject



31
32
33
34
35
# File 'lib/gtiny/isbn.rb', line 31

def publisher_identifier
  check_number_of_groups!

  groups[-3]
end

#title_identifierObject



37
38
39
40
41
# File 'lib/gtiny/isbn.rb', line 37

def title_identifier
  check_number_of_groups!

  groups[-2]
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/gtiny/isbn.rb', line 43

def valid?
  self.class.regex.match?(string) &&
    digits.size == canonical_digits_size &&
    valid_check_digit?
end