File tree Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change 1- require ' base64'
1+ require " base64"
22
33module 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 )
You can’t perform that action at this time.
0 commit comments