Sha256: 6bf921d875f4dd108874b9290209b02511f8af7724c448b6f2f9e33f9b8d5165

Contents?: true

Size: 747 Bytes

Versions: 2

Compression:

Stored size: 747 Bytes

Contents

module Cellularity
  class Esn

    attr_accessor :esn

    def initialize(esn = '')
      self.esn = esn.to_s
    end

    def valid?
      return is_valid_decimal? if self.esn.length == 11
      return is_valid_hexadecimal_with_prefix? if self.esn.length == 10
      return is_valid_hexadecimal_without_prefix? if self.esn.length == 8
      false
    end

    def is_valid_decimal?
      # Remove leading zeros so Integer doesn't think it's an octal.
      !!Integer(self.esn.gsub(/^0+/, ''))
    rescue ArgumentError, TypeError
      false
    end

    def is_valid_hexadecimal_with_prefix?
      self.esn =~ /0x[\h]+/i
    end

    def is_valid_hexadecimal_without_prefix?
      self.esn =~ /\h+/i && !self.esn.include?('0x')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cellularity-1.0.0 lib/cellularity/esn.rb
cellularity-0.0.2.1 lib/cellularity/esn.rb