diff --git a/doc/default/internet.md b/doc/default/internet.md index d702e6e4dd..36d847fcb3 100644 --- a/doc/default/internet.md +++ b/doc/default/internet.md @@ -71,4 +71,18 @@ Faker::Internet.user_agent #=> "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLB Faker::Internet.user_agent(vendor: :firefox) #=> "Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0" Faker::Internet.uuid #=> "929ef6ef-b11f-38c9-111b-accd67a258b2" + +Faker::Internet.personal_account #=> {:username=>"sabina", :password=>"Yh3Oz73Na4LaFx", :free_email=>"viola.block@hotmail.com", :safe_email=>"shanti@example.org", :email=>"nella@schaefer.co"} + +# Keyword arguments: options +Faker::Internet.personal_account({username: {specifier: 'Nancy'}}) #=> {:username=>"nancy", :password=>"UaGbCf5OdLxV", :free_email=>"antonia@yahoo.com", :safe_email=>"lavern_crooks@example.net", :email=>"lila_gottlieb@haley.biz"} + +# Keyword arguments: attributes +Faker::Internet.personal_account(:username, 'password') #=> {:username=>"brant", :password=>"Hr85Wf4L1uQpHl"} + +# Keyword arguments: attributes, options +Faker::Internet.personal_account(:username, :safe_email, :free_email, :email, :password, {username: {specifier: 8}, safe_email: {name: 'Nancy'}, free_email: {name: 'Nancy'}, email: {name: 'Janelle Santiago', separators: '+'}, password: {min_length: 10, max_length: 20, mix_case: true, special_characters: true}}) #=> {:username=>"willettewillette", :safe_email=>"nancy@example.net", :free_email=>"nancy@hotmail.com", :email=>"santiago+janelle@thiel.com", :password=>"!!&*$%Rt74261I41z"} + + + ``` diff --git a/lib/faker/default/internet.rb b/lib/faker/default/internet.rb index 8ac771e9fa..53b4357e58 100644 --- a/lib/faker/default/internet.rb +++ b/lib/faker/default/internet.rb @@ -282,6 +282,46 @@ def uuid '%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString end + ## + # Produces a hash of personal account attributes + # + # @param attributes [Array] array of required fields + # + # @param options [Hash] apply specific constraints on the data returned + # + # + # @return [Hash] + # + # @example + # Faker::Internet.personal_account #=> {:username=>"sabina", :password=>"Yh3Oz73Na4LaFx", :free_email=>"viola.block@hotmail.com", :safe_email=>"shanti@example.org", :email=>"nella@schaefer.co"} + # @example + # Faker::Internet.personal_account(:username) #=> {:username=>"wilber"} + # + # @example + # Faker::Internet.personal_account({username: {specifier: 'Nancy'}}) #=> {:username=>"nancy", :password=>"UaGbCf5OdLxV", :free_email=>"antonia@yahoo.com", :safe_email=>"lavern_crooks@example.net", :email=>"lila_gottlieb@haley.biz"} + # @example + # Faker::Internet.personal_account(:username, 'password') #=> {:username=>"brant", :password=>"Hr85Wf4L1uQpHl"} + # @example + # Faker::Internet.personal_account(:username, :safe_email, :free_email, :email, :password, {username: {specifier: 8}, safe_email: {name: 'Nancy'}, free_email: {name: 'Nancy'}, email: {name: 'Janelle Santiago', separators: '+'}, password: { min_length: 10, max_length: 20, mix_case: true, special_characters: true }}) #=> {:username=>"willettewillette", :safe_email=>"nancy@example.net", :free_email=>"nancy@hotmail.com", :email=>"santiago+janelle@thiel.com", :password=>"!!&*$%Rt74261I41z"} + # + # + # @note This method will return all the options available for *attributes parameter as a default. + # + # @faker.version 2.3.0 + def personal_account(*attributes, **options) + attributes = %i[username password free_email safe_email email] if attributes.length.zero? + attributes = attributes.map(&:to_sym) + options = options.each_with_object({}) do |(attr, attr_options), symbolized_options| + attr_options = attr_options.each_with_object({}) do |(key, value), new_options| + new_options[key.to_sym] = value + new_options + end + symbolized_options[attr.to_sym] = attr_options + symbolized_options + end + Hash[attributes.map { |attribute| [attribute, send(attribute.to_sym, **(options[attribute.to_sym] || {}))] }] + end + alias user_name username end end diff --git a/test/faker/default/test_faker_internet.rb b/test/faker/default/test_faker_internet.rb index 5590dfb74a..f35e97bc80 100644 --- a/test/faker/default/test_faker_internet.rb +++ b/test/faker/default/test_faker_internet.rb @@ -295,4 +295,24 @@ def test_uuid assert_equal(36, uuid.size) assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid) end + + def test_user_with_args + user = @tester.personal_account(:username, 'email', 'password', :free_email, :safe_email, password: { min_length: 3, max_length: 5 }) + assert user[:email].match(/.+@.+\.\w+/) + assert user[:password].length.between?(3, 5) + assert user[:username] + assert user[:free_email] + assert user[:safe_email] + end + + def test_user_without_args + user = @tester.personal_account + assert user[:email].match(/.+@.+\.\w+/) + end + + def test_user_with_invalid_args + assert_raises NoMethodError do + @tester.personal_account('xyz') + end + end end