commit c932349675bbf4cfe3ffe10703b0b0f31d4e0d6a Author: jimi Date: Sat Feb 16 22:50:02 2013 -0600 initial commit diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..fa50e23 --- /dev/null +++ b/app.rb @@ -0,0 +1,34 @@ +require 'sinatra' +require 'redis' +require 'haml' +require 'sass' + +redis = Redis.new + +helpers do + include Rack::Utils + alias_method :h, :escape_html + + def random_string(length) + rand(36**length).to_s(36) + end +end + +get ('/styles/main.css'){ scss :styles } + +get '/' do + haml :index +end + +post '/' do + if params[:url] and not params[:url].empty? + @shortcode = random_string 5 + redis.setnx "links:#{@shortcode}", params[:url] + end + haml :index +end + +get '/:shortcode' do + @url = redis.get "links:#{params[:shortcode]}" + redirect @url || '/' +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..e6269a0 --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +require './app' + +run Sinatra::Application diff --git a/views/index.haml b/views/index.haml new file mode 100644 index 0000000..055967f --- /dev/null +++ b/views/index.haml @@ -0,0 +1,17 @@ +!!!5 +%html + %head + %title Shortlink App + %link{ rel:"stylesheet", href:"/styles/main.css" } + %body + .container + %h1 shortlink app + %form{ method:"post" } + %input{ type:"text", value:"#{params[:url]}", name:"url", id:"url" } + %input{ type:"submit", value:"shorten", id:"submit" } + .clear + -if @shortcode + .result + Your shortened URL is: + %a{ href:"http://jinfield.com/#{@shortcode}" } + href:"http://jinfield.com/#{@shortcode}" diff --git a/views/styles.scss b/views/styles.scss new file mode 100644 index 0000000..492ea7a --- /dev/null +++ b/views/styles.scss @@ -0,0 +1,57 @@ +body { + font-family: "Gill Sans", "Gill Sans MT", Sans-Serif; } + +.container { + width: 400px; + margin: 120px auto 0px auto; } + +h1 { + width: 400px; + margin-bottom: 12px; + text-align: center; + color: #ff4b33; + font-size: 40px; + padding-bottom: 8px; + border-bottom: 2px solid #ff4b33; } + +form { + display: block; + width: 400px; } + +input { + display: block; + float: left; + padding: 8px; + font-size: 16px; } + +#url { + width: 280px; + margin-right: 12px; } + +#submit { + width: 88px; + border: none; + background: #ff4b33; + padding: 10px; + &:hover { + background: #ff7866; } } + +.clear { + height: 1px; + width: 400px; + clear: both; } + +.result { + clear: both; + width: 400px; + margin-top: 12px; + border-top: 2px solid #ff4b33; + padding-top: 12px; + text-align: center; + a { + font-size: 24px; + display: block; + margin-top: 8px; + color: #ff2d11; + background: #ffd2cc; + padding: 8px; } }