-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinatra.rb
322 lines (292 loc) · 7.81 KB
/
sinatra.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require 'sinatra'
require 'securerandom'
require 'yaml'
require 'uri'
require 'rqrcode'
require 'time'
class URLShortener
def initialize
@url_data = load_urls || {}
end
def shorten_url(long_url, custom_code = nil)
short_code = custom_code || SecureRandom.alphanumeric(6)
if @url_data.key?(short_code)
raise "Short code '#{short_code}' is already taken. Please choose another one."
end
encoded_url = URI.encode_www_form_component(long_url)
@url_data[short_code] = {
'long_url' => encoded_url,
'created_at' => Time.now,
'last_access' => nil,
'click_count' => 0
}
save_urls
short_code
end
def retrieve_url(short_code)
data = @url_data[short_code]
if data
data['last_accessed'] = Time.now
data['click_count'] += 1
save_urls
URI.decode_www_form_component(data['long_url'])
else
nil
end
end
def get_stats(short_code)
@url_data[short_code]
end
def generate_url_code(url)
qrcode = RQRCode::QRCode.new(url)
qrcode.as_svg(module_size: 6)
end
private
def load_urls
YAML.load_file('urls.yml') if File.exist?('urls.yml')
end
def save_urls
File.open('urls.yml', 'w') { |file| file.write(@url_data.to_yaml) }
end
end
shortener = URLShortener.new
# Home route with HTML form
# Route for the home page that shows the form and the list of shortened URLs
get '/' do
urls = shortener.get_all_urls
<<-HTML
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
}
.container {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
max-width: 600px;
width: 100%;
text-align: center;
}
h1 {
color: #333;
font-size: 36px;
margin-bottom: 20px;
}
form {
margin-bottom: 20px;
}
input[type="text"] {
padding: 10px;
width: 80%;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin: 10px 0;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>URL Shortener</h1>
<form action="/shorten" method="POST">
<label for="url">Enter URL to shorten:</label><br>
<input type="text" id="url" name="url" placeholder="Enter a URL" required><br>
<label for="custom_code">Custom Short Code (Optional):</label><br>
<input type="text" id="custom_code" name="custom_code" placeholder="Enter a custom short code"><br>
<button type="submit">Shorten URL</button>
</form>
<h2>Existing Short URLs:</h2>
<ul>
#{urls.map { |code, data| "<li><a href='/#{code}'>http://localhost:4567/#{code}</a> | <a href='/stats/#{code}'>View Stats</a></li>" }.join}
</ul>
</div>
</body>
</html>
HTML
end
# Method to get all URLs
class URLShortener
def get_all_urls
@url_data
end
end
# Route to shorten a URL
post '/shorten' do
long_url = params[:url]
custom_code = params[:custom_code]
begin
short_code = shortener.shorten_url(long_url, custom_code)
content_type 'text/html'
status 200
<<-HTML
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 600px;
width: 100%;
}
h1 {
color: #28a745;
font-size: 36px;
margin-bottom: 20px;
}
p {
font-size: 18px;
color: #555;
margin: 10px 0;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>URL Shortened!</h1>
<p>Short URL: <a href='http://localhost:4567/#{short_code}'>http://localhost:4567/#{short_code}</a></p>
<p><a href='/qr/#{short_code}'>Get QR Code</a></p>
<p><a href='/stats/#{short_code}'>View Statistics</a></p>
<a href='/'>Shorten another URL</a>
</div>
</body>
</html>
HTML
rescue => e
status 400
"<p>Error: #{e.message}</p><a href='/'>Go back</a>"
end
end
# Route to generate QR code for a short URL
get '/qr/:short_code' do
short_code = params[:short_code]
short_url = "http://localhost:4567/#{short_code}"
content_type 'image/svg+xml'
shortener.generate_url_code(short_url)
end
# Route to handle redirection from short URL
get '/:short_code' do
short_code = params[:short_code]
long_url = shortener.retrieve_url(short_code)
if long_url
encoded_redirect_url = URI.encode(long_url)
redirect encoded_redirect_url
else
"Short URL not found!"
end
end
# Route to display statistics for a short URL
get '/stats/:short_code' do
short_code = params[:short_code]
stats = shortener.get_stats(short_code)
if stats
content_type 'text/html'
<<-HTML
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 600px;
width: 100%;
}
h3 {
color: #333;
font-size: 30px;
margin-bottom: 20px;
}
p {
font-size: 18px;
color: #555;
margin: 10px 0;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h3>Statistics for Short URL: #{short_code}</h3>
<p>Original URL: <a href='#{URI.decode_www_form_component(stats['long_url'])}' target="_blank">#{URI.decode_www_form_component(stats['long_url'])}</a></p>
<p>Created at: #{stats['created_at']}</p>
<p>Last accessed: #{stats['last_accessed'] || 'Never'}</p>
<p>Click count: #{stats['click_count']}</p>
<a href='/'>Go back</a>
</div>
</body>
</html>
HTML
else
"No statistics available for this short URL."
end
end