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

Adds domain option for Internet email and domain_name methods #1808

Merged
Merged
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
6 changes: 5 additions & 1 deletion doc/default/internet.md
@@ -1,10 +1,11 @@
# Faker::Internet

```ruby
# Keyword arguments: name, separators
# Keyword arguments: name, separators, domain
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.com') #=> alice@example.com"

# Keyword arguments: name
Faker::Internet.free_email #=> "freddy@gmail.com"
Expand Down Expand Up @@ -32,7 +33,10 @@ 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
Faker::Internet.domain_name #=> "effertz.info"
Faker::Internet.domain_name(domain: "example.com") #=> "example.com"
Faker::Internet.domain_name(subdomain: true, domain: "example.com") #=> "horse.example.com"

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

Expand Down
13 changes: 7 additions & 6 deletions lib/faker/default/internet.rb
Expand Up @@ -3,16 +3,16 @@
module Faker
class Internet < Base
class << self
def email(legacy_name = NOT_GIVEN, legacy_separators = NOT_GIVEN, name: nil, separators: nil)
def email(legacy_name = NOT_GIVEN, legacy_separators = NOT_GIVEN, name: nil, separators: nil, domain: nil)
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].join('@')
[username(specifier: name, separators: separators), domain_name(domain: domain)].join('@')
else
[username(specifier: name), domain_name].join('@')
[username(specifier: name), domain_name(domain: domain)].join('@')
end
end

Expand Down Expand Up @@ -135,14 +135,15 @@ def password(legacy_min_length = NOT_GIVEN, legacy_max_length = NOT_GIVEN, legac
temp
end

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

with_locale(:en) do
domain_elements = [Char.prepare(domain_word), domain_suffix]
domain_elements.unshift(Char.prepare(domain_word)) if subdomain
given_domain_word = domain || domain_word
domain_elements = [Char.prepare(given_domain_word), domain_suffix]
domain_elements.unshift(Char.prepare(given_domain_word)) if subdomain
domain_elements.join('.')
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -15,6 +15,10 @@ 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_free_email
assert @tester.free_email.match(/.+@(gmail|hotmail|yahoo)\.com/)
end
Expand Down Expand Up @@ -150,6 +154,10 @@ 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+/)
end

def test_domain_word
assert @tester.domain_word.match(/^\w+$/)
end
Expand Down