module Obfuscate::Obfuscatable::ClassMethods

Public Class Methods

clarify( text, mode = :string) click to toggle source

Clarify obfuscated text.

@param [String] text to clarify @param [Symbol] mode to clarify, defaults to :string

# File lib/obfuscate/obfuscatable.rb, line 87
def clarify( text, mode = :string)
  self.obfuscatable_crypt.clarify(text, mode)
end
clarify_id( text ) click to toggle source

Clarifies obfuscated Model id @return [String]

# File lib/obfuscate/obfuscatable.rb, line 75
def clarify_id( text )
  begin
    self.obfuscatable_crypt.clarify( text, :block )
  rescue ArgumentError
    # invalid text passed in, causing a Base64 decode error, ignore.
  end
end
find_by_obfuscated_id( text ) click to toggle source

Find by obfuscated_id

@return [Object]

# File lib/obfuscate/obfuscatable.rb, line 62
def find_by_obfuscated_id( text )
  find_by_id( clarify_id( text ) )
end
find_obfuscated( text ) click to toggle source

Find by obfuscated_id @raises ActiveRecord::RecordNotFound @return [Object]

# File lib/obfuscate/obfuscatable.rb, line 69
def find_obfuscated( text )
  find( clarify_id( text ) )
end
obfuscate( text, mode = :string) click to toggle source

@param [String] text to clarify @param [Symbol] mode to clarify, defaults to :string

# File lib/obfuscate/obfuscatable.rb, line 94
def obfuscate( text, mode = :string)
  self.obfuscatable_crypt.obfuscate(text, mode)
end

Public Instance Methods

obfuscatable(options = {}) click to toggle source

Make this Model Obfuscatable. Adds method obfuscate_id which obfuscates the id to 11 characters. Cavaet: Only supports id lengths up to 8 (e.g. 99,999,999) due to use of Blowfish block encryption.

@params [Hash] options to override the default config @option options [Symbol] :salt A Model specific salt, length must be between 1-56 @option options [Symbol] :append_salt Append string to default salt and use for this Model. Overwrites the salt option. @option options [Symbol] :encode Enable Base64 and URL encoding for this Model. Enabled by default. @option options [Symbol] :remove_trailing_equal When in :block mode, removes the trailing = from the

obfuscated text.
# File lib/obfuscate/obfuscatable.rb, line 33
def obfuscatable(options = {})
  # :append_salt will append the string to the default salt
  append_salt = options.with_indifferent_access.delete(:append_salt)
  if append_salt
    options[:salt] = "#{Obfuscate.config.salt}#{append_salt}"
  end
  
  
  config = Obfuscate.config.apply(options)

  cattr_accessor :obfuscatable_config
  self.obfuscatable_config = config

  cattr_accessor :obfuscatable_crypt
  self.obfuscatable_crypt = Obfuscate::Crypt.new( config )

  define_method :obfuscated_id do
    self.obfuscatable_crypt.obfuscate( self.id, :block )
  end

  define_method :clarify_id do |text|
    self.class.clarify_id( text )
  end

  class << self

    # Find by obfuscated_id
    #
    # @return [Object]
    def find_by_obfuscated_id( text )
      find_by_id( clarify_id( text ) )
    end

    # Find by obfuscated_id
    # @raises ActiveRecord::RecordNotFound
    # @return [Object]
    def find_obfuscated( text )
      find( clarify_id( text ) )
    end

    # Clarifies obfuscated Model id
    # @return [String]
    def clarify_id( text )
      begin
        self.obfuscatable_crypt.clarify( text, :block )
      rescue ArgumentError
        # invalid text passed in, causing a Base64 decode error, ignore.
      end
    end

    # Clarify obfuscated text.
    #
    # @param [String] text to clarify
    # @param [Symbol] mode to clarify, defaults to :string
    def clarify( text, mode = :string)
      self.obfuscatable_crypt.clarify(text, mode)
    end

    #
    # @param [String] text to clarify
    # @param [Symbol] mode to clarify, defaults to :string
    def obfuscate( text, mode = :string)
      self.obfuscatable_crypt.obfuscate(text, mode)
    end
  end
end