-
Notifications
You must be signed in to change notification settings - Fork 7
/
url_shorten.rb
61 lines (51 loc) · 1.2 KB
/
url_shorten.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'sinatra'
require 'sqlite3'
class ShortenedURL
def initialize(destination_url)
@destination_url = destination_url
@shortened_url = generate_url
@db = SQLite3::Database.new('url.db')
end
def save
@db.execute("insert into urls values (?, ?)", @destination_url, @shortened_url)
end
def shortened
@shortened_url
end
def destination
@destination_url
end
#list of urls generated so far
#find by shortened url
def self.find_by_shortened_url(short_url)
#fill me in
#and what the hell is self
#I should return an instance of ShortenedURL
end
private
def generate_url
new_url = []
6.times do
new_url << ('A'..'Z').to_a.sample
end
new_url.join
end
end
get '/new' do
"<html>
<body>
<form action='/new' method='POST'>
<input type='url' name='url' placeholder='Enter URL here'>
<input type='submit' value='GO'>
</form>
</body>
</html>"
end
post '/new' do
new_url = ShortenedURL.new(params[:url])
new_url.save
"Your new URL is #{new_url.shortened} which redirects to #{new_url.destination}"
end
get '/:short_url' do |url|
redirect ShortenedURL.find_by_shortened_url(url).destination
end