Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement