Module: FFaker::PhoneNumber

Extended by:
ModuleUtils, PhoneNumber
Included in:
PhoneNumber
Defined in:
lib/ffaker/phone_number.rb

Instance Method Summary collapse

Methods included from ModuleUtils

const_missing, k, underscore

Instance Method Details

#area_codeObject



29
30
31
32
33
34
35
36
37
# File 'lib/ffaker/phone_number.rb', line 29

def area_code
  # The North American Numbering Plan (NANP) does not permit the digits 0
  # and 1 as the leading digit of an area code.
  # https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes
  loop do
    rand_area_code = rand(201..999)
    return rand_area_code unless rand_area_code % 100 == 11
  end
end

#imei(serial_number = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ffaker/phone_number.rb', line 43

def imei(serial_number = nil)
  # IMEI Format:
  # AA-BBBBBB-CCCCCC-D

  rbi = '00'            # Test IMEI for countries with 2-digit country codes
  tac = "#{rbi}124500"  # iPhone

  serial_number ||= rand(1_000_000)
  serial_number = sprintf('%06d', serial_number)

  imei_base = tac + serial_number

  check_digit = 0
  base_digits = imei_base.split('').map(&:to_i)
  base_digits.each_with_index do |digit, i|
    check_digit += i.even? ? 2 * digit : digit
  end
  check_digit = (10 - check_digit % 10) % 10

  "#{imei_base}#{check_digit}"
end

#phone_numberObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ffaker/phone_number.rb', line 8

def phone_number
  FFaker.numerify case rand(20)
                  when      0 then "#{area_code}-###-#### x#####"
                  when      1 then "#{area_code}-###-#### x####"
                  when      2 then "#{area_code}-###-#### x###"
                  when 3..4 then "#{area_code}-###-####"
                  when      5 then "#{area_code}.###.#### x#####"
                  when      6 then "#{area_code}.###.#### x####"
                  when      7 then "#{area_code}.###.#### x###"
                  when 8..9 then "#{area_code}.###.####"
                  when     10 then "(#{area_code})###-#### x#####"
                  when     11 then "(#{area_code})###-#### x####"
                  when     12 then "(#{area_code})###-#### x###"
                  when 13..14 then "(#{area_code})###-####"
                  when     15 then "1-#{area_code}-###-#### x#####"
                  when     16 then "1-#{area_code}-###-#### x####"
                  when     17 then "1-#{area_code}-###-#### x###"
                  when 18..19 then "1-#{area_code}-###-####"
  end
end

#short_phone_numberObject



39
40
41
# File 'lib/ffaker/phone_number.rb', line 39

def short_phone_number
  FFaker.numerify("#{area_code}-###-####")
end