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..09bad94f 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,15 @@ require 'sinatra/base' +require_relative './lib/little_chits' class Chitter < Sinatra::Base get '/test' do 'Test page' end + get '/chitter' do + @all_chits = Chits.all + erb(:index) + end + run! if app_file == $0 end diff --git a/lib/little_chits.rb b/lib/little_chits.rb new file mode 100644 index 00000000..666c5ba6 --- /dev/null +++ b/lib/little_chits.rb @@ -0,0 +1,18 @@ +require 'pg' + +class Chits + + 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 { |chit| chit['message'] } + + end + +end \ No newline at end of file diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb index b65ac196..f2006dac 100644 --- a/spec/features/test_page_spec.rb +++ b/spec/features/test_page_spec.rb @@ -1,6 +1,19 @@ +require 'pg' + feature 'Viewing test page' do scenario 'visiting the test page' do visit('/test') expect(page).to have_content "Test page" end + + scenario 'visit chitter and see chits' do + connection = PG.connect(dbname: 'chitter_test') + connection.exec("INSERT INTO peeps VALUES(1, 'test-peep');") + + visit('/chitter') + + expect(page).to have_content "Welcome to Chitter!" + expect(page).to have_content "test-peep" + end + end diff --git a/spec/little_chits_spec.rb b/spec/little_chits_spec.rb new file mode 100644 index 00000000..227b603f --- /dev/null +++ b/spec/little_chits_spec.rb @@ -0,0 +1,22 @@ +require 'little_chits' + +describe Chits do + + describe '.all' do + + it 'shows all chits' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message) VALUES ('Chit One');") + connection.exec("INSERT INTO peeps (message) VALUES('Chit Two');") + + all_chits = Chits.all + + expect(all_chits).to include("Chit One") + expect(all_chits).to include("Chit Two") + end + + end + +end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..2c6f6206 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,10 @@ +
+
+