added flash plugin
+ added flash plugin, and the session plugin required by flash + set the DB name as an environment variable + set @message variable where it needs to be set to show on the UIpull/11/head
parent
fff6b0f5dd
commit
b2b41f1aa1
16
app.rb
16
app.rb
|
|
@ -4,11 +4,16 @@ require 'json'
|
|||
require 'sequel'
|
||||
|
||||
class App < Roda
|
||||
DB = Sequel.sqlite('db/url_shortener.db')
|
||||
links = DB[:links]
|
||||
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
|
||||
plugin :render, escape: true
|
||||
plugin :flash
|
||||
|
||||
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
|
||||
links = DB[:links]
|
||||
|
||||
route do |r|
|
||||
r.root do
|
||||
@message = flash['message'] || "Enter a URL"
|
||||
view :home
|
||||
end
|
||||
|
||||
|
|
@ -19,11 +24,16 @@ class App < Roda
|
|||
|
||||
r.post "create" do
|
||||
url = r.params['url']
|
||||
if url.empty? then r.redirect '/' end
|
||||
if url.empty?
|
||||
flash['message'] = "Please enter a valid URL";
|
||||
r.redirect '/'
|
||||
end
|
||||
if nil == links.filter(:url => url).first
|
||||
code = SecureRandom.urlsafe_base64 4
|
||||
links.insert(url: url, code: code)
|
||||
@message = "Link created"
|
||||
end
|
||||
@message ||= "Link exists"
|
||||
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code]
|
||||
view :create
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue