Class: JWT::Encode

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt/encode.rb

Overview

The Encode class is responsible for encoding JWT tokens.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Encode

Initializes a new Encode instance.

Parameters:

  • options (Hash)

    the options for encoding the JWT token.

Options Hash (options):

  • :payload (Hash)

    the payload of the JWT token.

  • :headers (Hash)

    the headers of the JWT token.

  • :key (String)

    the key used to sign the JWT token.

  • :algorithm (String)

    the algorithm used to sign the JWT token.



15
16
17
18
19
# File 'lib/jwt/encode.rb', line 15

def initialize(options)
  @token     = Token.new(payload: options[:payload], header: options[:headers])
  @key       = options[:key]
  @algorithm = options[:algorithm]
end

Instance Method Details

#segmentsString

Encodes the JWT token and returns its segments.

Returns:

  • (String)

    the encoded JWT token.



24
25
26
27
28
# File 'lib/jwt/encode.rb', line 24

def segments
  @token.verify_claims!(:numeric)
  @token.sign!(algorithm: @algorithm, key: @key)
  @token.jwt
end