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

Re-add the Faker::Internet.base64 method #2378

Merged
merged 2 commits into from Sep 23, 2021
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
29 changes: 29 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -530,6 +530,35 @@ def uuid
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

##
# Produces a random string of alphabetic characters, (no digits)
#
# @param length [Integer] The length of the string to generate
# @param padding [Boolean] Toggles if a final equal '=' will be added.
# @param urlsafe [Boolean] Toggles charset to '-' and '_' instead of '+' and '/'.
#
# @return [String]
#
# @example
# Faker::Internet.base64
# #=> "r_hbZ2DSD-ZACzZT"
# @example
# Faker::Internet.base64(length: 4, padding: true, urlsafe: false)
# #=> "x1/R="
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add faker.version since the 2.11.0 series (https://github.com/faker-ruby/faker/blob/master/CHANGELOG.md#v2110-2020-03-24) except the latest one where it was removed mistakely ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this @faker.version 2.11.0 here?

Copy link
Contributor Author

@ashishra0 ashishra0 Aug 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@psibi added the faker.version do check if it's the appropriate one :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if it's available in v2.19.0 ? (If it's not, probably you can add that information in the docs here)

#
# @faker.version 2.11.0
def base64(length: 16, padding: false, urlsafe: true)
char_range = [
Array('0'..'9'),
Array('A'..'Z'),
Array('a'..'z'),
urlsafe ? %w[- _] : %w[+ /]
].flatten
s = Array.new(length) { sample(char_range) }.join
s += '=' if padding
s
end

##
# Produces a randomized hash of internet user details
# @example
Expand Down
7 changes: 7 additions & 0 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -333,6 +333,13 @@ def test_uuid
assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid)
end

def test_base64
assert_match(/[[[:alnum:]]\-_]{16}/, @tester.base64)
assert_match(/[[[:alnum:]]\-_]{4}/, @tester.base64(length: 4))
assert_match(/[[[:alnum:]]\-_]{16}=/, @tester.base64(padding: true))
assert_match(/[[[:alnum:]]+\/]{16}/, @tester.base64(urlsafe: false))
end

def test_user_with_args
user = @tester.user('username', 'email', 'password')
assert user[:username].match(/[a-z]+((_|\.)[a-z]+)?/)
Expand Down