-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
60 lines (50 loc) · 1.55 KB
/
main.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
require 'sinatra'
require 'securerandom'
require 'yaml'
require 'uri'
class URLShortener
def initialize
@url_data = load_urls || {}
end
def shorten_url(long_url)
encoded_url = URI.encode(long_url) # Encode the long URL here
short_code = SecureRandom.alphanumeric(6)
@url_data[short_code] = encoded_url # Store the encoded long URL
save_urls
puts "Shortened URL saved: #{short_code} => #{encoded_url}" # Debug
short_code
end
def retrieve_url(short_code)
encoded_url = @url_data[short_code]
long_url = URI.decode(encoded_url) # Decode the long URL when retrieving
puts "Retrieving URL for short code #{short_code}: #{long_url}" # Debug
long_url
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
# Route to shorten a URL
post '/shorten' do
long_url = params[:url]
short_code = URI.encode(shortener.shorten_url(long_url)) # Encode the short code
puts "Returning Short URL: http://localhost:4567/#{short_code}" # Debug
"Short URL: http://localhost:4567/#{short_code}"
end
# Route to handle redirection from short URL
get '/:short_code' do
short_code = URI.decode(params[:short_code]) # Decode the short code
puts "Requested short code: #{short_code}" # Debug
long_url = shortener.retrieve_url(short_code)
if long_url
puts "Redirecting to #{long_url}" # Debug
redirect long_url
else
"Short URL not found!"
end
end