Skip to content

Commit

Permalink
Add Faker::PhoneNumber.country_code (#1426)
Browse files Browse the repository at this point in the history
* adds country code for phone numbers and cel phones

* use interpolation for string, move country codes to phone_number file, Add and update country codes data

* update documentation with new methods

* add + sign in front of country code

* update data of country codes, replace spaces with dashes, sort  strings of country codes, add new line for text

* add test cases for new country code related functions

* update test cases, refactor code

* Update CHANGELOG.md

* Update CHANGELOG.md
  • Loading branch information
AmrAdelKhalil authored and vbrazo committed Oct 22, 2018
1 parent b36b625 commit 784c987
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## HEAD Unreleased

### Bug/Fixes
- [PR #1425](https://github.com/stympy/faker/pull/1425) NHS: fix occasional bad numbers [@ChaoticBoredom](https://github.com/ChaoticBoredom)
- [PR #1421](https://github.com/stympy/faker/pull/1421) Faker::Internet.user_name can't handle UTF-8 arguments [@ivanoblomov](https://github.com/ivanoblomov)
- [PR #1423](https://github.com/stympy/faker/pull/1423) Add missing locale tests - Part II [@vbrazo](https://github.com/vbrazo)
- [PR #1389](https://github.com/stympy/faker/pull/1389) Load faker I18n using custom backend chaining [@pjohnmeyer](https://github.com/pjohnmeyer)
- [PR #1384](https://github.com/stympy/faker/pull/1384) Quick number method bugfix [@vbrazo](https://github.com/vbrazo)
Expand All @@ -26,6 +28,8 @@
- [PR #372](https://github.com/stympy/faker/pull/372) Add test_password_could_achieve_max_length [@oleksii-ti](https://github.com/oleksii-ti)

### Deprecation
- [PR #1424](https://github.com/stympy/faker/pull/1424) Add Faker::Creature namespace [@ChaoticBoredom](https://github.com/ChaoticBoredom)
- Deprecates `::Cat` and `::Dog`
- [PR #1420](https://github.com/stympy/faker/pull/1420) Add Faker::JapaneseMedia namespace [@boardfish](https://github.com/boardfish)
- Deprecates `::DragonBall`, `::OnePiece` and `::SwordArtOnline`
- [PR #803](https://github.com/stympy/faker/pull/803) Modify Faker::Educator, Fix #576 [@ghbooth12](https://github.com/ghbooth12)
Expand All @@ -44,6 +48,7 @@
- [PR #1329](https://github.com/stympy/faker/pull/1329) Update docs on behavior of price [@softwaregravy](https://github.com/softwaregravy)

### Feature Request
- [PR #1426](https://github.com/stympy/faker/pull/1426) Add Faker::PhoneNumber.country_code [@AmrAdelKhalil](https://github.com/AmrAdelKhalil)
- [PR #1427](https://github.com/stympy/faker/pull/1427) Add Faker::Games::SuperSmashBros [@boardfish](https://github.com/boardfish)
- [PR #1410](https://github.com/stympy/faker/pull/1410) Add Faker::Vehicle.singapore_license_plate [@anonoz](https://github.com/anonoz)
- [PR #1422](https://github.com/stympy/faker/pull/1422) Add Faker::Games::SonicTheHedgehog [@boardfish](https://github.com/boardfish)
Expand Down
6 changes: 6 additions & 0 deletions doc/phone_number.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ Faker::PhoneNumber.subscriber_number #=> "3873"
Faker::PhoneNumber.subscriber_number(2) #=> "39"

Faker::PhoneNumber.extension #=> "3764"

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

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

Faker::PhoneNumber.cell_phone_with_country_code #=> "+974 (190) 987-9034"
```
12 changes: 12 additions & 0 deletions lib/faker/phone_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ def cell_phone
parse('cell_phone.formats')
end

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

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

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

# US and Canada only
def area_code
fetch('phone_number.area_code')
Expand Down
1 change: 1 addition & 0 deletions lib/locales/en/phone_number.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ en:
formats: ['###-###-####', '(###) ###-####', '1-###-###-####', '###.###.####', '###-###-####', '(###) ###-####', '1-###-###-####', '###.###.####', '###-###-#### x###', '(###) ###-#### x###', '1-###-###-#### x###', '###.###.#### x###', '###-###-#### x####', '(###) ###-#### x####', '1-###-###-#### x####', '###.###.#### x####', '###-###-#### x#####', '(###) ###-#### x#####', '1-###-###-#### x#####', '###.###.#### x#####']
cell_phone:
formats: ['###-###-####', '(###) ###-####', '1-###-###-####', '###.###.####']
country_code: ["1", "1-242", "1-246", "1-264", "1-268", "1-284", "1-340", "1-345", "1-441", "1-473", "1-649", "1-670", "1-671", "1-684", "1-758", "1-784", "1-787", "1-868", "1-869", "1-876", "1-939", "20", "212", "213", "216", "218", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230", "231", "232", "233", "234", "235", "236", "238", "238", "239", "240", "241", "242", "243", "244", "245", "247", "248", "249", "250", "251", "252", "253", "254", "255", "256", "257", "258", "260", "261", "262", "263", "264", "265", "266", "267", "268", "269", "269", "27", "290", "291", "297", "298", "299", "30", "31", "32", "33", "33", "34", "350", "351", "352", "353", "354", "355", "356", "357", "358", "359", "36", "370", "371", "372", "373", "374", "375", "376", "378", "380", "381", "381", "381", "385", "386", "387", "389", "39", "39", "40", "41", "420", "421", "423", "43", "44", "45", "46", "47", "48", "49", "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "51", "52", "53", "54", "55", "56", "57", "58", "591", "592", "593", "594", "595", "596", "596", "597", "598", "599", "60", "61", "61-8", "62", "63", "64", "65", "66", "670", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681", "682", "683", "685", "686", "687", "688", "689", "690", "691", "692", "7", "7-6", "7-7", "767", "809", "809", "809", "81", "82", "84", "850", "850", "852", "853", "855", "855", "856", "86", "880", "886", "886", "90", "91", "92", "93", "94", "95", "960", "961", "962", "963", "964", "965", "966", "967", "968", "971", "972", "973", "974", "975", "976", "977", "98", "993", "994", "995", "996"]
21 changes: 21 additions & 0 deletions test/test_faker_phone_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

require_relative 'test_helper'

class TestFakerPhone < Test::Unit::TestCase
def setup
@tester = Faker::PhoneNumber
@phone_with_country_code_regex = /\A\+[\s|\d|\-|\(|\)|x|\.]*\z/
end

def test_country_code
assert @tester.country_code.match(/\A\+[\d|-]+\z/)
end

def test_phone_number_with_country_code
assert @tester.phone_number_with_country_code.match(@phone_with_country_code_regex)
end

def test_cell_phone_with_country_code
assert @tester.cell_phone_with_country_code.match(@phone_with_country_code_regex)
end
end

0 comments on commit 784c987

Please sign in to comment.