Created
February 21, 2015 01:16
-
-
Save evanscottgray/47f4b88406cf71631d03 to your computer and use it in GitHub Desktop.
link short
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
| 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