Skip to content

Instantly share code, notes, and snippets.

@snapcase snapcase/rubycat.rb
Created Aug 8, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.