Skip to content

Commit

Permalink
Makes minor refactors on Internet.domain_name method (faker-ruby#1847)
Browse files Browse the repository at this point in the history
* Makes minor refactors on Internet.domain_name method

* Fixes block-code style (rubocop complaints)

* Fixes rubocop offense on method call
  • Loading branch information
tiagofsilva authored and vbrazo committed Dec 7, 2019
1 parent 0fc277e commit 5456744
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,19 @@ def domain_name(legacy_subdomain = NOT_GIVEN, subdomain: false, domain: nil)
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
domain
.split('.')
.map { |domain_part| Char.prepare(domain_part) }
.tap do |domain_elements|
domain_elements << domain_suffix if domain_elements.length < 2
domain_elements.unshift(Char.prepare(domain_word)) if subdomain && domain_elements.length < 3
end.join('.')
else
domain_elements << domain_word
domain_elements << domain_suffix
domain_elements.unshift(Char.prepare(domain_word)) if subdomain
[domain_word, domain_suffix].tap do |domain_elements|
domain_elements.unshift(Char.prepare(domain_word)) if subdomain
end.join('.')
end

domain_elements.join('.')
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ 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_name_with_subdomain_and_with_domain_option_given_with_domain_suffixc
def test_domain_name_with_subdomain_and_with_domain_option_given_with_domain_suffix
assert @tester.domain_name(subdomain: true, domain: 'customdomain.customdomainsuffix').match(/customdomain\.customdomainsuffix/)
end

Expand Down

0 comments on commit 5456744

Please sign in to comment.