lib/faker/default/internet.rb in faker-2.10.2 vs lib/faker/default/internet.rb in faker-2.11.0

- old
+ new

@@ -293,9 +293,38 @@ ary[2] = (ary[2] & 0x0fff) | 0x4000 ary[3] = (ary[3] & 0x3fff) | 0x8000 '%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=" + # + # @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 + alias user_name username end end end