Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add domain_suffix option to internet .email and .domain_name #1856

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions doc/default/internet.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Faker::Internet

```ruby
# Keyword arguments: name, separators, domain
# Keyword arguments: name, separators, subdomain, domain, domain_suffix
Faker::Internet.email #=> "eliza@mann.net"
Faker::Internet.email(name: 'Nancy') #=> "nancy@terry.biz"
Faker::Internet.email(name: 'Janelle Santiago', separators: '+') #=> janelle+santiago@becker.com"
Faker::Internet.email(domain: 'example') #=> alice@example.name"
Faker::Internet.email(subdomain: true) #=> alice@keebler.sons.name"
Faker::Internet.email(domain_suffix: 'xyz') #=> alice@terry.xyz"
Faker::Internet.email(domain: 'company.example', domain_suffix: 'xyz') #=> alice@company.example.xyz"
Faker::Internet.email(subdomain: true, domain: 'company.example', domain_suffix: 'xyz') #=> alice@keebler.company.example.xyz"

# Keyword arguments: name
Faker::Internet.free_email #=> "freddy@gmail.com"
Expand Down Expand Up @@ -34,10 +38,12 @@ Faker::Internet.password(min_length: 10, max_length: 20) #=> "EoC9ShWd1hWq4vBgFw
Faker::Internet.password(min_length: 10, max_length: 20, mix_case: true) #=> "3k5qS15aNmG"
Faker::Internet.password(min_length: 10, max_length: 20, mix_case: true, special_characters: true) #=> "*%NkOnJsH4"

# Keyword arguments: subdomain, domain
# Keyword arguments: subdomain, domain, domain_suffix
Faker::Internet.domain_name #=> "effertz.info"
Faker::Internet.domain_name(domain: "example") #=> "example.net"
Faker::Internet.domain_name(subdomain: true, domain: "example") #=> "horse.example.org"
Faker::Internet.domain_name(domain: "company.example", domain_suffix: "xyz") #=> "company.example.xyz"
Faker::Internet.domain_name(subdomain: true, domain: "company.example", domain_suffix: "xyz") #=> "hello.company.example.xyz"

Faker::Internet.domain_word #=> "haleyziemann"

Expand Down
42 changes: 22 additions & 20 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
module Faker
class Internet < Base
class << self
def email(legacy_name = NOT_GIVEN, legacy_separators = NOT_GIVEN, name: nil, separators: nil, domain: nil)
# rubocop:disable Metrics/ParameterLists
def email(legacy_name = NOT_GIVEN, legacy_separators = NOT_GIVEN, name: nil, separators: nil, domain: nil, domain_suffix: nil, subdomain: false)
# rubocop:enable Metrics/ParameterLists
warn_for_deprecated_arguments do |keywords|
keywords << :name if legacy_name != NOT_GIVEN
keywords << :separators if legacy_separators != NOT_GIVEN
end

if separators
[username(specifier: name, separators: separators), domain_name(domain: domain)].join('@')
else
[username(specifier: name), domain_name(domain: domain)].join('@')
end
given_username = if separators
username(specifier: name, separators: separators)
else
username(specifier: name)
end
[given_username, domain_name(domain: domain, domain_suffix: domain_suffix, subdomain: subdomain)].join('@')
end

def free_email(legacy_name = NOT_GIVEN, name: nil)
Expand Down Expand Up @@ -135,26 +138,19 @@ def password(legacy_min_length = NOT_GIVEN, legacy_max_length = NOT_GIVEN, legac
temp
end

def domain_name(legacy_subdomain = NOT_GIVEN, subdomain: false, domain: nil)
def domain_name(legacy_subdomain = NOT_GIVEN, subdomain: false, domain: nil, domain_suffix: nil)
warn_for_deprecated_arguments do |keywords|
keywords << :subdomain if legacy_subdomain != NOT_GIVEN
end

with_locale(:en) do
domain_elements = []

if domain
domain.split('.').each do |domain_part|
domain_elements << Char.prepare(domain_part)
end
domain_elements << domain_suffix if domain_elements.length < 2
domain_elements.unshift(Char.prepare(domain_word)) if subdomain && domain_elements.length < 3
else
domain_elements << domain_word
domain_elements << domain_suffix
domain_elements.unshift(Char.prepare(domain_word)) if subdomain
end
given_domain = domain || domain_word
given_suffix = domain_suffix || self.domain_suffix

domain_elements = []
domain_elements << Char.prepare(domain_word) if subdomain
domain_elements << perpare_domain_part(given_domain)
domain_elements << perpare_domain_part(given_suffix)
domain_elements.join('.')
end
end
Expand All @@ -175,6 +171,12 @@ def domain_suffix
fetch('internet.domain_suffix')
end

def perpare_domain_part(domain_part)
domain_part.split('.')
.map { |word| Char.prepare(word) }
.join('.')
end

def mac_address(legacy_prefix = NOT_GIVEN, prefix: '')
warn_for_deprecated_arguments do |keywords|
keywords << :prefix if legacy_prefix != NOT_GIVEN
Expand Down
44 changes: 32 additions & 12 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ def test_email_with_separators
assert @tester.email(name: 'jane doe', separators: '+').match(/.+\+.+@.+\.\w+/)
end

def test_email_with_domain_option_given
assert @tester.email(name: 'jane doe', domain: 'customdomain').match(/.+@customdomain\.\w+/)
end

def test_email_with_domain_option_given_with_domain_suffix
assert @tester.email(name: 'jane doe', domain: 'customdomain.customdomainsuffix').match(/.+@customdomain\.customdomainsuffix/)
end

def test_free_email
assert @tester.free_email.match(/.+@(gmail|hotmail|yahoo)\.com/)
end
Expand Down Expand Up @@ -158,12 +150,40 @@ def test_domain_name_with_subdomain
assert @tester.domain_name(subdomain: true).match(/\w+\.\w+\.\w+/)
end

def test_domain_name_with_subdomain_and_with_domain_option_given
assert @tester.domain_name(subdomain: true, domain: 'customdomain').match(/customdomain\.\w+/)
def test_domain_name_with_domain_suffix
assert @tester.domain_name(domain_suffix: 'domain_suffix').match(/\w+\.domain_suffix/)
end

def test_domain_name_with_subdomain_with_domain_suffix
assert @tester.domain_name(subdomain: true, domain_suffix: 'domain_suffix').match(/\w+\.\w+\.domain_suffix/)
end

def test_domain_name_with_subdomain_with_complex_domain_suffix
assert @tester.domain_name(subdomain: true, domain_suffix: 'my.domain.suffix').match(/\w+\.\w+\.my.domain.suffix/)
end

def test_email_with_domain_option_given
assert @tester.email(domain: 'custom_domain').match(/.+@custom_domain\.\w+/)
end

def test_email_with_domain_suffix_option_given
assert @tester.email(domain_suffix: 'custom_domain_suffix').match(/.+custom_domain_suffix/)
end

def test_email_with_domain_and_domain_suffix_option_given
assert @tester.email(domain: 'custom_domain', domain_suffix: 'custom_domain_suffix').match(/.+@custom_domain\.custom_domain_suffix+/)
end

def test_email_with_subdomain_and_domain_suffix_option_given
assert @tester.email(subdomain: true, domain_suffix: 'custom_domain_suffix').match(/.+@\w+\.\w+\.custom_domain_suffix/)
end

def test_email_with_subdomain_and_domain_and_domain_suffix_option_given
assert @tester.email(subdomain: true, domain: 'custom_domain', domain_suffix: 'custom_domain_suffix').match(/.+@\w+\.custom_domain\.custom_domain_suffix/)
end

def test_domain_name_with_subdomain_and_with_domain_option_given_with_domain_suffixc
assert @tester.domain_name(subdomain: true, domain: 'customdomain.customdomainsuffix').match(/customdomain\.customdomainsuffix/)
def test_email_with_custom_subdomain_and_custom_suffix_option_given
assert @tester.email(domain: 'custom_subdomain.custom_domain', domain_suffix: 'custom.domain.suffix').match(/.+@custom_subdomain\.custom_domain\.custom\.domain\.suffix/)
end

def test_domain_word
Expand Down