From a69a9529267b6bac0a31831866c36d156a53a538 Mon Sep 17 00:00:00 2001 From: jordanmarkham Date: Mon, 18 Jul 2022 11:09:15 +0200 Subject: [PATCH] User stories 1 and 2 completed (temporary solution to user story 4) --- Gemfile | 1 + Gemfile.lock | 3 +++ app.rb | 17 +++++++++++++++++ lib/peep.rb | 24 ++++++++++++++++++++++++ spec/features/create_peep_spec.rb | 10 ++++++++++ spec/features/peeps_spec.rb | 19 +++++++++++++++++++ spec/homepage_spec.rb | 28 ++++++++++++++++++++++++++++ views/home.erb | 10 ++++++++++ 8 files changed, 112 insertions(+) create mode 100644 lib/peep.rb create mode 100644 spec/features/create_peep_spec.rb create mode 100644 spec/features/peeps_spec.rb create mode 100644 spec/homepage_spec.rb create mode 100644 views/home.erb diff --git a/Gemfile b/Gemfile index 99d8e519..d0d971e8 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'sinatra-contrib' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..21826b1e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -83,6 +85,7 @@ GEM nokogiri (~> 1.8) PLATFORMS + arm64-darwin-21 x86_64-darwin-20 DEPENDENCIES diff --git a/app.rb b/app.rb index 2450fb92..20b45d6d 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,26 @@ require 'sinatra/base' +require 'sinatra/reloader' +require_relative 'lib/peep' class Chitter < Sinatra::Base + configure :development do + register Sinatra::Reloader + end + get '/test' do 'Test page' end + get '/' do + @all_peeps = Peep.all + erb(:home) + end + + post '/' do + Peep.create(params[:message]) + redirect("/") + end + + run! if app_file == $0 end diff --git a/lib/peep.rb b/lib/peep.rb new file mode 100644 index 00000000..a500b95f --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,24 @@ +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" + return result.map{ |elem| elem["message"]}.reverse + end + + def self.create(peep) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + connection.exec "INSERT INTO peeps (message) VALUES('#{peep}')" + end +end diff --git a/spec/features/create_peep_spec.rb b/spec/features/create_peep_spec.rb new file mode 100644 index 00000000..83b580e6 --- /dev/null +++ b/spec/features/create_peep_spec.rb @@ -0,0 +1,10 @@ +feature 'Creating a new peep' do + scenario 'A user can write and send new Peeps' do + + visit('/') + fill_in('message', with: "This is a new Peep!") + click_button('Submit') + + expect(page).to have_content "This is a new Peep!" + end +end diff --git a/spec/features/peeps_spec.rb b/spec/features/peeps_spec.rb new file mode 100644 index 00000000..4cd52dc6 --- /dev/null +++ b/spec/features/peeps_spec.rb @@ -0,0 +1,19 @@ +require 'pg' +require 'peep' + +feature 'Viewing Peeps' do + scenario 'A user can see Peeps' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + Peep.create('New Peep 1!') + Peep.create('Yoyoyo!') + Peep.create('http://www.google.com') + + visit('/') + + expect(page).to have_content "New Peep 1!" + expect(page).to have_content "Yoyoyo!" + expect(page).to have_content "http://www.google.com" + end +end \ No newline at end of file diff --git a/spec/homepage_spec.rb b/spec/homepage_spec.rb new file mode 100644 index 00000000..1e2cfa42 --- /dev/null +++ b/spec/homepage_spec.rb @@ -0,0 +1,28 @@ +require 'pg' +require 'peep' + +describe '.all' do + it 'returns all peeps in reverse chronological order' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + Peep.create('New Peep 1!') + Peep.create('Yoyoyo!') + Peep.create('http://www.google.com') + + peeps = Peep.all + + expect(peeps).to include('New Peep 1!') + expect(peeps).to include('Yoyoyo!') + expect(peeps).to include('http://www.google.com') + end +end + +describe '.create' do + it 'adds peeps to the database' do + connection = PG.connect(dbname: 'chitter_test') + Peep.create("Peep peep!") + peeps = Peep.all + expect(peeps).to include('Peep peep!') + end +end \ No newline at end of file diff --git a/views/home.erb b/views/home.erb new file mode 100644 index 00000000..9a979eac --- /dev/null +++ b/views/home.erb @@ -0,0 +1,10 @@ +
+

Chitter:

+ +
+ + +
+ +

<%= @all_peeps.join('/n') %>

+
\ No newline at end of file