Module: Net::IMAP::NumValidator

Defined in:
lib/net/imap.rb

Overview

Common validators of number and nz_number types

Class Method Summary collapse

Class Method Details

.ensure_number(num) ⇒ Object

Ensure argument is ‘number’ or raise DataFormatError

Raises:



1611
1612
1613
1614
1615
1616
# File 'lib/net/imap.rb', line 1611

def ensure_number(num)
  return if valid_number?(num)

  msg = "number must be unsigned 32-bit integer: #{num}"
  raise DataFormatError, msg
end

.ensure_nz_number(num) ⇒ Object

Ensure argument is ‘nz_number’ or raise DataFormatError

Raises:



1619
1620
1621
1622
1623
1624
# File 'lib/net/imap.rb', line 1619

def ensure_nz_number(num)
  return if valid_nz_number?(num)

  msg = "nz_number must be non-zero unsigned 32-bit integer: #{num}"
  raise DataFormatError, msg
end

.valid_number?(num) ⇒ Boolean

Check is passed argument valid ‘number’ in RFC 3501 terminology

Returns:

  • (Boolean)


1593
1594
1595
1596
1597
1598
1599
# File 'lib/net/imap.rb', line 1593

def valid_number?(num)
  # [RFC 3501]
  # number          = 1*DIGIT
  #                    ; Unsigned 32-bit integer
  #                    ; (0 <= n < 4,294,967,296)
  num >= 0 && num < 4294967296
end

.valid_nz_number?(num) ⇒ Boolean

Check is passed argument valid ‘nz_number’ in RFC 3501 terminology

Returns:

  • (Boolean)


1602
1603
1604
1605
1606
1607
1608
# File 'lib/net/imap.rb', line 1602

def valid_nz_number?(num)
  # [RFC 3501]
  # nz-number       = digit-nz *DIGIT
  #                    ; Non-zero unsigned 32-bit integer
  #                    ; (0 < n < 4,294,967,296)
  num != 0 && valid_number?(num)
end