Class: Chat
- Inherits:
-
Object
- Object
- Chat
- Defined in:
- lib/chatC.rb
Class Method Summary collapse
- .checkLogin(connection) ⇒ Object
- .get_connection(host, username, password, port, database) ⇒ Object
- .get_lastmsgs(connection, table, n) ⇒ Object
- .help ⇒ Object
- .input ⇒ Object
- .push_msg(connection, user, msg) ⇒ Object
- .refresh(lastmsgs) ⇒ Object
- .registration(connection) ⇒ Object
Class Method Details
.checkLogin(connection) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/chatC.rb', line 15 def self.checkLogin ( connection ) print "Login: " user = self.input print "Password: " passwd = self.input md5passwd = Digest::MD5.new md5passwd << passwd connection.query("SELECT * FROM users WHERE username = '#{user}'").each do |data_for_passwd| if (md5passwd == data_for_passwd["password"]) return user else puts "Login failed" return "" end end end |
.get_connection(host, username, password, port, database) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/chatC.rb', line 6 def self.get_connection (host,username,password,port,database) return Mysql2::Client.new( :host => host, :username => username , :password => password , :port => port , :database => database ) end |
.get_lastmsgs(connection, table, n) ⇒ Object
69 70 71 |
# File 'lib/chatC.rb', line 69 def self.get_lastmsgs ( connection, table, n) connection.query("SELECT * FROM #{table} WHERE id > ( SELECT MAX(id) - #{n} FROM chat)") end |
.help ⇒ Object
75 76 77 |
# File 'lib/chatC.rb', line 75 def self.help puts "Help information for work with chat_by_rznvls /n -r = refresh chat information, shows last 5 messages" end |
.input ⇒ Object
86 87 88 |
# File 'lib/chatC.rb', line 86 def self.input gets.to_s.strip end |
.push_msg(connection, user, msg) ⇒ Object
65 66 67 |
# File 'lib/chatC.rb', line 65 def self.push_msg(connection, user,msg) connection.query("INSERT INTO chat (user, msg) VALUES ('#{user}', '#{msg}')") end |
.refresh(lastmsgs) ⇒ Object
79 80 81 82 83 |
# File 'lib/chatC.rb', line 79 def self.refresh (lastmsgs) lastmsgs.each do |row| puts row["user"] + ": " + row["msg"] end end |
.registration(connection) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chatC.rb', line 39 def self.registration(connection) print "Login: " user = Chat.input print "Password: " passwd = Chat.input md5passwd = Digest::MD5.new md5passwd << passwd connection.query("SELECT * FROM users WHERE username = '#{user}'").each do |data_for_reg| if (user == data_for_reg["username"]) puts "Try another login" puts user return "" end end puts "Login succes" puts user connection.query("INSERT INTO users (username, password) VALUES ('#{user}', '#{md5passwd}')") return user end |