Class: JWTEasy::Encoder
- Inherits:
-
Object
- Object
- JWTEasy::Encoder
- Defined in:
- lib/jwt_easy/encoder.rb
Overview
Encoder object for generating new tokens.
-
This is usually not instantiated directly, but rather by way of calling
JWTEasy.encode
.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#encode ⇒ String
Encodes the data with the configured options.
-
#expiration_time ⇒ Integer
Calculates the expiration time if configured.
-
#initialize(data, configuration = nil) ⇒ Encoder
constructor
Initializes a new encoder instance.
-
#not_before_time ⇒ Integer
Calculates the not before time if configured.
-
#payload ⇒ Object
Determines the structure of the payload to be encoded.
Constructor Details
#initialize(data, configuration = nil) ⇒ Encoder
Initializes a new encoder instance.
-
If no configuration object is passed or is
nil
, the value ofJWTEasy.configuration
is used as the configuration object
19 20 21 22 |
# File 'lib/jwt_easy/encoder.rb', line 19 def initialize(data, configuration = nil) @data = data @configuration = configuration || JWTEasy.configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
9 10 11 |
# File 'lib/jwt_easy/encoder.rb', line 9 def configuration @configuration end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/jwt_easy/encoder.rb', line 9 def data @data end |
Instance Method Details
#encode ⇒ String
Encodes the data with the configured options.
27 28 29 |
# File 'lib/jwt_easy/encoder.rb', line 27 def encode JWT.encode(payload, configuration.secret, configuration.algorithm) end |
#expiration_time ⇒ Integer
Calculates the expiration time if configured.
48 49 50 |
# File 'lib/jwt_easy/encoder.rb', line 48 def expiration_time Time.now.to_i + configuration.expiration_time end |
#not_before_time ⇒ Integer
Calculates the not before time if configured.
55 56 57 |
# File 'lib/jwt_easy/encoder.rb', line 55 def not_before_time Time.now.to_i - configuration.not_before_time end |
#payload ⇒ Object
Determines the structure of the payload to be encoded.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jwt_easy/encoder.rb', line 34 def payload case configuration.claim when CLAIM_EXPIRATION_TIME { data: data, exp: expiration_time } when CLAIM_NOT_BEFORE_TIME { data: data, nbf: not_before_time } else data end end |