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.5.15"

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 = isbn.to_s.delete("-")
  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



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

def correct_format?(isbn)
  case isbn.to_s.delete("-").size
  when 13
    ISBN13.correct_format?(isbn)
  when 10
    ISBN10.correct_format?(isbn)
  else
    false
  end
end

#hyphenate(isbn) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/petrarca.rb', line 51

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

#to_10(isbn13) ⇒ Object



61
62
63
64
# File 'lib/petrarca.rb', line 61

def to_10(isbn13)
  s = isbn13.to_s.delete("-")[3, 9]
  ISBN10.hyphenate(s + ISBN10.calc_check_digit(s))
end

#to_13(isbn10) ⇒ Object



66
67
68
69
# File 'lib/petrarca.rb', line 66

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

#valid?(isbn) ⇒ Boolean



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

def valid?(isbn)
  case isbn.to_s.delete("-").size
  when 13
    ISBN13.valid?(isbn)
  when 10
    ISBN10.valid?(isbn)
  else
    false
  end
end