class TJSON::DataType::Binary32

Base32-serialized binary data

Public Instance Methods

decode(str) click to toggle source
# File lib/tjson/datatype/binary.rb, line 30
def decode(str)
  raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
  raise TJSON::ParseError, "base32 must be lower case: #{str.inspect}" if str =~ /[A-Z]/
  raise TJSON::ParseError, "padding disallowed: #{str.inspect}" if str.include?("=")
  raise TJSON::ParseError, "invalid base32: #{str.inspect}" unless str =~ /\A[a-z2-7]*\z/

  ::Base32.decode(str.upcase).force_encoding(Encoding::BINARY)
end
encode(binary) click to toggle source
# File lib/tjson/datatype/binary.rb, line 39
def encode(binary)
  Base32.encode(binary).downcase.delete("=")
end
tag() click to toggle source
# File lib/tjson/datatype/binary.rb, line 26
def tag
  "d32"
end