Created
August 8, 2017 18:44
-
-
Save snapcase/b7782380a49b3cb89c64c3a6066f5ace to your computer and use it in GitHub Desktop.
rubycat
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
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