Skip to content

@robomc /short.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
short
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
Something went wrong with that request. Please try again.