Compare commits
7 Commits
88b52df610
...
5ca4bf707b
| Author | SHA1 | Date |
|---|---|---|
|
|
5ca4bf707b | |
|
|
8b2780820c | |
|
|
8639c785b7 | |
|
|
84a047fb0c | |
|
|
b2b41f1aa1 | |
|
|
fff6b0f5dd | |
|
|
c51c0afcf2 |
|
|
@ -1,3 +1,4 @@
|
||||||
*.sw*
|
*.sw*
|
||||||
.bundle
|
.bundle
|
||||||
*.db
|
*.db
|
||||||
|
.env.rb
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,13 @@ if you want the development group included run this first:
|
||||||
|
|
||||||
`bundle config set --local with 'development'
|
`bundle config set --local with 'development'
|
||||||
|
|
||||||
|
then create a .env.rb file in the root directory that contains the following ENV attributes:
|
||||||
|
APP_SESSION_SECRET
|
||||||
|
DB_NAME
|
||||||
|
|
||||||
after the dependencies are installed, you have to create the db
|
after the dependencies are installed, you have to create the db
|
||||||
|
|
||||||
`sequel -m db/migrations sqlite://db/url_shortener.db`
|
`sequel -m db/migrations sqlite://db/{DB_NAME}`
|
||||||
|
|
||||||
to start the application with Falcon:
|
to start the application with Falcon:
|
||||||
|
|
||||||
|
|
|
||||||
16
app.rb
16
app.rb
|
|
@ -4,11 +4,16 @@ require 'json'
|
||||||
require 'sequel'
|
require 'sequel'
|
||||||
|
|
||||||
class App < Roda
|
class App < Roda
|
||||||
DB = Sequel.sqlite('db/url_shortener.db')
|
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
|
||||||
links = DB[:links]
|
|
||||||
plugin :render, escape: true
|
plugin :render, escape: true
|
||||||
|
plugin :flash
|
||||||
|
|
||||||
|
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
|
||||||
|
links = DB[:links]
|
||||||
|
|
||||||
route do |r|
|
route do |r|
|
||||||
r.root do
|
r.root do
|
||||||
|
@message = flash['message'] || "Enter a URL"
|
||||||
view :home
|
view :home
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -19,11 +24,16 @@ class App < Roda
|
||||||
|
|
||||||
r.post "create" do
|
r.post "create" do
|
||||||
url = r.params['url']
|
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
|
if nil == links.filter(:url => url).first
|
||||||
code = SecureRandom.urlsafe_base64 4
|
code = SecureRandom.urlsafe_base64 4
|
||||||
links.insert(url: url, code: code)
|
links.insert(url: url, code: code)
|
||||||
|
@message = "Link created"
|
||||||
end
|
end
|
||||||
|
@message ||= "Link exists"
|
||||||
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code]
|
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code]
|
||||||
view :create
|
view :create
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
require './.env'
|
||||||
require './app'
|
require './app'
|
||||||
|
|
||||||
run App.app
|
run App.app
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ Feature: Homepage
|
||||||
Scenario: Homepage Loads with a form
|
Scenario: Homepage Loads with a form
|
||||||
Given I visit the "/" page
|
Given I visit the "/" page
|
||||||
Then I should see text "URL Shortener"
|
Then I should see text "URL Shortener"
|
||||||
|
And I should see the message "Enter a URL"
|
||||||
And I should see a form field "url"
|
And I should see a form field "url"
|
||||||
And I should see a "Submit" button
|
And I should see a "Submit" button
|
||||||
|
|
||||||
|
|
@ -14,9 +15,21 @@ Feature: Homepage
|
||||||
Given I visit the "/" page
|
Given I visit the "/" page
|
||||||
When I click the "Submit" button
|
When I click the "Submit" button
|
||||||
Then I should be on "/" page
|
Then I should be on "/" page
|
||||||
|
And I should see the message "Please enter a valid URL"
|
||||||
|
|
||||||
|
@db-test
|
||||||
Scenario: Submitting the form with a correct URL
|
Scenario: Submitting the form with a correct URL
|
||||||
Given I visit the "/" page
|
Given I visit the "/" page
|
||||||
When I type "http://google.com/" in the "url" field
|
When I type "http://google.com" in the "url" field
|
||||||
And I click the "Submit" button
|
And I click the "Submit" button
|
||||||
Then I should be on "/create" page
|
Then I should be on "/create" page
|
||||||
|
And I should see the message "Link created"
|
||||||
|
|
||||||
|
@db-test
|
||||||
|
Scenario: Submitting the form with an existing URL
|
||||||
|
Given I visit the "/" page
|
||||||
|
And A link already exists with the url "http://google.com"
|
||||||
|
When I type "http://google.com" in the "url" field
|
||||||
|
And I click the "Submit" button
|
||||||
|
Then I should be on "/create" page
|
||||||
|
And I should see the message "Link exists"
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,18 @@
|
||||||
|
# BEFORE
|
||||||
|
Before('@db-test') do
|
||||||
|
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
|
||||||
|
end
|
||||||
|
|
||||||
# GIVEN
|
# GIVEN
|
||||||
|
|
||||||
Given('I visit the {string} page') do |string|
|
Given('I visit the {string} page') do |string|
|
||||||
visit string
|
visit string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Given('A link already exists with the url {string}') do |string|
|
||||||
|
@links.insert(url: string, code: "aaaaaa")
|
||||||
|
end
|
||||||
|
|
||||||
# WHEN
|
# WHEN
|
||||||
|
|
||||||
When('I click the {string} button') do |string|
|
When('I click the {string} button') do |string|
|
||||||
|
|
@ -31,3 +40,13 @@ end
|
||||||
Then('I should be on {string} page') do |string|
|
Then('I should be on {string} page') do |string|
|
||||||
page.should have_current_path string
|
page.should have_current_path string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Then('I should see the message {string}') do |message|
|
||||||
|
page.should have_selector '#message', text: message
|
||||||
|
end
|
||||||
|
|
||||||
|
# AFTER
|
||||||
|
|
||||||
|
After('@db-test') do
|
||||||
|
@links.delete
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,20 @@
|
||||||
|
require_relative '../../.env'
|
||||||
|
ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}"
|
||||||
require_relative '../../app'
|
require_relative '../../app'
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'roda'
|
require 'roda'
|
||||||
|
require 'sequel'
|
||||||
require 'capybara'
|
require 'capybara'
|
||||||
require 'capybara/dsl'
|
require 'capybara/dsl'
|
||||||
require 'rspec'
|
require 'rspec'
|
||||||
|
|
||||||
|
# DB initialization
|
||||||
|
Sequel.extension :migration
|
||||||
|
Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db|
|
||||||
|
Sequel::Migrator.apply(db, "db/migrations")
|
||||||
|
end
|
||||||
|
|
||||||
|
# attach app to Capybara
|
||||||
Capybara.app = App
|
Capybara.app = App
|
||||||
|
|
||||||
include Capybara::DSL
|
include Capybara::DSL
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
<body style="background: #FEFEFE; font-family: Helvetica, sans-serif; color: #555555;">
|
<body style="background: #FEFEFE; font-family: Helvetica, sans-serif; color: #555555;">
|
||||||
<main style="max-width: 30rem; min-width: 18rem; border: 5px solid #999999; border-radius: 20px; margin: auto; padding: 1rem; margin-top: 5rem; text-align: center; min-height: 175px;">
|
<main style="max-width: 30rem; min-width: 18rem; border: 5px solid #999999; border-radius: 20px; margin: auto; padding: 1rem; margin-top: 5rem; text-align: center; min-height: 175px;">
|
||||||
<h1>URL Shortener</h1>
|
<h1>URL Shortener</h1>
|
||||||
|
<h4 id="message"><%== @message %></h4>
|
||||||
<%== yield %>
|
<%== yield %>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue