-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortner.rb
42 lines (31 loc) · 855 Bytes
/
shortner.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
require 'rubygems'
require 'sinatra'
require 'mongoid'
require './models/url'
require 'alphadecimal'
class Shortner < Sinatra::Base
# setting up the mongodb db name and connection
#Mongoid.load!("./config/mongoid.yml")
#replaced Yaml file with the configuration below becuase of mongoid's validate_db_name
# The error is caused by changes in Rubygems parsing YAML files with 'syck' see the link below:
# https://github.com/mongoid/mongoid/issues/648
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("shortner")
end
get '/' do
erb :index
end
post '/' do
@short_url = Url.find_or_create_by_url(params[:url])
if @short_url.valid?
erb :success
else
erb :index
end
end
#The redirect route block
get '/:shortened' do
short_url = Url.find_by_shortened(params[:shortened])
redirect short_url.url
end
end