Created
November 26, 2012 05:31
-
-
Save innovationhero/4146730 to your computer and use it in GitHub Desktop.
1.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mohamed Fouad - Everything is an idea! | |
#TODO somthing weird about beign rescue ensure, as it passess faulty code in begin and rescue sections, must dig deeper there... | |
#TODO Automatic generation of UserID to be supplied to redis | |
require 'redis' | |
require "digest/sha2" | |
module DB | |
#include DateTime | |
$r = Redis.new | |
def DB.signup(details) #pseudo-keyword argument | |
begin | |
#make sure somthing is ready | |
#get new userID and make it a global variable | |
# circle:jdoe:family or users:applicationID:userID | |
$userID = 0 | |
username(details) | |
password(details) | |
email(details) | |
resource(details) | |
timestamp | |
userID(details) | |
rescue | |
# rescue some errors by custom error msg | |
ensure | |
userID(details) | |
puts "Storing signup details.... " | |
puts $r.hgetall(['users:userID']) | |
p "exists" | |
# puts $r.exists(['users:userID']) #works | |
if !$r.exists(['users:userID']) then | |
p 'Username avialable' | |
else | |
p 'Sorry! Username unavialable' | |
end | |
if $r.exists(['users:userID'])? do | |
p 'Username avialable' | |
else | |
p 'Sorry! Username unavialable' | |
end | |
=begin | |
p "#############################And here comes the keys############################" | |
for s in $r.keys("*") do | |
p s | |
if not s == 'users:userID' then | |
p 'username avialable ' | |
else | |
p 'Sorry! Username unavialable' | |
puts "now.." + $r.hget([s], ["username"]) | |
end | |
# p $r.hget([s], ["userame"]) | |
#puts $r.hgetall(s) | |
end | |
=end | |
=begin | |
p $r.keys("*").each | |
$r.keys("*").each { |d| | |
#puts $r.keys(d) | |
#puts $r.hgetall(["#{$r.keys(d)}"]) | |
#puts $r.hgetall([$r.keys(d)]) | |
# yield( d ) | |
#$r.keys(d) | |
#puts $r.hgetall([$r.keys("#{d}")]) | |
puts $r.hgetall([$r.keys("*").each]) | |
puts "---------------------------------------"} | |
=end | |
#p $r.keys("*") | |
# "Wherver you may go, men will seek you out, and compel you to belong to there desperate company of odd fellows" - Henery David Thoreau | |
#puts $r.all | |
#c = DateTime.now | |
#p self.now | |
end | |
end | |
# Helping Methods | |
#TODO can be changed to attributes | |
#Generate a userID for each new user who signup | |
def DB.userID(details) | |
=begin | |
$r.hset(['users:$userID'], ["id"], 3 ) | |
puts $r.hgetall(['users:$userID}']) | |
=end | |
id = $r.incr('users:usersdID') | |
p "current users:userID... " + id.to_s | |
#works | |
#user_id = $r.incr("user:uid") | |
#p user_id | |
end | |
def DB.username(details) | |
if details[:username] then | |
$r.hset(['users:userID'], ["username"], details[:username]) | |
#$r.hget(['users:id:#{id}], | |
else | |
puts "No username to store, operation aported " | |
end | |
end | |
def DB.password(details) | |
salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp | |
password_salt, password_hash = salt, Digest::SHA256.hexdigest(details[:password] + salt) | |
$r.hset(['users:userID'], ["password_salt"], password_salt) | |
$r.hset(['users:userID'], ["password_hash"], password_hash) | |
end | |
def DB.status | |
# either return online or offline status | |
$r.hget(['users:userID'], ["status"]) | |
end | |
def DB.email(details) | |
if details[:email] then | |
$r.hset(['users:userID'], ["email"], details[:email]) | |
else | |
puts "No email provided" | |
end | |
end | |
def DB.resource(details) | |
if details[:resource] then | |
$r.hset(['users:userID'], ["resource"], details[:resource]) | |
else | |
puts "fix resource not found " | |
end | |
end | |
def DB.timestamp | |
$r.hset(['users:userID'], ["Since"], Time.now.to_s) | |
end | |
# End of Helping Methods | |
#TODO must check both username and password together, rightnow username is missing | |
def DB.authenticate(details) | |
p "#############################################################" | |
#user = find_by_username(username) | |
hash = Digest::SHA256.hexdigest(details[:password]+ $r.hget(['users:userID'], [:password_salt])) | |
if hash == $r.hget(['users:userID'], [:password_hash]) | |
# No of visits | |
$r.hincrby(['users:userID'],[:visits], 1) | |
p "User Authenticated" | |
p "User can login" | |
else | |
p "Username or password invalid" | |
p "User can't login" | |
end | |
end | |
def login | |
# set status online | |
end | |
def logout | |
# set status offline | |
end | |
end | |
DB.signup(username: "sueruser5", password: "superpass5", email: "[email protected]", resource: "4http:\\url.com") | |
DB.authenticate(username: "superuser5", password: "superpass5") | |
=begin | |
def DB.password(details) | |
#Add salt hashing and high security encryption of the passwords | |
if details[:password] then | |
$r.hset(['users:userID'], ["password"], details[:password]) | |
else | |
puts "No password provided" | |
end | |
end | |
=end | |
#DB.init(username: "superuser2", password: "superpass2", email: "[email protected]", url:"2http:\\url.com") | |
=begin | |
p $r.hkeys(['users:userID']) #works | |
# p $r.hdel(['users:userID'], ["url"] ) #works | |
p $r.hvals(['users:userID']) #works | |
=end | |
=begin | |
def DB.init(e) | |
DB.signup(username: e[:username], password: e[:password], email: e[:email], resource: e[:resource]) | |
#p e[:email] + "this is email frm init" | |
#DB.signup(e[:username], e[:password], e[:email]) | |
#DB.resource(e[:url]) | |
puts $r.hgetall(['users:userID']) | |
end | |
=end | |
=begin | |
ensure | |
puts "Storing Username.... " | |
puts "Printing detail.... " + details[:username] | |
p("Signup attempted on username ") | |
puts $r.hget(['users:userID'], [:username]) | |
p("print all this user details ..... ") | |
puts $r.hgetall(['users:userID']) | |
puts $r.hget(['users:userID'], [:email]) | |
end | |
=end | |
#DB.signup(username: "312", password: "3", email: "3") | |
#DB.resource(url:"http:eko.me") | |
=begin | |
def DB.bulk_signup(details) #pseudo-keyword argument | |
begin | |
$r.hset(['users:userID'], ["username"], ["mohamed"]) | |
$r.hset(['users:userID'], ["password"], ["mohamedpass"]) | |
puts $r.hget(['users:userID'], ["username"]) | |
puts $r.hgetall(['users:userID']) | |
puts options[:username] | |
rescue | |
if details[:username] then | |
[details[:username]].each do |detail| | |
#[details[:username], details[:password], details[:email]].each do |detail| | |
$r.hset(['users:userID'], ["username"], details[:username]) | |
puts "Storing Username.... " | |
#puts $r.hget(['users:userID'], ["username"]) | |
puts $r.hget(['users:userID'], details[:username]) | |
puts $r.hgetall(['users:userID']) | |
puts "Printing detail.... " | |
puts detail | |
end | |
else | |
puts "No username to store, operation aported " | |
end | |
ensure | |
p("Signup attempted...") | |
end | |
end | |
end | |
=end | |
#DB.signup(username: "x", password:"y", email:"z") | |
#run Rack::Torii::Server::Core::signup | |
#$r.hset("users", "username1", "password1") | |
#$r.smembers('db:accessdetails') | |
#$r.set(['users:toby']) | |
=begin | |
[@o.username, @o.password, @o.email].each do |detail| | |
puts "Printing detail.... " | |
puts detail | |
end | |
=end | |
#$redis.hset("filenames", doc_id, filename) | |
=begin | |
>>> import redis | |
>>> redis = redis.Redis(host='localhost', port=6379, db=0) | |
>>> redis.smembers('circle:jdoe:soccer') | |
set(['users:toby', 'users:adam', 'users:apollo', 'users:mike']) | |
>>> redis.sadd('circle:jdoe:soccer', 'users:fred') | |
True | |
>>> redis.smembers('circle:jdoe:soccer') | |
set(['users:toby', 'users:adam', 'users:apollo', 'users:mike', 'users:fred']) | |
=end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment