Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request - Stephanie #226

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ ruby '3.0.2'

gem 'pg'
gem 'sinatra'
gem 'thin'
gem 'puma'
gem 'reel'
gem 'http'
gem 'webrick'

group :test do
gem 'capybara'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ GEM
mini_mime (1.1.1)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3-arm64-darwin)
racc (~> 1.4)
nokogiri (1.12.3-x86_64-darwin)
racc (~> 1.4)
parallel (1.20.1)
Expand Down Expand Up @@ -83,6 +85,7 @@ GEM
nokogiri (~> 1.8)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
Expand Down
26 changes: 26 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
require 'sinatra/base'
require './lib/messages'

class Chitter < Sinatra::Base
enable :sessions
configure do
enable :reloader
end

get '/test' do
'Test page'
end

get '/chitter' do
erb :index
end

get '/peeps' do
@messages = Messages.all
erb :allpeeps
end

get '/drafts' do
erb :post
end

post '/postpeep' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This route might be clearer and stick to conventions better if it were called '/peep/new'

peep = params['message']
Messages.create(message: peep)
redirect('/chitter')
end


run! if app_file == $0
end
23 changes: 23 additions & 0 deletions lib/messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'pg'

class Messages

def self.all
if ENV['ENVIRONMENT'] == 'test'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you not use the db setup helper here to remove this if else statement?

connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
result = connection.exec('SELECT * FROM peeps;')
result.map { |message| message['message'] }
end

def self.create(message:)
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
connection.exec("INSERT INTO peeps (message) VALUES('#{message}')")
end
end
15 changes: 15 additions & 0 deletions spec/features/adding_peep_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
feature 'Viewing added peeps' do
scenario 'adds peeps to the list' do
visit('/chitter')
click_button('Post a Peep')

expect(current_path).to eq '/drafts'

fill_in('message', with: 'This is my first peep!')
click_button('Submit')
expect(current_path).to eq '/chitter'
click_button('View all Peeps')

expect(page).to have_content "This is my first peep!"
end
end
19 changes: 19 additions & 0 deletions spec/features/view_messages_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'pg'

feature 'Viewing messages' do
scenario 'shows all messages' do

connection = PG.connect(dbname: 'chitter_test')

Messages.create(message: 'Update - I am in Paris')
Messages.create(message: 'Just got back from the park!')
Messages.create(message: 'Hi Chitter.')

visit ('/peeps')

expect(current_path).to eq '/peeps'
expect(page).to have_content 'Update - I am in Paris'
expect(page).to have_content 'Just got back from the park!'
expect(page).to have_content 'Hi Chitter.'
end
end
29 changes: 29 additions & 0 deletions spec/messages_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'messages'
require 'pg'

describe Messages do
describe '.all' do
it 'returns a list of messages' do
connection = PG.connect(dbname: 'chitter_test')

Messages.create(message: 'Hello')
Messages.create(message: 'Hi!')
Messages.create(message: 'Hiya.')

messages = Messages.all

expect(messages.length).to eq 3
expect(messages).to include "Hello"
expect(messages).to include "Hi!"
expect(messages).to include "Hiya."
end
end

describe '.create' do
it 'creates a new posted peep' do
Messages.create(message: "This is my first peep!")

expect(Messages.all).to include 'This is my first peep!'
end
end
end
6 changes: 6 additions & 0 deletions views/allpeeps.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>All Peeps:</h1>
<ul>
<% @messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
8 changes: 8 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Welcome to Chitter:</h1>
<form action="/peeps" method="get">
<input type="submit" value="View all Peeps"/>
</form>

<form action="/drafts" method='get'>
<input type="submit" value="Post a Peep">
</form>
5 changes: 5 additions & 0 deletions views/post.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Post a Peep:</h1>
<form action='/postpeep' method='post'>
<input type="text" name="message"/>
<input type="submit" value="Submit" />
</form>