class TJSON::DataType::Binary64

Base64-serialized binary data

Public Instance Methods

decode(str) click to toggle source
# File lib/tjson/datatype/binary.rb, line 50
def decode(str)
  raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
  raise TJSON::ParseError, "base64url only: #{str.inspect}" if str =~ %r{\+|\/}
  raise TJSON::ParseError, "padding disallowed: #{str.inspect}" if str.include?("=")
  raise TJSON::ParseError, "invalid base64url: #{str.inspect}" unless str =~ /\A[A-Za-z0-9\-_]*\z/

  # Add padding, as older Rubies (< 2.3) require it
  str = str.ljust((str.length + 3) & ~3, "=") if (str.length % 4).nonzero?

  ::Base64.urlsafe_decode64(str)
end
encode(binary) click to toggle source
# File lib/tjson/datatype/binary.rb, line 62
def encode(binary)
  Base64.urlsafe_encode64(binary).delete("=")
end
tag() click to toggle source
# File lib/tjson/datatype/binary.rb, line 46
def tag
  "d64"
end