Compare commits

...

11 Commits

Author SHA1 Message Date
bucky be17e2f146 Merge pull request 'add-flash' (#11) from add-flash into master
Reviewed-on: #11
2023-10-03 16:12:15 -07:00
Adam Townsend 80fae88228 sqlite3 note 2023-10-03 16:11:01 -07:00
Adam Townsend d93a8df199 formatting 2023-10-03 16:08:53 -07:00
Adam Townsend f0bad17e89 some formatting and more context in README 2023-10-03 16:08:16 -07:00
Adam Townsend 5ca4bf707b removed rspec db setup, i probably didn't have it set right to clean up
after each task, implemented that in the before and after steps
2023-10-03 16:02:33 -07:00
Adam Townsend 8b2780820c update for features
+ added flash/message assertions
+ adjusted urls to be http://google.com for tests for now
+ added @db-test tags to tests that use db interactions
+ added a scenario for duplicate URLs being used
+ added before and after steps to init and clean up db tests
+ added initialization of a test db for tests instead of interacting
  with the production database
2023-10-03 15:58:56 -07:00
Adam Townsend 8639c785b7 added a spot for messages to show up in the layout 2023-10-03 15:58:33 -07:00
Adam Townsend 84a047fb0c require the .env file to load ENV variables set in there 2023-10-03 15:58:01 -07:00
Adam Townsend b2b41f1aa1 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 UI
2023-10-03 15:56:42 -07:00
Adam Townsend fff6b0f5dd added more notes for the README 2023-10-03 15:55:59 -07:00
Adam Townsend c51c0afcf2 ignore the local env file 2023-10-03 15:55:37 -07:00
8 changed files with 74 additions and 6 deletions

1
.gitignore vendored
View File

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

View File

@ -5,18 +5,32 @@ Roda, Falcon, Sequel, and SQLite
the point of this project is to quickly build something and work on continuous deployment while making small refinements to the functional pieces.
the only outside piece of software that this project relies on is sqlite3.
first you have to install the dependencies:
`bundle install`
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:
```
ENV["APP_SESSION_SECRET"] = {output of a random 64 byte secret}
ENV["DB_NAME"] = {db file name}
```
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:

16
app.rb
View File

@ -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

View File

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

View File

@ -7,6 +7,7 @@ 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
@ -14,9 +15,21 @@ 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,9 +1,18 @@
# 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|
@ -31,3 +40,13 @@ 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,11 +1,20 @@
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,6 +6,7 @@
<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>