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

Chitter Challenge #222

Open
wants to merge 2 commits 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
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
11 changes: 11 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
require 'sinatra/base'
require 'pg'

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

get '/' do
'Welcome to Chitter'
end

get '/peeps' do
Chitter.add

Choose a reason for hiding this comment

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

This route should return a template that shows the user all of the peeps in the database. That means you'll need to use Peep.all at some point.

erb: "index"
end


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

Choose a reason for hiding this comment

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

detail: class names should always start with a capital letter

class peep
attr_reader :id, :message

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

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 bookmarks')
result.map {|peep| peep['message']}
end

def self.add(message:)
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end

result = connection.exec('INSERT INTO chitter (message) values(:message) ')
8 changes: 8 additions & 0 deletions spec/features/viewing_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
feature 'Viewing peeps' do
scenario 'a user can see peeps' do
visit('/peeps')
expect(page).to have_content("This is a peep!")
expect(page).to have_content("This is a second peep!")
expect(page).to have_content("This is a third peep!")
end
end
2 changes: 2 additions & 0 deletions spec/setup_test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ def setup_test_database
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("INSERT INTO peeps (message) values ('This is a second peep!');")
connection.exec("INSERT INTO peeps (message) values ('This is a third peep!');")
end
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
ENV['ENVIRONMENT'] = 'test'

ENV['RACK_ENV'] = 'test'

require File.join(File.dirname(__FILE__), '..', 'app.rb')

require 'capybara'
require 'capybara/rspec'
require 'rspec'
require_relative './setup_test_database'

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

Capybara.app = Chitter

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down
Empty file added views/index.erb
Empty file.
7 changes: 7 additions & 0 deletions views/new.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1> What's on your mind today? </h1>
<form action="/peeps" method="post">
<input type="text" name="message" placeholder="What's on your mind? :D"/>
<input type="submit" name="Submit!" />
</form>