Created
November 1, 2013 20:21
-
-
Save davidbalbert/7271383 to your computer and use it in GitHub Desktop.
A silly little URL shortener.
This file contains hidden or 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
source "https://rubygems.org" | |
gem "sinatra" |
This file contains hidden or 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
GEM | |
remote: https://rubygems.org/ | |
specs: | |
rack (1.5.2) | |
rack-protection (1.5.1) | |
rack | |
sinatra (1.4.4) | |
rack (~> 1.4) | |
rack-protection (~> 1.4) | |
tilt (~> 1.3, >= 1.3.4) | |
tilt (1.4.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
sinatra |
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'bundler/setup' | |
require 'sinatra' | |
require 'securerandom' | |
URLS = {} | |
get '/:slug' do | |
redirect URLS[params[:slug]] | |
end | |
post '/' do | |
begin | |
slug = SecureRandom.hex(4) | |
end while URLS.has_key?(slug) | |
URLS[slug] = params[:url] | |
"#{slug}\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment