Module: Petrarca

Extended by:
Petrarca
Included in:
Petrarca
Defined in:
lib/petrarca.rb,
lib/petrarca/isbn10.rb,
lib/petrarca/isbn13.rb,
lib/petrarca/helpers.rb,
lib/petrarca/version.rb

Defined Under Namespace

Modules: Helpers, ISBN10, ISBN13 Classes: Error, IncorrectFormatError, InvalidEANPrefixError, InvalidRangeError

Constant Summary collapse

REGISTRATION_GROUP_RANGES =
Helpers.load_ranges("#{data_dir}/registration_group_ranges.txt")
REGISTRANT_RANGES =
Helpers.load_ranges("#{data_dir}/registrant_ranges.txt")
VERSION =
"0.6.6"

Instance Method Summary collapse

Instance Method Details

#calc_check_digit(isbn) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/petrarca.rb', line 39

def calc_check_digit(isbn)
  isbn = dehyphenate(isbn)
  case isbn.size
  when 12, 13
    ISBN13.calc_check_digit(isbn)
  when 9, 10
    ISBN10.calc_check_digit(isbn)
  else
    raise IncorrectFormatError
  end
end

#correct_format?(isbn) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/petrarca.rb', line 28

def correct_format?(isbn)
  case dehyphenate(isbn).size
  when 13
    ISBN13.correct_format?(isbn)
  when 10
    ISBN10.correct_format?(isbn)
  else
    false
  end
end

#dehyphenate(isbn) ⇒ Object



51
52
53
# File 'lib/petrarca.rb', line 51

def dehyphenate(isbn)
  isbn.to_s.delete("-")
end

#hyphenate(isbn) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/petrarca.rb', line 55

def hyphenate(isbn)
  isbn = isbn.to_s
  case isbn.size
  when 13
    ISBN13.hyphenate(isbn)
  when 10
    ISBN10.hyphenate(isbn)
  end
end

#split(isbn) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/petrarca.rb', line 65

def split(isbn)
  isbn = isbn.to_s
  case isbn.size
  when 13
    ISBN13.split(isbn)
  when 10
    ISBN10.split(isbn)
  end
end

#to_10(isbn13) ⇒ Object



75
76
77
78
# File 'lib/petrarca.rb', line 75

def to_10(isbn13)
  s = dehyphenate(isbn13)[3, 9]
  ISBN10.hyphenate(s + ISBN10.calc_check_digit(s))
end

#to_13(isbn10) ⇒ Object



80
81
82
83
# File 'lib/petrarca.rb', line 80

def to_13(isbn10)
  s = "978" + dehyphenate(isbn10)[0, 9]
  ISBN13.hyphenate(s + ISBN13.calc_check_digit(s))
end

#valid?(isbn) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/petrarca.rb', line 17

def valid?(isbn)
  case dehyphenate(isbn).size
  when 13
    ISBN13.valid?(isbn)
  when 10
    ISBN10.valid?(isbn)
  else
    false
  end
end