class Object

Public Class Methods

collection(option) click to toggle source

determine the database collection to use

# File lib/session_manager/handler.rb, line 65
def self.collection(option)
    user = option.fetch(:user) || false
    app  = option.fetch(:app)  || false
    collection = user ? USERS_SESSIONS : APPS_SESSIONS
    return collection
end
generate_token() click to toggle source

generate token

# File lib/session_manager/handler.rb, line 60
def self.generate_token
    get_uuid.generate
end
get_uuid() click to toggle source
# File lib/session_manager/handler.rb, line 54
def self.get_uuid
    uuid = UUID.new
    uuid
end
parse_json(param) click to toggle source

parse json

# File lib/session_manager/handler.rb, line 73
def self.parse_json(param)
  JSON.parse(param)
end
return_response(code,msg) click to toggle source

change the return parameter in hash format

# File lib/session_manager/handler.rb, line 167
def self.return_response(code,msg)
  {code: code,message: msg}
end
session_generator(option, param, token) click to toggle source

create a session hash for the database

# File lib/session_manager/handler.rb, line 78
def self.session_generator(option, param, token)
  option[:user] ? { login: param['login'],token: token} : { api_key: param['api_key'],token: token} 
end
valid_keys?(param,option) click to toggle source

check if the passed keys are those which are waited

# File lib/session_manager/handler.rb, line 83
def self.valid_keys?(param,option)
  option[:user] ? waited_keys = ["login","token"] : waited_keys = ["api_key","token"] 
  waited_keys.each do |k|
     if !(param.has_key?(k))
       return false
       break
     end
   end
   return true
end
valid_token?(token,option) click to toggle source

check if the passed token is valid

# File lib/session_manager/handler.rb, line 95
def self.valid_token?(token,option)
  option[:user] ? attr = "login" : attr = "api_key"
  begin
    object = parse_json(token)
    hash   = object.to_hash
    if !(hash.has_key?(attr)) || !(hash[attr].is_a?(String))
      return false
    end
  rescue
    return false
  end
return true
end
validate_argument(param) click to toggle source

check if the hash values are not nil neither empty

# File lib/session_manager/handler.rb, line 122
def self.validate_argument(param)
  param.values.each do |v|
   if v.nil? || v.empty?
     return false
     break
   end
  end
  return true
end
validate_json(param,option) click to toggle source

check if the json object is valid

# File lib/session_manager/handler.rb, line 133
def self.validate_json(param,option)
  begin
   object  = parse_json(param) 
   hash    = object.to_hash
   return valid_keys?(hash,option) && validate_argument(hash) && validate_values(hash)
  rescue
    return false
  end
end
validate_option(option) click to toggle source

check if the option is valid

# File lib/session_manager/handler.rb, line 144
 def self.validate_option(option)
  if !(option.nil?) && option.is_a?(Hash) && !(option.empty?) && option.has_key?(:user) && option.has_key?(:app)
    if option[:user] && option[:app]
      return false
    else 
      if !option[:user] && !option[:app]
       return  false
      else
        option.values.each do |v|
          unless (v.is_a?(TrueClass) || v.is_a?(FalseClass))
            return false
            break
          end
        end
      end
    end
  else
    return false
  end
  return true
end
validate_values(hash) click to toggle source

check if hash's values are String

# File lib/session_manager/handler.rb, line 110
def self.validate_values(hash)
  values = hash.values
  values.each do |v|
    if !(v.is_a?(String))
      return false
      break
    end
  end
  return true
end