Class: Compeon::Token::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/compeon/token/encoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:, token:) ⇒ Encoder

Returns a new instance of Encoder.



10
11
12
13
14
15
16
# 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

Instance Method Details

#encodeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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