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, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.area_codeString

Produces a random area code.

Examples:

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

Returns:

  • (String)

Available since:

  • 1.3.0



107
108
109
# File 'lib/faker/default/phone_number.rb', line 107

def area_code
  fetch('phone_number.area_code')
end

.cell_phoneString

Produces a random cell phone number in a random format without the country code and it can have different dividers.

Examples:

Faker::PhoneNumber.cell_phone #=> "(836) 115-8995"
Faker::PhoneNumber.cell_phone #=> "382-597-5739"
Faker::PhoneNumber.cell_phone #=> "316.828.1822"

Returns:

  • (String)

Available since:

  • 1.0.0



32
33
34
# File 'lib/faker/default/phone_number.rb', line 32

def cell_phone
  parse('cell_phone.formats')
end

.cell_phone_in_e164String

Produces a random phone number in e164 format, i.e., without any dividers.

Examples:

Faker::PhoneNumber.cell_phone_in_e164 #=> "+542024834991"
Faker::PhoneNumber.cell_phone_in_e164 #=> "+8522846847703"
Faker::PhoneNumber.cell_phone_in_e164 #=> "+649477546575"

Returns:

  • (String)

Available since:

  • 1.9.2



92
93
94
# File 'lib/faker/default/phone_number.rb', line 92

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 #=> "+852 (190) 987-9034"
Faker::PhoneNumber.cell_phone_with_country_code #=> "+64 (820) 583-6474"
Faker::PhoneNumber.cell_phone_with_country_code #=> "+1 591.871.7985"

Returns:

  • (String)

Available since:

  • 1.9.2



77
78
79
# File 'lib/faker/default/phone_number.rb', line 77

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

.country_codeString

Produces a random country code.

Examples:

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

Returns:

  • (String)

Available since:

  • 1.9.2



47
48
49
# File 'lib/faker/default/phone_number.rb', line 47

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

.exchange_codeString

Produces a random exchange code.

Examples:

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

Returns:

  • (String)

Available since:

  • 1.3.0



122
123
124
# File 'lib/faker/default/phone_number.rb', line 122

def exchange_code
  fetch('phone_number.exchange_code')
end

.phone_numberString

Produces a phone number in a random format without the country code and it can have different dividers.

Examples:

Faker::PhoneNumber.phone_number #=> "(504) 113-1705"
Faker::PhoneNumber.phone_number #=> "662.291.7201"
Faker::PhoneNumber.phone_number #=> "9415283713"

Returns:

  • (String)

Available since:

  • 0.3.0



17
18
19
# File 'lib/faker/default/phone_number.rb', line 17

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 #=> "+55 466-746-6882"
Faker::PhoneNumber.phone_number_with_country_code #=> "+81 3718219558"
Faker::PhoneNumber.phone_number_with_country_code #=> "+49 140 957 9846"

Returns:

  • (String)

Available since:

  • 1.9.2



62
63
64
# File 'lib/faker/default/phone_number.rb', line 62

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

.subscriber_number(length: 4) ⇒ String Also known as: extension

Produces a random 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"
Faker::PhoneNumber.extension(length: 2) => "37"

Parameters:

  • length (Integer) (defaults to: 4)

    Specifies the length of the return value. Defaults to 4.

Returns:

  • (String)

Available since:

  • 1.3.0



139
140
141
142
143
144
145
146
147
# File 'lib/faker/default/phone_number.rb', line 139

def subscriber_number(length: 4)
  if !length.is_a?(Integer) || length > 10
    raise ArgumentError, 'length must be an Integer and be lesser than 10'
  end

  generate(:string) do |g|
    g.int(length: length)
  end
end