From f30cb2e91f8feab31b0e1e3430b6aa0be9993179 Mon Sep 17 00:00:00 2001 From: Leigh Rose Date: Fri, 1 Apr 2022 17:28:06 +0100 Subject: [PATCH] first version --- Gemfile | 2 ++ Gemfile.lock | 14 ++++++++++++++ Views/chitter/peeps.erb | 12 ++++++++++++ app.rb | 16 ++++++++++++++-- lib/peeps.rb | 31 +++++++++++++++++++++++++++++++ spec/features/add_peeps_spec.rb | 8 ++++++++ spec/features/test_page_spec.rb | 6 ------ spec/features/view_peeps.rb | 11 +++++++++++ 8 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 Views/chitter/peeps.erb create mode 100644 lib/peeps.rb create mode 100644 spec/features/add_peeps_spec.rb delete mode 100644 spec/features/test_page_spec.rb create mode 100644 spec/features/view_peeps.rb diff --git a/Gemfile b/Gemfile index 99d8e519..f8b18dcf 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'sinatra-contrib' +gem 'launchy' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..62d3dfd1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 diff --git a/Views/chitter/peeps.erb b/Views/chitter/peeps.erb new file mode 100644 index 00000000..e86fd182 --- /dev/null +++ b/Views/chitter/peeps.erb @@ -0,0 +1,12 @@ + +Please enter a new peep below: +
+ + +
diff --git a/app.rb b/app.rb index 2450fb92..2ce4d353 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,20 @@ require 'sinatra/base' +require './lib/peeps' class Chitter < Sinatra::Base - get '/test' do - 'Test page' + + # get '/' do + # redirect '/chitter/peeps' + # end + + get '/chitter/peeps' do + @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 diff --git a/lib/peeps.rb b/lib/peeps.rb new file mode 100644 index 00000000..d0fdce8f --- /dev/null +++ b/lib/peeps.rb @@ -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' + 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 diff --git a/spec/features/add_peeps_spec.rb b/spec/features/add_peeps_spec.rb new file mode 100644 index 00000000..879e7b2e --- /dev/null +++ b/spec/features/add_peeps_spec.rb @@ -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 diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb deleted file mode 100644 index b65ac196..00000000 --- a/spec/features/test_page_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -feature 'Viewing test page' do - scenario 'visiting the test page' do - visit('/test') - expect(page).to have_content "Test page" - end -end diff --git a/spec/features/view_peeps.rb b/spec/features/view_peeps.rb new file mode 100644 index 00000000..d974d80b --- /dev/null +++ b/spec/features/view_peeps.rb @@ -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