Skip to content

Instantly share code, notes, and snippets.

@evanscottgray
Created February 21, 2015 01:16
Show Gist options
  • Select an option

  • Save evanscottgray/47f4b88406cf71631d03 to your computer and use it in GitHub Desktop.

Select an option

Save evanscottgray/47f4b88406cf71631d03 to your computer and use it in GitHub Desktop.
link short
require 'sinatra'
require 'redis'
require 'json'
require 'securerandom'
get '/l/:token' do
redis = Redis.new
token_contents = redis.get(params[:token])
if token_contents
redirect token_contents
else
redirect 'https://cuetutor.com'
end
end
post '/l' do
content_type :json
req_body = request.body.read
if JSON.parse(req_body)
json = JSON.parse(req_body)
destination = json.fetch('url', 'https://cuetutor.com')
end
redis = Redis.new
token = SecureRandom.hex(3)
token_link = "l/%s" % token
redis.set(token.to_s, destination.to_s)
resp = { short_link: token_link, long_link: destination }
resp.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment