Skip to content

Commit 6a6618a

Browse files
committed
Transforms::Base64: add padding option and docs
1 parent 44ef255 commit 6a6618a

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/moneta/transforms/base64.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
1-
require 'base64'
1+
require "base64"
22

33
module Moneta
44
module Transforms
55
# Encodes text as Base64 using the stdlib +base64+ library
66
class Base64 < Transform
7-
def initialize(url_safe: false, strict: false, **options)
7+
# @param url_safe [Boolean] If +true+, URL-safe base64 will be used instead.
8+
# @param strict [Boolean] By default the resulting string has line breaks in it. Specifying
9+
# +strict: true+ will remove line breaks.
10+
# @param padding [Boolean] This can be set to +false+ in conjuction with +url_safe: true+ to output unpadded
11+
# URL-safe Base64
12+
def initialize(url_safe: false, strict: false, padding: true, **options)
813
super
914

1015
raise "Cannot use strict and url_safe together" if url_safe && strict
16+
raise "padding: false is only valid with url_safe: true" if !padding && !url_safe
17+
1118
@url_safe = url_safe
1219
@strict = strict
20+
@padding = padding
1321
end
1422

23+
# Encode string to Base64
24+
#
25+
# @param value [String]
26+
# @return [String]
1527
def encode(value)
1628
if @url_safe
17-
::Base64.urlsafe_encode64(value)
29+
::Base64.urlsafe_encode64(value, padding: @padding)
1830
elsif @strict
1931
::Base64.strict_encode64(value)
2032
else
2133
::Base64.encode64(value)
2234
end
2335
end
2436

37+
# Decode from Base64
38+
#
39+
# @param value [String]
40+
# @return [String]
2541
def decode(value)
2642
if @url_safe
2743
::Base64.urlsafe_decode64(value)

0 commit comments

Comments
 (0)