Sha256: 146d41ad1fc0eb18c28a78068a593c994ad139cde2c1673c271cbc95098dac25

Contents?: true

Size: 1.41 KB

Versions: 16

Compression:

Stored size: 1.41 KB

Contents

require "petrarca/version"
require "petrarca/isbn13"
require "petrarca/isbn10"
require "petrarca/helpers"


module Petrarca
  class Error < StandardError; end
  class IncorrectFormatError < StandardError; end

  data_dir = __dir__ + "/../data"
  REGISTRATION_GROUP_RANGES = Helpers.load_ranges("#{data_dir}/registration_group_ranges.txt")
  REGISTRANT_RANGES = Helpers.load_ranges("#{data_dir}/registrant_ranges.txt")

  extend self

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

  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

  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

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

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

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

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
petrarca-0.5.15 lib/petrarca.rb
petrarca-0.5.14 lib/petrarca.rb
petrarca-0.5.13 lib/petrarca.rb
petrarca-0.5.12 lib/petrarca.rb
petrarca-0.5.11 lib/petrarca.rb
petrarca-0.5.10 lib/petrarca.rb
petrarca-0.5.9 lib/petrarca.rb
petrarca-0.5.8 lib/petrarca.rb
petrarca-0.5.7 lib/petrarca.rb
petrarca-0.5.6 lib/petrarca.rb
petrarca-0.5.5 lib/petrarca.rb
petrarca-0.5.4 lib/petrarca.rb
petrarca-0.5.3 lib/petrarca.rb
petrarca-0.5.2 lib/petrarca.rb
petrarca-0.5.1 lib/petrarca.rb
petrarca-0.5.0 lib/petrarca.rb