Skip to content

Commit 31d99d1

Browse files
Stefanni Brasilraysapida
Stefanni Brasil
andauthored
Fix Phone number long number and other updates (#2842)
* Fix Phone number long number and other updates - moves phones country code to `phone_number.country_code` to avoid mixing it with address country code; - removes extensions from the phone number generation. Extensions added more numbers to the cell phone; - update `en.phone_number.country_code` locale to include only a sample of random country codes, and removes extra digits such as area code from them; - fixes Phone number area and exchange code with no locales: if there is no locale set, falls back to the `en.phone_number` area and exchange code generators; - updates YARD docs about the generators, and add more tests; - updates documentation; * Add note about North American Numbering Plan in documentation Co-authored-by: Raymond Sapida <[email protected]> * Update formatting for locale files using reformat_yaml rake task Co-authored-by: Raymond Sapida <[email protected]> * Update phone_number.md --------- Co-authored-by: Raymond Sapida <[email protected]>
1 parent 3173009 commit 31d99d1

35 files changed

+54372
-6440
lines changed

doc/default/phone_number.md

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,128 @@
1-
# `Faker::PhoneNumber`
1+
# Faker::PhoneNumber
22

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).
44

5-
This formatter will return one of the following formats:
5+
## Faker::PhoneNumber.phone_number
66

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.
158

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+
```
1714

18-
### `.cell_phone`
15+
## Faker::PhoneNumber.cell_phone
1916

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.
2118

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+
```
2624

27-
### `.cell_phone_in_e164`
25+
## Faker::PhoneNumber.country_code
2826

29-
This formatter will return one of the following formats:
27+
Generates a random country code number.
3028

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+
```
3334

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
3636

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.
4438

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"
4943
```
5044

51-
## Usage
45+
## Faker::PhoneNumber.cell_phone_with_country_code
5246

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.
5448

5549
```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+
```
5754

58-
Faker::PhoneNumber.cell_phone #=> "(186)285-7925"
55+
## Faker::PhoneNumber.cell_phone_in_e164
5956

60-
Faker::PhoneNumber.cell_phone_in_e164 #=> "+944937040625"
57+
Generates a random phone number in e164 format, i.e., without any dividers.
6158

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+
```
6764

68-
# Locale-specific
65+
## Faker::PhoneNumber.area_code
66+
67+
Generates a random area code.
68+
69+
```ruby
6970
Faker::PhoneNumber.area_code #=> "201"
71+
Faker::PhoneNumber.area_code #=> "613"
72+
Faker::PhoneNumber.area_code #=> "321"
73+
```
74+
75+
## Faker::PhoneNumber.exchange_code
7076

71-
# Locale-specific
77+
Generates a random exchange code.
78+
79+
```ruby
7280
Faker::PhoneNumber.exchange_code #=> "208"
81+
Faker::PhoneNumber.exchange_code #=> "415"
82+
Faker::PhoneNumber.exchange_code #=> "652"
83+
```
7384

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.
7591
Faker::PhoneNumber.subscriber_number #=> "3873"
7692
Faker::PhoneNumber.subscriber_number(length: 2) #=> "39"
77-
7893
Faker::PhoneNumber.extension #=> "3764"
94+
Faker::PhoneNumber.extension(length: 2) => "37"
95+
```
7996

