class Compeon::Token::Encoder

Attributes

key[R]
token[R]

Public Class Methods

new(key:, token:) click to toggle source
# File lib/compeon/token/encoder.rb, line 10
def initialize(key:, token:)
  @token = token
  @key = key

  raise 'No key given.' if @key.nil?
  raise 'Token is invalid.' unless @token.valid?
end

Public Instance Methods

encode() click to toggle source
# File lib/compeon/token/encoder.rb, line 18
def encode
  JWT.encode(
    {
      **attributes,
      **token.registered_claims,
      knd: token.class.kind
    },
    key,
    token.class.jwt_algorithm
  )
rescue JWT::EncodeError
  raise EncodeError
end

Private Instance Methods

attributes() click to toggle source
# File lib/compeon/token/encoder.rb, line 36
def attributes
  token
    .class
    .attributes_mapping
    .invert
    .transform_values { |attribute| token.public_send(attribute) }
    .reject { |attribute, value| token.class.optional_attributes.include?(attribute) && value.nil? }
end