From f5d2c53042885352dddab04361ac43dcf722123e Mon Sep 17 00:00:00 2001 From: jkgiwa Date: Mon, 4 Apr 2022 09:43:58 +0100 Subject: [PATCH 1/2] added extras --- Gemfile.lock | 3 +++ app.rb | 11 +++++++++++ peep.rb | 29 +++++++++++++++++++++++++++++ spec/features/viewing_peeps_spec.rb | 8 ++++++++ spec/setup_test_database.rb | 2 ++ spec/spec_helper.rb | 19 +++++++++++++++++++ views/index.erb | 0 views/new.erb | 7 +++++++ 8 files changed, 79 insertions(+) create mode 100644 peep.rb create mode 100644 spec/features/viewing_peeps_spec.rb create mode 100644 views/index.erb create mode 100644 views/new.erb 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..c5dd6b82 100644 --- a/app.rb +++ b/app.rb @@ -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 + "This is a peep!" + erb: index + end + + run! if app_file == $0 end diff --git a/peep.rb b/peep.rb new file mode 100644 index 00000000..9f901f3d --- /dev/null +++ b/peep.rb @@ -0,0 +1,29 @@ +require 'pg' + +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) ') diff --git a/spec/features/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb new file mode 100644 index 00000000..9bab8e45 --- /dev/null +++ b/spec/features/viewing_peeps_spec.rb @@ -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 diff --git a/spec/setup_test_database.rb b/spec/setup_test_database.rb index af832f7d..3761de26 100644 --- a/spec/setup_test_database.rb +++ b/spec/setup_test_database.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74046cad..0e8e653e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..e69de29b diff --git a/views/new.erb b/views/new.erb new file mode 100644 index 00000000..ed4bb3c2 --- /dev/null +++ b/views/new.erb @@ -0,0 +1,7 @@ +

What's on your mind today?

+
+ + +
+ + From 903a15a6ac24ce06a68eba91242104865e104409 Mon Sep 17 00:00:00 2001 From: jkgiwa Date: Mon, 4 Apr 2022 09:45:25 +0100 Subject: [PATCH 2/2] saved app.rb and index.erb --- app.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.rb b/app.rb index c5dd6b82..56b5e8a7 100644 --- a/app.rb +++ b/app.rb @@ -11,8 +11,8 @@ class Chitter < Sinatra::Base end get '/peeps' do - "This is a peep!" - erb: index + Chitter.add + erb: "index" end