Created
July 12, 2012 11:06
-
-
Save rob-mcgrail/3097470 to your computer and use it in GitHub Desktop.
short
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
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' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment