Compare commits

..

No commits in common. "5ca4bf707b84221ec5cb48fc2b7f264d07d45232" and "88b52df610097923b61cfe5121146689c38ad162" have entirely different histories.

8 changed files with 5 additions and 63 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
*.sw*
.bundle
*.db
.env.rb

View File

@ -14,13 +14,9 @@ if you want the development group included run this first:
`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
`sequel -m db/migrations sqlite://db/{DB_NAME}`
`sequel -m db/migrations sqlite://db/url_shortener.db`
to start the application with Falcon:

16
app.rb
View File

@ -4,16 +4,11 @@ require 'json'
require 'sequel'
class App < Roda
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
plugin :render, escape: true
plugin :flash
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
DB = Sequel.sqlite('db/url_shortener.db')
links = DB[:links]
plugin :render, escape: true
route do |r|
r.root do
@message = flash['message'] || "Enter a URL"
view :home
end
@ -24,16 +19,11 @@ class App < Roda
r.post "create" do
url = r.params['url']
if url.empty?
flash['message'] = "Please enter a valid URL";
r.redirect '/'
end
if url.empty? then 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

View File

@ -1,4 +1,3 @@
require './.env'
require './app'
run App.app

View File

@ -7,7 +7,6 @@ Feature: Homepage
Scenario: Homepage Loads with a form
Given I visit the "/" page
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 "Submit" button
@ -15,21 +14,9 @@ Feature: Homepage
Given I visit the "/" page
When I click the "Submit" button
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
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
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"

View File

@ -1,18 +1,9 @@
# BEFORE
Before('@db-test') do
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
end
# GIVEN
Given('I visit the {string} page') do |string|
visit string
end
Given('A link already exists with the url {string}') do |string|
@links.insert(url: string, code: "aaaaaa")
end
# WHEN
When('I click the {string} button') do |string|
@ -40,13 +31,3 @@ end
Then('I should be on {string} page') do |string|
page.should have_current_path string
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

View File

@ -1,20 +1,11 @@
require_relative '../../.env'
ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}"
require_relative '../../app'
require 'rubygems'
require 'roda'
require 'sequel'
require 'capybara'
require 'capybara/dsl'
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
include Capybara::DSL

View File

@ -6,7 +6,6 @@
<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;">
<h1>URL Shortener</h1>
<h4 id="message"><%== @message %></h4>
<%== yield %>
</main>
</body>