Sha256: 76109c33a49dfa0c60ff3b964c4e93d1ab730905f12ba039bc3b0283b2f11907

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module RelatonIsbn
  class Isbn
    #
    # Create ISBN object.
    #
    # @param [String] isbn ISBN 13 number
    #
    def initialize(isbn)
      @isbn = isbn&.delete("-")&.sub(/^ISBN\s/, "")
    end

    def parse
      convert_to13
    end

    def check?
      case @isbn
      when /^\d{9}[\dX]$/i then check10?
      when /^\d{13}$/ then check13?
      else false
      end
    end

    def check10?
      @isbn[9] == calc_check_digit10
    end

    def check13?
      @isbn[12] == calc_check_digit13
    end

    def calc_check_digit10
      sum = 0
      @isbn[..-2].chars.each_with_index do |c, i|
        sum += c.to_i * (10 - i)
      end
      chk = (11 - sum % 11) % 11
      chk == 10 ? "X" : chk.to_s
    end

    def calc_check_digit13
      sum = 0
      @isbn[..-2].chars.each_with_index do |c, i|
        sum += c.to_i * (i.even? ? 1 : 3)
      end
      ((10 - sum % 10) % 10).to_s
    end

    def convert_to13
      return unless check?

      return @isbn if @isbn.size == 13

      @isbn = "978#{@isbn}"
      @isbn[12] = calc_check_digit13
      @isbn
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
relaton-isbn-1.18.0 lib/relaton_isbn/isbn.rb
relaton-isbn-1.17.1 lib/relaton_isbn/isbn.rb