Class: Bookworm
- Inherits:
-
Object
- Object
- Bookworm
- Defined in:
- lib/bookworm.rb
Instance Attribute Summary collapse
-
#isbn ⇒ Object
readonly
Returns the value of attribute isbn.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Instance Method Summary collapse
- #as_new(identifier = isbn) ⇒ Object
- #as_ten ⇒ Object
- #as_used ⇒ Object
-
#initialize(identifier) ⇒ Bookworm
constructor
A new instance of Bookworm.
- #is_valid? ⇒ Boolean
- #scrub(string) ⇒ Object
- #ten_check_digit(identifier = isbn) ⇒ Object
- #thirteen_check_digit(identifier = isbn) ⇒ Object
- #thirteen_strip(identifier) ⇒ Object
- #to_thirteen(identifier) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(identifier) ⇒ Bookworm
Returns a new instance of Bookworm.
4 5 6 7 8 |
# File 'lib/bookworm.rb', line 4 def initialize(identifier) @original = identifier.to_s @isbn = as_new(identifier.to_s) @type = type end |
Instance Attribute Details
#isbn ⇒ Object (readonly)
Returns the value of attribute isbn.
2 3 4 |
# File 'lib/bookworm.rb', line 2 def isbn @isbn end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
2 3 4 |
# File 'lib/bookworm.rb', line 2 def original @original end |
Instance Method Details
#as_new(identifier = isbn) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/bookworm.rb', line 10 def as_new(identifier=isbn) return nil unless identifier identifier = scrub(identifier) case identifier when /^290/ then to_thirteen("978#{identifier[3..-1]}") when /^291/ then to_thirteen("979#{identifier[3..-1]}") when /^979|978|\d{10}/ then to_thirteen(identifier) else nil end end |
#as_ten ⇒ Object
27 28 29 30 31 |
# File 'lib/bookworm.rb', line 27 def as_ten return nil if not is_valid? or isbn =~ /979/ identifier = isbn[3..-2] identifier + ten_check_digit(identifier) end |
#as_used ⇒ Object
21 22 23 24 25 |
# File 'lib/bookworm.rb', line 21 def as_used return nil unless is_valid? prefix = /^979/.match(isbn) ? "291" : "290" to_thirteen(prefix + isbn[3..-1]) end |
#is_valid? ⇒ Boolean
88 89 90 91 92 93 94 |
# File 'lib/bookworm.rb', line 88 def is_valid? case original when /^\d{13}/ then original[12] == thirteen_check_digit(original) when /^\d{10}/ then original[9] == ten_check_digit(original) else false end end |
#scrub(string) ⇒ Object
51 52 53 54 |
# File 'lib/bookworm.rb', line 51 def scrub(string) result = /^((?:978|979|291|290)\d{10}|\d{10}).*/.match(string.delete('-')) result ? result[1] : nil end |
#ten_check_digit(identifier = isbn) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/bookworm.rb', line 72 def ten_check_digit(identifier=isbn) sum = 0 9.times do |index| sum += (10 - index) * identifier[index].to_i end checksum = 11 - sum % 11 case checksum when 10 then "X" when 11 then "0" else checksum.to_s end end |
#thirteen_check_digit(identifier = isbn) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bookworm.rb', line 56 def thirteen_check_digit(identifier=isbn) identifier, sum = identifier.rjust(13,"978")[/(.+)\w/,1], 0 12.times do |index| digit = identifier[index].to_i sum += index.even? ? digit : digit * 3 end checksum = (10 - sum % 10) checksum == 10 ? '0' : checksum.to_s end |
#thirteen_strip(identifier) ⇒ Object
68 69 70 |
# File 'lib/bookworm.rb', line 68 def thirteen_strip(identifier) identifier.rjust(13,"978")[0..-2] end |
#to_thirteen(identifier) ⇒ Object
47 48 49 |
# File 'lib/bookworm.rb', line 47 def to_thirteen(identifier) thirteen_strip(identifier) + thirteen_check_digit(identifier) end |
#type ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bookworm.rb', line 33 def type addendum = case original when /^(?:978|979)\d{10}/ addendum = /^(?:978|979)\d{10}(\d{5})$/.match(original).to_a.last addendum == '90000' ? 'used' : 'new' when /^(?:290|291)\d{10}/ addendum = /^(?:290|291)\d{10}(\d{5})$/.match(original).to_a.last addendum == '99990' ? 'new' : 'used' when /^\d{10}$/ 'new' else nil end end |