Created
December 6, 2010 01:47
-
-
Save dydx/729703 to your computer and use it in GitHub Desktop.
my Shortener.rb webapp in one file
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
| # shortener.rb - Josh Sandlin - 12/5/2010 - 6:19PM | |
| # Simple url shortener with Sinatra and OpenKeyVal.org | |
| # "THE BEER-WARE LICENSE" (Revision 43): | |
| # <[email protected]> wrote this file. As long as you retain this | |
| # notice you can do whatever you want with this stuff. If we meet some day, | |
| # and you think this stuff is worth it, you can buy me a beer in return. | |
| # ~Josh Sandlin | |
| require 'rubygems' | |
| require 'sinatra' | |
| require 'flyingv' | |
| require 'digest/md5' | |
| # Some basic configurations. Edit as needed | |
| HOST = "localhost" | |
| PORT = 4567 | |
| # some basic functions for setting and getting key-val pairs | |
| def gen_key( string ) | |
| md5 = Digest::MD5.hexdigest( string ) | |
| return md5[0, 3] + md5[29, 31] | |
| end | |
| def send_key_val( key, val ) | |
| FlyingV.post( key, val ) | |
| end | |
| def get_by_key( key ) | |
| FlyingV.get( key ) | |
| end | |
| # render the frontend all nice and purdy | |
| get '/' do | |
| erb :index | |
| end | |
| post '/' do | |
| url = params[:url] | |
| key = gen_key( url ) | |
| send_key_val( key, url ) | |
| @short_url = "http://#{HOST}:#{PORT}/#{key}" | |
| erb :submitted | |
| end | |
| get '/:url' do |hash_key| | |
| original_url = get_by_key( hash_key ) | |
| redirect original_url | |
| end | |
| __END__ | |
| @@ layout | |
| <!doctype html> | |
| <html> | |
| <head> | |
| <style type="text/css"> | |
| body { | |
| background: #191919; | |
| color: #D1CB9A; | |
| font-family: arial, helvetica, sans-serif; | |
| font-size: small; | |
| letter-spacing: 1px; | |
| line-height: 1.6em; | |
| margin: 0; | |
| padding: 1em; | |
| text-align: center; | |
| } | |
| #wrap { | |
| margin: 0 auto; | |
| padding: 1em; | |
| text-align: left; | |
| width: 20em; | |
| } | |
| h1 { | |
| color: #ABA071; | |
| font-size: 280%; | |
| font-weight: normal; | |
| margin: 0; | |
| padding: 2em 0; | |
| text-align: center; | |
| } | |
| form { | |
| padding: 1em; | |
| } | |
| p { | |
| margin: 0; | |
| padding: 1em; | |
| text-align: center; | |
| } | |
| p.short { | |
| color: #FF9900; | |
| } | |
| section { | |
| border: 1px solid; | |
| } | |
| footer { | |
| margin: 0; | |
| padding: 4em 0; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="wrap"> | |
| <header> | |
| <h1>Shortener.rb</h1> | |
| </header> | |
| <section> | |
| <%= yield %> | |
| </section> | |
| <footer> | |
| <p>© 2010 - Josh Sandlin</p> | |
| </footer> | |
| </div> | |
| </body> | |
| <html> | |
| @@ index | |
| <form method="post"> | |
| <input type="text" name="url" /> | |
| <input type="submit" value="Shorten"> | |
| </form> | |
| @@ submitted | |
| <p>Here is your shortened url:</p> | |
| <p class="short"><%= @short_url %></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment