Class: Faker::PhoneNumber

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/phone_number.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.area_codeString

Produces a random US or Canada-based area code.

Examples:

Faker::PhoneNumber.area_code #=> "201"

Returns:

  • (String)

Available since:

  • 1.3.0


93
94
95
96
97
# File 'lib/faker/default/phone_number.rb', line 93

def area_code
  fetch('phone_number.area_code')
rescue I18n::MissingTranslationData
  nil
end

.cell_phoneString

Produces a random cell phone number in a random format (may or may not have a country code and can have different dividers).

Examples:

Faker::PhoneNumber.cell_phone #=> "(186)285-7925"

Returns:

  • (String)

Available since:

  • 1.0.0


28
29
30
# File 'lib/faker/default/phone_number.rb', line 28

def cell_phone
  parse('cell_phone.formats')
end

.cell_phone_in_e164String

Produces a random phone number in e164 format.

Examples:

Faker::PhoneNumber.cell_phone_in_e164 #=> "+944937040625"

Returns:

  • (String)

Available since:

  • 1.9.2


80
81
82
# File 'lib/faker/default/phone_number.rb', line 80

def cell_phone_in_e164
  cell_phone_with_country_code.delete('^+0-9')
end

.cell_phone_with_country_codeString

Produces a random cell phone number with country code.

Examples:

Faker::PhoneNumber.cell_phone_with_country_code #=> "+974 (190) 987-9034"

Returns:

  • (String)

Available since:

  • 1.9.2


67
68
69
# File 'lib/faker/default/phone_number.rb', line 67

def cell_phone_with_country_code
  "#{country_code} #{cell_phone}"
end

.country_codeString

Produces a random country code.

Examples:

Faker::PhoneNumber.country_code #=> "+20"

Returns:

  • (String)

Available since:

  • 1.9.2


41
42
43
# File 'lib/faker/default/phone_number.rb', line 41

def country_code
  "+#{fetch('country_code')}"
end

.exchange_codeString

Produces a random US or Canada-based exchange code.

Examples:

Faker::PhoneNumber.exchange_code #=> "208"

Returns:

  • (String)

Available since:

  • 1.3.0


108
109
110
111
112
# File 'lib/faker/default/phone_number.rb', line 108

def exchange_code
  fetch('phone_number.exchange_code')
rescue I18n::MissingTranslationData
  nil
end

.phone_numberString

Produces a random phone number in a random format (may or may not have a country code, extension and can have different dividers).

Examples:

Faker::PhoneNumber.phone_number #=> "397.693.1309 x4321"

Returns:

  • (String)

Available since:

  • 0.3.0


15
16
17
# File 'lib/faker/default/phone_number.rb', line 15

def phone_number
  parse('phone_number.formats')
end

.phone_number_with_country_codeString

Produces a random phone number with country code.

Examples:

Faker::PhoneNumber.phone_number_with_country_code #=> "+95 1-672-173-8153"

Returns:

  • (String)

Available since:

  • 1.9.2


54
55
56
# File 'lib/faker/default/phone_number.rb', line 54

def phone_number_with_country_code
  "#{country_code} #{phone_number}"
end

.subscriber_number(legacy_length = NOT_GIVEN, length: 4) ⇒ String Also known as: extension

Produces a random US or Canada-based extension / subscriber number. Can be used for both extensions and last four digits of phone number.

Examples:

Faker::PhoneNumber.subscriber_number #=> "3873"
Faker::PhoneNumber.subscriber_number(length: 2) #=> "39"
Faker::PhoneNumber.extension #=> "3764"

Parameters:

  • length (Integer) (defaults to: 4)

    Speficies the length of the return value.

Returns:

  • (String)

Available since:

  • 1.3.0


126
127
128
129
130
131
132
# File 'lib/faker/default/phone_number.rb', line 126

def subscriber_number(legacy_length = NOT_GIVEN, length: 4)
  warn_for_deprecated_arguments do |keywords|
    keywords << :length if legacy_length != NOT_GIVEN
  end

  rand.to_s[2..(1 + length)]
end