Skip to content

Instantly share code, notes, and snippets.

@snapcase
Created August 8, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snapcase/b7782380a49b3cb89c64c3a6066f5ace to your computer and use it in GitHub Desktop.
Save snapcase/b7782380a49b3cb89c64c3a6066f5ace to your computer and use it in GitHub Desktop.
rubycat
class RubyCat < Sinatra::Base
@@db_conns = []
#
# current implementation
#
# creates a new connection to mysql
get '/create' do
@@db_conns << Mysql2::Client.new(host: '127.0.0.1', username: 'root', password: 'derp')
'established a new database connection'
end
# current connection count
get '/count' do
count = @@db_conns.size
"there is a total of #{count} connections"
end
#
# enhancements
#
# closes all the connections and wipes objects from array
get '/nuke' do
@@db_conns.map!(&:close).compact!
'closed all connections'
end
# retrieve a random, already established connection
get '/random' do
return 'no connections' if @@db_conns.empty?
conn = @@db_conns.sample
"retrieved db connection with object id: #{conn.object_id}"
end
# do you feel lucky, punk?
get '/trisslott' do
return 'no connections' if @@db_conns.empty?
if rand(100) < 50
conn = @@db_conns.sample
"retrieved db connection with object id: #{conn.object_id}"
else
'better luck next time'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment