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

Class Method Details

.build_ciphers!void

This method returns an undefined value.

Builds the encoding and decoding ciphers based on the current configuration.

Raises:

  • (RuntimeError)

    if the seed is not set in the 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.

Returns:

  • (Boolean)

    true if the encoding cipher is set, false otherwise.



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.

Parameters:

  • token (String)

    an encoded-token string

Returns:

  • (String)

    the decoded value if successful

  • (nil)

    if unsuccessful



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.

Parameters:

  • token (String)

    an encoded-token string

Returns:

  • (String)

    the decoded value

Raises:

  • (ArgumentError, RuntimeError)

    if the decoding fails, if the token is not a string, or if the seed is not set.



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_ciphersArray<Cipher>

Returns a list of duplicates of the current decoding ciphers.

Returns:

  • (Array<Cipher>)

    an array of decoding cipher instances.



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.

Parameters:

  • input (Integer, String, *.to_s)

    any object that responds to '.to_s'

  • encoder (:utf8, :legacy) (defaults to: :utf8)

    the encoder to use.

Returns:

  • (String)

    if successful

  • (nil)

    if unsuccessful



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.

Parameters:

  • input (Integer, String, *.to_s)

    any object that responds to '.to_s'

  • encoder (:utf8, :legacy) (defaults to: :utf8)

    the encoder to use.

Returns:

  • (String)

    the encoded token

Raises:

  • (ArgumentError, RuntimeError)

    if the encoding fails or if the seed is not set.



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_cipherCipher

Returns a duplicate of the current encoding cipher.

Returns:

  • (Cipher)

    the encoding cipher instance.



40
41
42
# File 'lib/encoded_token/encoder.rb', line 40

def encoding_cipher
  @encoding_cipher.dup
end

.max_input_sizeInteger

Returns the maximum input size from configuration.

Returns:

  • (Integer)

    the maximum size allowed for input.



71
72
73
# File 'lib/encoded_token/encoder.rb', line 71

def max_input_size
  EncodedToken.configuration.max_input_size
end

.min_token_sizeInteger

Returns the minimum token size from configuration.

Returns:

  • (Integer)

    the minimum size for an encoded token.



61
62
63
# File 'lib/encoded_token/encoder.rb', line 61

def min_token_size
  EncodedToken.configuration.min_token_size
end