Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created November 1, 2013 20:21
Show Gist options
  • Save davidbalbert/7271383 to your computer and use it in GitHub Desktop.
Save davidbalbert/7271383 to your computer and use it in GitHub Desktop.
A silly little URL shortener.
source "https://rubygems.org"
gem "sinatra"
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
#!/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