Class: Cellularity::Esn

Inherits:
Object
  • Object
show all
Defined in:
lib/cellularity/esn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(esn = '') ⇒ Esn

Returns a new instance of Esn.



6
7
8
# File 'lib/cellularity/esn.rb', line 6

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

Instance Attribute Details

#esnObject

Returns the value of attribute esn.



4
5
6
# File 'lib/cellularity/esn.rb', line 4

def esn
  @esn
end

Instance Method Details

#is_valid_decimal?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/cellularity/esn.rb', line 17

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

#is_valid_hexadecimal_with_prefix?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/cellularity/esn.rb', line 24

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

#is_valid_hexadecimal_without_prefix?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/cellularity/esn.rb', line 28

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

#valid?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/cellularity/esn.rb', line 10

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