diff --git a/Gemfile b/Gemfile index 99d8e519..a5a1010f 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'sinatra-contrib' +gem 'webrick' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..53992752 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,8 +16,11 @@ GEM diff-lcs (1.4.4) docile (1.4.0) 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,14 +78,22 @@ 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) unicode-display_width (2.0.0) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) PLATFORMS + arm64-darwin-21 x86_64-darwin-20 DEPENDENCIES @@ -93,6 +104,8 @@ DEPENDENCIES simplecov simplecov-console sinatra + sinatra-contrib + webrick RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb index 2450fb92..d9104bd4 100644 --- a/app.rb +++ b/app.rb @@ -1,6 +1,25 @@ require 'sinatra/base' +require 'sinatra/reloader' +require_relative 'lib/peep' class Chitter < Sinatra::Base + configure :development do + register Sinatra::Reloader + end + + get '/peeps' do + @peeps_return = Peep.all + erb :peeps + end + + get '/peeps/post' do + erb :post + end + + post '/peeps/new' do + Peep.post(params[:message]) + redirect '/peeps' + end get '/test' do 'Test page' end diff --git a/lib/peep.rb b/lib/peep.rb new file mode 100644 index 00000000..b0c190a5 --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,25 @@ +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;') + result.map{ |peep| peep["message"] } + end + + def self.post(message) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + connection.exec("INSERT INTO peeps (message) VALUES('#{message}');") + end +end \ No newline at end of file diff --git a/spec/features/post_peep_spec.rb b/spec/features/post_peep_spec.rb new file mode 100644 index 00000000..08c1f7ae --- /dev/null +++ b/spec/features/post_peep_spec.rb @@ -0,0 +1,9 @@ +feature 'posting a peep' do + scenario 'user wants to post a peep' do + visit('/peeps/post') + fill_in :message, with: 'I am building a twitter clone' + click_button ('Post') + + expect(page).to have_content('I am building a twitter clone') + end +end \ No newline at end of file diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb new file mode 100644 index 00000000..050b8b45 --- /dev/null +++ b/spec/features/view_peeps_spec.rb @@ -0,0 +1,16 @@ +require 'pg' +require 'peep' + +feature 'viewing peeps' do + scenario 'user wants to view peeps in browser' do + connection= PG.connect(dbname: 'chitter_test') + + connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + connection.exec("INSERT INTO peeps (message) VALUES('peep 2');") + + visit('/peeps') + + expect(page).to have_content 'This is a peep!' + expect(page).to have_content 'peep 2' + end +end \ No newline at end of file diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb new file mode 100644 index 00000000..4e0e0a7c --- /dev/null +++ b/spec/peep_spec.rb @@ -0,0 +1,24 @@ +require 'pg' +require 'peep' + +RSpec.describe '.all' do + it 'displays a list of messages' do + connection = PG.connect(dbname: 'chitter_test') + + connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + connection.exec("INSERT INTO peeps (message) VALUES('peep 2');") + + peeps = Peep.all + expect(peeps).to include 'This is a peep!' + expect(peeps).to include 'peep 2' + end +end + +describe '.post' do + it 'posts a message to chitter' do + Peep.post('I am building a twitter clone') + + expect(Peep.all).to include 'I am building a twitter clone' + end +end + \ No newline at end of file diff --git a/spec/setup_test_database.rb b/spec/setup_test_database.rb index af832f7d..42273359 100644 --- a/spec/setup_test_database.rb +++ b/spec/setup_test_database.rb @@ -1,11 +1,10 @@ require 'pg' -def setup_test_database - connection = PG.connect(dbname: 'chitter_test') - connection.exec("TRUNCATE peeps;") -end +def test_data_script + + p "setting up test chitter" + + connection = PG.connect(dbname: 'chitter_test') -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("TRUNCATE peeps;") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74046cad..41686637 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -40,7 +40,7 @@ RSpec.configure do |config| config.before(:each) do - setup_test_database + test_data_script end config.after(:suite) do diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..e69de29b diff --git a/views/peeps.erb b/views/peeps.erb new file mode 100644 index 00000000..ca648b1b --- /dev/null +++ b/views/peeps.erb @@ -0,0 +1,13 @@ +

Peeps

+ + +
+
+ +
diff --git a/views/post.erb b/views/post.erb new file mode 100644 index 00000000..a32910e6 --- /dev/null +++ b/views/post.erb @@ -0,0 +1,6 @@ +

New Post

+
+
+
+ +
\ No newline at end of file