Skip to content

Commit

Permalink
Transforms::Base64: add padding option and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asppsa committed Apr 22, 2023
1 parent 44ef255 commit 6a6618a
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/moneta/transforms/base64.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
require 'base64'
require "base64"

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

raise "Cannot use strict and url_safe together" if url_safe && strict
raise "padding: false is only valid with url_safe: true" if !padding && !url_safe

@url_safe = url_safe
@strict = strict
@padding = padding
end

# Encode string to Base64
#
# @param value [String]
# @return [String]
def encode(value)
if @url_safe
::Base64.urlsafe_encode64(value)
::Base64.urlsafe_encode64(value, padding: @padding)
elsif @strict
::Base64.strict_encode64(value)
else
::Base64.encode64(value)
end
end

# Decode from Base64
#
# @param value [String]
# @return [String]
def decode(value)
if @url_safe
::Base64.urlsafe_decode64(value)
Expand Down

0 comments on commit 6a6618a

Please sign in to comment.