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

adding and displaying peeps #213

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

Filter by extension

Filter by extension


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

gem 'pg'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'webrick'

group :test do
gem 'capybara'
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ GEM
diff-lcs (1.4.4)
docile (1.4.0)
mini_mime (1.1.1)
multi_json (1.15.0)
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 @@ -75,14 +78,22 @@ GEM
rack (~> 2.2)
rack-protection (= 2.1.0)
tilt (~> 2.0)
sinatra-contrib (2.1.0)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.1.0)
sinatra (= 2.1.0)
tilt (~> 2.0)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (2.0.0)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
Expand All @@ -93,6 +104,8 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
sinatra-contrib
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
19 changes: 19 additions & 0 deletions app.rb
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
Comment on lines +10 to +22
Copy link
Contributor

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

get '/test' do
'Test page'
end
Comment on lines 23 to 25
Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Expand Down
25 changes: 25 additions & 0 deletions lib/peep.rb
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
9 changes: 9 additions & 0 deletions spec/features/post_peep_spec.rb
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
16 changes: 16 additions & 0 deletions spec/features/view_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'pg'
require 'peep'

feature 'viewing peeps' do
Copy link
Contributor

Choose a reason for hiding this comment

The 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
24 changes: 24 additions & 0 deletions spec/peep_spec.rb
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

13 changes: 6 additions & 7 deletions spec/setup_test_database.rb
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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

RSpec.configure do |config|
config.before(:each) do
setup_test_database
test_data_script
end

config.after(:suite) do
Expand Down
Empty file added views/index.erb
Empty file.
13 changes: 13 additions & 0 deletions views/peeps.erb
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>
6 changes: 6 additions & 0 deletions views/post.erb
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>