class Virgil::Jwt::Bytes

Public Class Methods

from_base64(str) click to toggle source

Initializes a new array of bytes from specified string, which encodes binary data as base-64 digits.

# File lib/virgil/jwt/bytes.rb, line 84
def self.from_base64(str)
  new(Base64.decode64(str).bytes)
end
from_hex(str) click to toggle source

Initializes a new array of bytes from specified string, which encodes binary data as hexadecimal digits.

# File lib/virgil/jwt/bytes.rb, line 96
def self.from_hex(str)
  new(str.scan(/../).map { |x| x.hex })
end
from_string(str, encoding = VirgilStringEncoding::UTF8) click to toggle source

Initializes a new array of bytes from specified string, which encodes binary data. @param str [String] String to decode. @param encoding [VirgilStringEncoding] The character encoding of string. @raise [ArgumentError] if encoding is undefined

# File lib/virgil/jwt/bytes.rb, line 45
def self.from_string(str, encoding = VirgilStringEncoding::UTF8)
  case encoding
  when VirgilStringEncoding::BASE64
    from_base64(str)
  when VirgilStringEncoding::HEX
    from_hex(str)
  when VirgilStringEncoding::UTF8
    from_utf8(str)
  else
    raise ArgumentError, 'Encoding is undefined'
  end
end
from_utf8(str) click to toggle source

Initializes a new array of bytes from specified string, which encodes binary data as utf8.

# File lib/virgil/jwt/bytes.rb, line 90
def self.from_utf8(str)
  new(str.bytes)
end

Public Instance Methods

to_base64() click to toggle source

Converts all the bytes to its equivalent string representation that is encoded with base-64 digits.

# File lib/virgil/jwt/bytes.rb, line 102
def to_base64
  Base64.strict_encode64(to_s)
end
to_hex() click to toggle source

Converts the numeric value of each element of a current array of bytes to its equivalent hexadecimal string representation.

# File lib/virgil/jwt/bytes.rb, line 113
def to_hex
  to_s.each_byte.map { |b| b.to_s(16) }.join
end
to_s() click to toggle source

Converts all the bytes to its equivalent string representation in utf8.

# File lib/virgil/jwt/bytes.rb, line 78
def to_s
  pack('c*')
end
to_string(encoding = VirgilStringEncoding::UTF8) click to toggle source

Decodes the current bytes to a string according to the specified character encoding. @param encoding [VirgilStringEncoding] The character encoding to encode to.

equivalent string representation if raw bytes in selected encoding.

@return [String] @raise [ArgumentError] if encoding is undefined

# File lib/virgil/jwt/bytes.rb, line 64
def to_string(encoding = VirgilStringEncoding::UTF8)
  case encoding
  when VirgilStringEncoding::BASE64
    to_base64
  when VirgilStringEncoding::HEX
    to_hex
  when VirgilStringEncoding::UTF8
    to_s
  else
    raise ArgumentError, 'Encoding is undefined'
  end
end
to_utf8() click to toggle source

Encodes all the bytes into a utf8 string.

# File lib/virgil/jwt/bytes.rb, line 107
def to_utf8
  to_s
end