|
1 |
| -# `Faker::PhoneNumber` |
| 1 | +# Faker::PhoneNumber |
2 | 2 |
|
3 |
| -### `.phone_number` |
| 3 | +Phone numbers generate North American Numbering Plan formats (with +1 as the country code). For different countries formats, see [Phone Numbers and Locales](#phone-number-and-locales). |
4 | 4 |
|
5 |
| -This formatter will return one of the following formats: |
| 5 | +## Faker::PhoneNumber.phone_number |
6 | 6 |
|
7 |
| - * 333-333-3333 |
8 |
| - * (333) 333-3333 |
9 |
| - * 1-333-333-3333 |
10 |
| - * 333.333.3333 |
11 |
| - * 333-333-3333 x3333 |
12 |
| - * (333) 333-3333 x3333 |
13 |
| - * 1-333-333-3333 x3333 |
14 |
| - * 333.333.3333 x3333 |
| 7 | +Generates a phone number in a random format without the country code and it can have different dividers. |
15 | 8 |
|
16 |
| -Note: For formats with extensions, the extension can be inclusively between 3 and 5 digits long. |
| 9 | +```ruby |
| 10 | +Faker::PhoneNumber.phone_number #=> "(504) 113-1705" |
| 11 | +Faker::PhoneNumber.phone_number #=> "662.291.7201" |
| 12 | +Faker::PhoneNumber.phone_number #=> "9415283713" |
| 13 | +``` |
17 | 14 |
|
18 |
| -### `.cell_phone` |
| 15 | +## Faker::PhoneNumber.cell_phone |
19 | 16 |
|
20 |
| -This formatter will return one of the following formats: |
| 17 | +Generates a random cell phone number in a random format without the country code and it can have different dividers. |
21 | 18 |
|
22 |
| - * 333-333-3333 |
23 |
| - * (333) 333-3333 |
24 |
| - * 1-333-333-3333 |
25 |
| - * 333.333.3333 |
| 19 | +```ruby |
| 20 | +Faker::PhoneNumber.cell_phone #=> "(836) 115-8995" |
| 21 | +Faker::PhoneNumber.cell_phone #=> "382-597-5739" |
| 22 | +Faker::PhoneNumber.cell_phone #=> "316.828.1822" |
| 23 | +``` |
26 | 24 |
|
27 |
| -### `.cell_phone_in_e164` |
| 25 | +## Faker::PhoneNumber.country_code |
28 | 26 |
|
29 |
| -This formatter will return one of the following formats: |
| 27 | +Generates a random country code number. |
30 | 28 |
|
31 |
| - * +33333333333333 |
32 |
| - * +3333333333333 |
| 29 | +```ruby |
| 30 | +Faker::PhoneNumber.country_code #=> "+20" |
| 31 | +Faker::PhoneNumber.country_code #=> "+39" |
| 32 | +Faker::PhoneNumber.country_code #=> "+852" |
| 33 | +``` |
33 | 34 |
|
34 |
| -## Locale |
35 |
| -By setting the locale, you can generate a phone number with a valid area code in that locale (and valid exchange code, where relevant). |
| 35 | +## Faker::PhoneNumber.phone_number_with_country_code |
36 | 36 |
|
37 |
| -### Locales with area codes |
38 |
| -```Faker::Config.locale = 'en-US' |
39 |
| -Faker::Config.locale = 'en-CA' |
40 |
| -Faker::Config.locale = 'fr-CA' |
41 |
| -Faker::Config.locale = 'en-AU' |
42 |
| -Faker::Config.locale = 'pt-BR' |
43 |
| -``` |
| 37 | +Generates a random phone number with country code. |
44 | 38 |
|
45 |
| -### Locales with exchange codes |
46 |
| -```Faker::Config.locale = 'en-US' |
47 |
| -Faker::Config.locale = 'en-CA' |
48 |
| -Faker::Config.locale = 'fr-CA' |
| 39 | +```ruby |
| 40 | +Faker::PhoneNumber.phone_number_with_country_code #=> "+55 466-746-6882" |
| 41 | +Faker::PhoneNumber.phone_number_with_country_code #=> "+81 3718219558" |
| 42 | +Faker::PhoneNumber.phone_number_with_country_code #=> "+49 140 957 9846" |
49 | 43 | ```
|
50 | 44 |
|
51 |
| -## Usage |
| 45 | +## Faker::PhoneNumber.cell_phone_with_country_code |
52 | 46 |
|
53 |
| -Don't let the example output below fool you -- any format can be returned at random. |
| 47 | +Generates a random cell phone number with country code. |
54 | 48 |
|
55 | 49 | ```ruby
|
56 |
| -Faker::PhoneNumber.phone_number #=> "397.693.1309 x4321" |
| 50 | +Faker::PhoneNumber.cell_phone_with_country_code #=> "+852 (190) 987-9034" |
| 51 | +Faker::PhoneNumber.cell_phone_with_country_code #=> "+64 (820) 583-6474" |
| 52 | +Faker::PhoneNumber.cell_phone_with_country_code #=> "+1 591.871.7985" |
| 53 | +``` |
57 | 54 |
|
58 |
| -Faker::PhoneNumber.cell_phone #=> "(186)285-7925" |
| 55 | +## Faker::PhoneNumber.cell_phone_in_e164 |
59 | 56 |
|
60 |
| -Faker::PhoneNumber.cell_phone_in_e164 #=> "+944937040625" |
| 57 | +Generates a random phone number in e164 format, i.e., without any dividers. |
61 | 58 |
|
62 |
| -# NOTE NOTE NOTE NOTE |
63 |
| -# For these two 'Locale-specific' methods, first you must set a relevant locale as mentioned above, such as: |
64 |
| -Faker::Config.locale = 'en-US' |
65 |
| -# or for Canada |
66 |
| -Faker::Config.locale = 'en-CA' |
| 59 | +```ruby |
| 60 | +Faker::PhoneNumber.cell_phone_in_e164 #=> "+542024834991" |
| 61 | +Faker::PhoneNumber.cell_phone_in_e164 #=> "+8522846847703" |
| 62 | +Faker::PhoneNumber.cell_phone_in_e164 #=> "+649477546575" |
| 63 | +``` |
67 | 64 |
|
68 |
| -# Locale-specific |
| 65 | +## Faker::PhoneNumber.area_code |
| 66 | + |
| 67 | +Generates a random area code. |
| 68 | + |
| 69 | +```ruby |
69 | 70 | Faker::PhoneNumber.area_code #=> "201"
|
| 71 | +Faker::PhoneNumber.area_code #=> "613" |
| 72 | +Faker::PhoneNumber.area_code #=> "321" |
| 73 | +``` |
| 74 | + |
| 75 | +## Faker::PhoneNumber.exchange_code |
70 | 76 |
|
71 |
| -# Locale-specific |
| 77 | +Generates a random exchange code. |
| 78 | + |
| 79 | +```ruby |
72 | 80 | Faker::PhoneNumber.exchange_code #=> "208"
|
| 81 | +Faker::PhoneNumber.exchange_code #=> "415" |
| 82 | +Faker::PhoneNumber.exchange_code #=> "652" |
| 83 | +``` |
73 | 84 |
|
74 |
| -# Keyword arguments: length |
| 85 | +## Faker::PhoneNumber.subscriber_number (alias Faker::PhoneNumber.extension) |
| 86 | + |
| 87 | +Generates a random extension / subscriber number. Can be used for both extensions and last four digits of phone number. |
| 88 | + |
| 89 | +```ruby |
| 90 | +# keyword arguments: length. Defaults to 4. |
75 | 91 | Faker::PhoneNumber.subscriber_number #=> "3873"
|
76 | 92 | Faker::PhoneNumber.subscriber_number(length: 2) #=> "39"
|
77 |
| - |
78 | 93 | Faker::PhoneNumber.extension #=> "3764"
|
| 94 | +Faker::PhoneNumber.extension(length: 2) => "37" |
| 95 | +``` |
79 | 96 |
|
80 |
| -Faker::PhoneNumber.country_code #=> "+20" |
| 97 | +## Phone Number and Locales |
81 | 98 |
|
82 |
| -Faker::PhoneNumber.phone_number_with_country_code #=> "+95 1-672-173-8153" |
| 99 | +If no locale is set, Faker generates North American Numbering Plan formats (with +1 area code). For more accurate values when generating US or CA formats, it's |
| 100 | +recommended to set their locales accordingly: |
83 | 101 |
|
84 |
| -Faker::PhoneNumber.cell_phone_with_country_code #=> "+974 (190) 987-9034" |
| 102 | +```ruby |
| 103 | +# set locale first |
| 104 | +Faker::Config.locale = 'en-US' |
| 105 | +Faker::PhoneNumber.country_code #=> "+1" |
| 106 | +Faker::PhoneNumber.area_code #=> "504" |
| 107 | +Faker::PhoneNumber.exchange_code #=> "715" |
| 108 | + |
| 109 | + |
| 110 | +# set locale first |
| 111 | +Faker::Config.locale = 'en-CA' |
| 112 | +Faker::PhoneNumber.country_code #=> "+1" |
| 113 | +Faker::PhoneNumber.area_code #=> "226" |
| 114 | +Faker::PhoneNumber.exchange_code #=> "956" |
| 115 | +``` |
| 116 | + |
| 117 | +Besides US and CA formats, Faker also generates phone numbers according to various locales. Here are some examples: |
| 118 | + |
| 119 | +```ruby |
| 120 | +Faker::Config.locale = 'da-DK' |
| 121 | +Faker::PhoneNumber.cell_phone_with_country_code # => "+45 20 76 45 76" |
| 122 | + |
| 123 | +Faker::Config.locale = 'de' |
| 124 | +Faker::PhoneNumber.cell_phone_with_country_code # => "+49 1559-7973422" |
| 125 | + |
| 126 | +Faker::Config.locale = 'pt-BR' |
| 127 | +Faker::PhoneNumber.cell_phone_with_country_code # => "+55 (77) 96227-7968" |
85 | 128 | ```
|
0 commit comments