Module: EncodedToken::Encoder
- Extended by:
- ErrorMessages
- Defined in:
- lib/encoded_token/encoder.rb,
lib/encoded_token/encoder/utf8_decoder.rb,
lib/encoded_token/encoder/utf8_encoder.rb,
lib/encoded_token/encoder/legacy_decoder.rb,
lib/encoded_token/encoder/legacy_encoder.rb
Overview
EncodedToken::Encoder
This module handles the encoding and decoding of tokens.
Defined Under Namespace
Modules: LegacyDecoder, LegacyEncoder, Utf8Decoder, Utf8Encoder
Constant Summary
Constants included from ErrorMessages
EncodedToken::ErrorMessages::CIPHER_ERRORS, EncodedToken::ErrorMessages::CONFIG_ERRORS, EncodedToken::ErrorMessages::ENCODED_TOKEN_ERRORS, EncodedToken::ErrorMessages::ENCODER_ERRORS, EncodedToken::ErrorMessages::ENCRYPTOR_ERRORS, EncodedToken::ErrorMessages::ERROR_MESSAGES, EncodedToken::ErrorMessages::V1_ENCODER_ERRORS
Class Method Summary collapse
-
.build_ciphers! ⇒ void
Builds the encoding and decoding ciphers based on the current configuration.
-
.ciphers_built? ⇒ Boolean
Checks if the ciphers have been built.
-
.decode(token) ⇒ String?
Decodes a given token to the original string.
-
.decode!(token) ⇒ String
Decodes a given token to the original string, raising an exception if encountered.
-
.decoding_ciphers ⇒ Array<Cipher>
Returns a list of duplicates of the current decoding ciphers.
-
.encode(input, encoder = :utf8) ⇒ String?
Encodes a given input to an encoded token.
-
.encode!(input, encoder = :utf8) ⇒ String
Encodes a given input to an encoded token, raising an exception if encountered.
-
.encoding_cipher ⇒ Cipher
Returns a duplicate of the current encoding cipher.
-
.max_input_size ⇒ Integer
Returns the maximum input size from configuration.
-
.min_token_size ⇒ Integer
Returns the minimum token size from configuration.
Class Method Details
.build_ciphers! ⇒ void
This method returns an undefined value.
Builds the encoding and decoding ciphers based on the current configuration.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/encoded_token/encoder.rb', line 168 def build_ciphers! default_seed = EncodedToken.configuration.seed || fail_with(:seed_not_set) @encoding_cipher = Cipher.new(default_seed) @decoding_ciphers = [@encoding_cipher] EncodedToken.configuration.expiring_seeds.each do |exp_seed, details| cipher = Cipher.new(exp_seed, details[:expires_on], details[:cipher_count]) @decoding_ciphers << cipher unless cipher.expired? end end |
.ciphers_built? ⇒ Boolean
Checks if the ciphers have been built.
30 31 32 |
# File 'lib/encoded_token/encoder.rb', line 30 def ciphers_built? !!encoding_cipher end |
.decode(token) ⇒ String?
Decodes a given token to the original string.
130 131 132 133 134 |
# File 'lib/encoded_token/encoder.rb', line 130 def decode(token) decode!(token) rescue nil end |
.decode!(token) ⇒ String
Decodes a given token to the original string, raising an exception if encountered.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/encoded_token/encoder.rb', line 149 def decode!(token) fail_with(:seed_not_set) unless encoding_cipher fail_with(:non_string_token) unless token.is_a?(String) if token[-1] == '_' Utf8Decoder.decode(token) else LegacyDecoder.decode(token) end end |
.decoding_ciphers ⇒ Array<Cipher>
Returns a list of duplicates of the current decoding ciphers.
50 51 52 53 |
# File 'lib/encoded_token/encoder.rb', line 50 def decoding_ciphers @decoding_ciphers ||= [] @decoding_ciphers.map{ |cipher| cipher.dup } end |
.encode(input, encoder = :utf8) ⇒ String?
Encodes a given input to an encoded token.
86 87 88 89 90 |
# File 'lib/encoded_token/encoder.rb', line 86 def encode(input, encoder = :utf8) encode!(input, encoder) rescue StandardError nil end |
.encode!(input, encoder = :utf8) ⇒ String
Encodes a given input to an encoded token, raising an exception if encountered.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/encoded_token/encoder.rb', line 106 def encode!(input, encoder = :utf8) fail_with(:seed_not_set) unless encoding_cipher case encoder when :utf8 Encoder::Utf8Encoder.encode(input) when :legacy Encoder::LegacyEncoder.encode(input) else fail_with(:invalid_encoder) end end |
.encoding_cipher ⇒ Cipher
Returns a duplicate of the current encoding cipher.
40 41 42 |
# File 'lib/encoded_token/encoder.rb', line 40 def encoding_cipher @encoding_cipher.dup end |
.max_input_size ⇒ Integer
Returns the maximum input size from configuration.
71 72 73 |
# File 'lib/encoded_token/encoder.rb', line 71 def max_input_size EncodedToken.configuration.max_input_size end |
.min_token_size ⇒ Integer
Returns the minimum token size from configuration.
61 62 63 |
# File 'lib/encoded_token/encoder.rb', line 61 def min_token_size EncodedToken.configuration.min_token_size end |