80-
Faker::PhoneNumber.country_code #=> "+20"
97+
## Phone Number and Locales
8198

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:
83101

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"
85128
```

lib/faker/default/phone_number.rb

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ module Faker
44
class PhoneNumber < Base
55
class << self
66
##
7-
# Produces a random phone number in a random format (may or may not have a country code, extension and can have different dividers).
7+
# Produces a phone number in a random format without the country code and it can have different dividers.
88
#
99
# @return [String]
1010
#
1111
# @example
12-
# Faker::PhoneNumber.phone_number #=> "397.693.1309 x4321"
12+
# Faker::PhoneNumber.phone_number #=> "(504) 113-1705"
13+
# Faker::PhoneNumber.phone_number #=> "662.291.7201"
14+
# Faker::PhoneNumber.phone_number #=> "9415283713"
1315
#
1416
# @faker.version 0.3.0
1517
def phone_number
1618
parse('phone_number.formats')
1719
end
1820

1921
##
20-
# Produces a random cell phone number in a random format (may or may not have a country code and can have different dividers).
22+
# Produces a random cell phone number in a random format without the country code and it can have different dividers.
2123
#
2224
# @return [String]
2325
#
2426
# @example
25-
# Faker::PhoneNumber.cell_phone #=> "(186)285-7925"
27+
# Faker::PhoneNumber.cell_phone #=> "(836) 115-8995"
28+
# Faker::PhoneNumber.cell_phone #=> "382-597-5739"
29+
# Faker::PhoneNumber.cell_phone #=> "316.828.1822"
2630
#
2731
# @faker.version 1.0.0
2832
def cell_phone
@@ -36,10 +40,12 @@ def cell_phone
3640
#
3741
# @example
3842
# Faker::PhoneNumber.country_code #=> "+20"
43+
# Faker::PhoneNumber.country_code #=> "+39"
44+
# Faker::PhoneNumber.country_code #=> "+852"
3945
#
4046
# @faker.version 1.9.2
4147
def country_code
42-
"+#{fetch('country_code')}"
48+
"+#{fetch('phone_number.country_code')}"
4349
end
4450

4551
##
@@ -48,7 +54,9 @@ def country_code
4854
# @return [String]
4955
#
5056
# @example
51-
# Faker::PhoneNumber.phone_number_with_country_code #=> "+95 1-672-173-8153"
57+
# Faker::PhoneNumber.phone_number_with_country_code #=> "+55 466-746-6882"
58+
# Faker::PhoneNumber.phone_number_with_country_code #=> "+81 3718219558"
59+
# Faker::PhoneNumber.phone_number_with_country_code #=> "+49 140 957 9846"
5260
#
5361
# @faker.version 1.9.2
5462
def phone_number_with_country_code
@@ -61,70 +69,81 @@ def phone_number_with_country_code
6169
# @return [String]
6270
#
6371
# @example
64-
# Faker::PhoneNumber.cell_phone_with_country_code #=> "+974 (190) 987-9034"
72+
# Faker::PhoneNumber.cell_phone_with_country_code #=> "+852 (190) 987-9034"
73+
# Faker::PhoneNumber.cell_phone_with_country_code #=> "+64 (820) 583-6474"
74+
# Faker::PhoneNumber.cell_phone_with_country_code #=> "+1 591.871.7985"
6575
#
6676
# @faker.version 1.9.2
6777
def cell_phone_with_country_code
6878
"#{country_code} #{cell_phone}"
6979
end
7080

7181
##
72-
# Produces a random phone number in e164 format.
82+
# Produces a random phone number in e164 format, i.e., without any dividers.
7383
#
7484
# @return [String]
7585
#
7686
# @example
77-
# Faker::PhoneNumber.cell_phone_in_e164 #=> "+944937040625"
87+
# Faker::PhoneNumber.cell_phone_in_e164 #=> "+542024834991"
88+
# Faker::PhoneNumber.cell_phone_in_e164 #=> "+8522846847703"
89+
# Faker::PhoneNumber.cell_phone_in_e164 #=> "+649477546575"
7890
#
7991
# @faker.version 1.9.2
8092
def cell_phone_in_e164
8193
cell_phone_with_country_code.delete('^+0-9')
8294
end
8395

8496
##
85-
# Produces a random US or Canada-based area code.
97+
# Produces a random area code.
8698
#
8799
# @return [String]
88100
#
89101
# @example
90102
# Faker::PhoneNumber.area_code #=> "201"
103+
# Faker::PhoneNumber.area_code #=> "613"
104+
# Faker::PhoneNumber.area_code #=> "321"
91105
#
92106
# @faker.version 1.3.0
93107
def area_code
94108
fetch('phone_number.area_code')
95-
rescue I18n::MissingTranslationData
96-
nil
97109
end
98110

99111
##
100-
# Produces a random US or Canada-based exchange code.
112+
# Produces a random exchange code.
101113
#
102114
# @return [String]
103115
#
104116
# @example
105117
# Faker::PhoneNumber.exchange_code #=> "208"
118+
# Faker::PhoneNumber.exchange_code #=> "415"
119+
# Faker::PhoneNumber.exchange_code #=> "652"
106120
#
107121
# @faker.version 1.3.0
108122
def exchange_code
109123
fetch('phone_number.exchange_code')
110-
rescue I18n::MissingTranslationData
111-
nil
112124
end
113125

114126
##
115-
# Produces a random US or Canada-based extension / subscriber number. Can be used for both extensions and last four digits of phone number.
127+
# Produces a random extension / subscriber number. Can be used for both extensions and last four digits of phone number.
116128
#
117-
# @param length [Integer] Specifies the length of the return value.
129+
# @param length [Integer] Specifies the length of the return value. Defaults to 4.
118130
# @return [String]
119131
#
120132
# @example
121133
# Faker::PhoneNumber.subscriber_number #=> "3873"
122134
# Faker::PhoneNumber.subscriber_number(length: 2) #=> "39"
123135
# Faker::PhoneNumber.extension #=> "3764"
136+
# Faker::PhoneNumber.extension(length: 2) => "37"
124137
#
125138
# @faker.version 1.3.0
126139
def subscriber_number(length: 4)
127-
rand.to_s[2..(1 + length)]
140+
if !length.is_a?(Integer) || length > 10
141+
raise ArgumentError, 'length must be an Integer and be lesser than 10'
142+
end
143+
144+
generate(:string) do |g|
145+
g.int(length: length)
146+
end
128147
end
129148

130149
alias extension subscriber_number

0 commit comments

Comments
 (0)