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

first version #216

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 'launchy'

group :test do
gem 'capybara'
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ GEM
xpath (~> 3.2)
diff-lcs (1.4.4)
docile (1.4.0)
launchy (2.5.0)
addressable (~> 2.7)
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,6 +80,12 @@ 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)
Expand All @@ -83,16 +94,19 @@ GEM
nokogiri (~> 1.8)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
capybara
launchy
pg
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
sinatra-contrib

RUBY VERSION
ruby 3.0.2p107
Expand Down
12 changes: 12 additions & 0 deletions Views/chitter/peeps.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ul>
<% @peeps.each do |peep| %>
<li>
<%= peep.message %>
</li>
<% end %>
</ul>
Please enter a new peep below:
<form action= "/chitter/peeps" method = "post">
<input type="text" name="message" placeholder="Peep" />
<input type="submit" value="Submit" />
</form>
16 changes: 14 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'sinatra/base'
require './lib/peeps'

class Chitter < Sinatra::Base
get '/test' do
'Test page'

# get '/' do

Choose a reason for hiding this comment

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

I would probably try to avoid leaving commented out code in your code base, either you need that redirect or you don't

# redirect '/chitter/peeps'
# end

get '/chitter/peeps' do

Choose a reason for hiding this comment

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

It looks like you tried to organise your routes, however the best way to do this is with a system we call REST. For example with your routes as they are now you would presumably have /chitter/ in front of everything making it unhelpful bloat, and peeps is a bit generic and doesn't give any indication of what that route is for. Perhaps something like get '/peeps' for the landing page and then post '/peeps/new' might be a better solution for your other route. This way we know that these routes are for peeps and that one of them is to create a new one.

@peeps = Peeps.all
erb :'chitter/peeps'
end

post '/chitter/peeps' do
Peeps.create(message: params[:message])
redirect :'chitter/peeps'
end

run! if app_file == $0
Expand Down
31 changes: 31 additions & 0 deletions lib/peeps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Peeps
attr_reader :id, :message

def initialize(id:, message:)
@id = id
@message = message
end

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.

This if else is going to be needed in any function you create and it already adds quite a lot of bloat with just two at the moment, could you extract that into a separate function?

connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
result = connection.exec('SELECT * FROM peeps;')
result.map do |peep|
Peeps.new(id: peep['id'], message: peep['message'])
end
end

def self.create(message:)
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
result = connection.exec_params("INSERT INTO peeps (message) VALUES($1) RETURNING message;",
[message])
Peeps.new(id: result[0]['id'], message: result[0]['message'])
end
end
8 changes: 8 additions & 0 deletions spec/features/add_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
feature 'Add a peep' do
scenario 'A user can add a peep' do
visit('/chitter/peeps')
fill_in('message', :with => 'Leigh')
click_button('Submit')
expect(page).to have_content('Leigh')
end
end
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

11 changes: 11 additions & 0 deletions spec/features/view_peeps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
feature 'Viewing peeps' do
scenario 'A user can see peeps' do
Peeps.create(message: "hello")
Peeps.create(message: "it's")
Peeps.create(message: "me")
visit('/chitter/peeps')
expect(page).to have_content("hello")
expect(page).to have_content("it's")
expect(page).to have_content("me")
end
end