public
Last active

short

  • Download Gist
short.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
require 'sinatra'
require 'redis'
 
$redis = Redis.new
 
get '/s' do
'Enter a URL: <form method="post" action="s"><input type="text" name="url"><input type="submit" value="Shorten"></form>'
end
 
post '/s' do
k = $redis.incr('short:keyspace').to_s
$redis.set 'short:' + k, params[:url]
"#{params[:url]} shortened to <a href='#{request.url.chomp('s') + k}'>#{request.url.chomp('s') + k}</a>"
end
 
get '/:key/?' do |k|
url = $redis.get 'short:' + k
url ? redirect(url) : 404
end
 
not_found { redirect '/s' }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.