-
Notifications
You must be signed in to change notification settings - Fork 235
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
adding and displaying peeps #213
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ ruby '3.0.2' | |
|
||
gem 'pg' | ||
gem 'sinatra' | ||
gem 'sinatra-contrib' | ||
gem 'webrick' | ||
|
||
group :test do | ||
gem 'capybara' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
require 'sinatra/base' | ||
require 'sinatra/reloader' | ||
require_relative 'lib/peep' | ||
|
||
class Chitter < Sinatra::Base | ||
configure :development do | ||
register Sinatra::Reloader | ||
end | ||
|
||
get '/peeps' do | ||
@peeps_return = Peep.all | ||
erb :peeps | ||
end | ||
|
||
get '/peeps/post' do | ||
erb :post | ||
end | ||
|
||
post '/peeps/new' do | ||
Peep.post(params[:message]) | ||
redirect '/peeps' | ||
end | ||
get '/test' do | ||
'Test page' | ||
end | ||
Comment on lines
23
to
25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's always a good idea to remove test code from code you submit for code review (anywhere, not just at Makers) |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'pg' | ||
|
||
class Peep | ||
|
||
def self.all | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
|
||
result = connection.exec('SELECT * FROM peeps;') | ||
result.map{ |peep| peep["message"] } | ||
end | ||
|
||
def self.post(message) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
Comment on lines
+17
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This piece of code appears twice in this file. How might you remove this duplication? |
||
|
||
connection.exec("INSERT INTO peeps (message) VALUES('#{message}');") | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
feature 'posting a peep' do | ||
scenario 'user wants to post a peep' do | ||
visit('/peeps/post') | ||
fill_in :message, with: 'I am building a twitter clone' | ||
click_button ('Post') | ||
|
||
expect(page).to have_content('I am building a twitter clone') | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'pg' | ||
require 'peep' | ||
|
||
feature 'viewing peeps' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice feature tests! |
||
scenario 'user wants to view peeps in browser' do | ||
connection= PG.connect(dbname: 'chitter_test') | ||
|
||
connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") | ||
connection.exec("INSERT INTO peeps (message) VALUES('peep 2');") | ||
|
||
visit('/peeps') | ||
|
||
expect(page).to have_content 'This is a peep!' | ||
expect(page).to have_content 'peep 2' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'pg' | ||
require 'peep' | ||
|
||
RSpec.describe '.all' do | ||
it 'displays a list of messages' do | ||
connection = PG.connect(dbname: 'chitter_test') | ||
|
||
connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") | ||
connection.exec("INSERT INTO peeps (message) VALUES('peep 2');") | ||
|
||
peeps = Peep.all | ||
expect(peeps).to include 'This is a peep!' | ||
expect(peeps).to include 'peep 2' | ||
end | ||
end | ||
|
||
describe '.post' do | ||
it 'posts a message to chitter' do | ||
Peep.post('I am building a twitter clone') | ||
|
||
expect(Peep.all).to include 'I am building a twitter clone' | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
require 'pg' | ||
|
||
def setup_test_database | ||
connection = PG.connect(dbname: 'chitter_test') | ||
connection.exec("TRUNCATE peeps;") | ||
end | ||
def test_data_script | ||
|
||
p "setting up test chitter" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, remove test code. |
||
|
||
connection = PG.connect(dbname: 'chitter_test') | ||
|
||
def add_row_to_test_database | ||
connection = PG.connect(dbname: 'chitter_test') | ||
connection.exec("INSERT INTO peeps (message) values ('This is a peep!');") | ||
connection.exec("TRUNCATE peeps;") | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1>Peeps</h1> | ||
|
||
<ul> | ||
<% @peeps_return.each do |peep| %> | ||
<li> | ||
<%= peep %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
<br> | ||
<form action="/peeps/post"> | ||
<input type="submit" value="New Post"> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>New Post</h1> | ||
<br> | ||
<form action="/peeps/new" method="post"> | ||
<input type="text" name="message" placeholder="Enter post"><br> | ||
<input type="submit" value="Post" > | ||
</form> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your routs are quite close to the standard RESTful routing conventions, but not quite! If you'd like to use more standard names for you routes, check out this blog post on RESTful routing: https://medium.com/@shubhangirajagrawal/the-7-restful-routes-a8e84201f206