-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortener.rb
41 lines (32 loc) · 696 Bytes
/
shortener.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$LOAD_PATH << File.join(Dir.getwd, 'lib')
require 'rubygems'
require 'sinatra'
require 'shorten'
before do
@shorten = Shortener.load
end
helpers do
def base_url
base = "http:https://#{Sinatra::Application.host}"
port = Sinatra::Application.port == 80 ? base : base <<
":#{Sinatra::Application.port}"
end
def url(path='')
[base_url, path].join('/')
end
end
get '/' do
erb :home
end
get '/:hash' do
url = @shorten.lookup(params[:hash])
redirect url.url
end
post '/shorten' do
url = @shorten.shorten(params['url'])
redirect "/info/#{url.hash}"
end
get '/info/:hash' do
url = @shorten.lookup(params[:hash])
erb :shortened, :locals => {:url => url}
end