Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class RubyCat < Sinatra::Base
  2. @@db_conns = []
  3.  
  4. #
  5. # current implementation
  6. #
  7.  
  8. # creates a new connection to mysql
  9. get '/create' do
  10. @@db_conns << Mysql2::Client.new(host: '127.0.0.1', username: 'root', password: 'derp')
  11. 'established a new database connection'
  12. end
  13.  
  14. # current connection count
  15. get '/count' do
  16. count = @@db_conns.size
  17. "there is a total of #{count} connections"
  18. end
  19.  
  20. #
  21. # enhancements
  22. #
  23.  
  24. # closes all the connections and wipes objects from array
  25. get '/nuke' do
  26. @@db_conns.map!(&:close).compact!
  27. 'closed all connections'
  28. end
  29.  
  30. # retrieve a random, already established connection
  31. get '/random' do
  32. return 'no connections' if @@db_conns.empty?
  33. conn = @@db_conns.sample
  34. "retrieved db connection with object id: #{conn.object_id}"
  35. end
  36.  
  37. # do you feel lucky, punk?
  38. get '/trisslott' do
  39. return 'no connections' if @@db_conns.empty?
  40. if rand(100) < 50
  41. conn = @@db_conns.sample
  42. "retrieved db connection with object id: #{conn.object_id}"
  43. else
  44. 'better luck next time'
  45. end
  46. end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement