Browse Source

initial commit

master
jimi 12 years ago
commit
c932349675
  1. 34
      app.rb
  2. 3
      config.ru
  3. 17
      views/index.haml
  4. 57
      views/styles.scss

34
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

3
config.ru

@ -0,0 +1,3 @@
require './app'
run Sinatra::Application

17
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}"

57
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; } }