diff --git a/.rspec b/.rspec index c99d2e73..775c62b0 100644 --- a/.rspec +++ b/.rspec @@ -1 +1,2 @@ --require spec_helper +--format documentation \ No newline at end of file diff --git a/Gemfile b/Gemfile index 99d8e519..9305876b 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'pg' gem 'sinatra' group :test do + get 'timecop' gem 'capybara' gem 'rspec' gem 'simplecov', require: false @@ -17,3 +18,5 @@ end group :development, :test do gem 'rubocop', '1.20' end + +gem "timecop", "~> 0.9.5" diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..a0dc0563 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) @@ -78,11 +80,13 @@ GEM terminal-table (3.0.1) unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) + timecop (0.9.5) unicode-display_width (2.0.0) xpath (3.2.0) nokogiri (~> 1.8) PLATFORMS + arm64-darwin-21 x86_64-darwin-20 DEPENDENCIES @@ -93,6 +97,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + timecop (~> 0.9.5) RUBY VERSION ruby 3.0.2p107 diff --git a/README.md b/README.md index f9638b66..4e94c8e1 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ As a Maker So that I can see when people are doing things I want to see the date the message was posted ``` -(Hint the database table will need to change to store the date too) +(Hint the database table will need to change to store the date too) - add an extra column ``` As a Maker diff --git a/app.rb b/app.rb index 2450fb92..8c281f57 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,36 @@ require 'sinatra/base' +require './lib/chitter' class Chitter < Sinatra::Base get '/test' do 'Test page' end + get '/' do + erb :index + end + + get '/peeps' do + @peeps = Peep.all + # @time = Peep.time + erb :peeps + end + + post '/peeps' do + Peep.create(peep: params[:peep], timestamp: params[:timestamp]) + + # post '/bookmarks' do + # Bookmark.create(url: params[:url], title: params[:title]) + # redirect '/bookmarks' + # end + # @time = Peep.time + @peeps = Peep.all + erb :peeps + end + + get '/peeps/add' do + erb :"/peeps/add" + end + run! if app_file == $0 end diff --git a/db/migrations/01_create_chitter_table.sql b/db/migrations/01_create_chitter_table.sql index 6e077248..03c56360 100644 --- a/db/migrations/01_create_chitter_table.sql +++ b/db/migrations/01_create_chitter_table.sql @@ -1 +1,2 @@ +-- To both chitter and chitter_test databases: CREATE TABLE peeps(id SERIAL PRIMARY KEY, message VARCHAR(60)); diff --git a/db/migrations/02_add_timestamp_to_chitter.sql b/db/migrations/02_add_timestamp_to_chitter.sql new file mode 100644 index 00000000..ca47350f --- /dev/null +++ b/db/migrations/02_add_timestamp_to_chitter.sql @@ -0,0 +1,2 @@ +-- To both chitter and chitter_test databases: +ALTER TABLE peeps ADD COLUMN timestamp VARCHAR(60); \ No newline at end of file diff --git a/lib/chitter.rb b/lib/chitter.rb new file mode 100644 index 00000000..88a35e9e --- /dev/null +++ b/lib/chitter.rb @@ -0,0 +1,35 @@ +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.create(peep:, timestamp:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + connection.exec("INSERT INTO peeps (message, timestamp) VALUES('#{peep}', #{timestamp})") + # connection.exec("INSERT INTO bookmarks (title, url) VALUES('#{title}', '#{url}') RETURNING id, url, title") + end + + # def self.time + # if ENV['ENVIRONMENT'] == 'test' + # connection = PG.connect(dbname: 'chitter_test') + # else + # connection = PG.connect(dbname: 'chitter') + # end + + # connection.exec("INSERT INTO peeps (timestamp) VALUES('#{Time.now}')") + # end +end diff --git a/spec/features/adding_peeps_spec.rb b/spec/features/adding_peeps_spec.rb new file mode 100644 index 00000000..8dd5f454 --- /dev/null +++ b/spec/features/adding_peeps_spec.rb @@ -0,0 +1,8 @@ +feature 'Viewing peeps' do + scenario 'viewing related timestamp' do + visit('/peeps/add') + fill_in('peep', with: 'Peep peep!') + click_button('Post') + expect(page).to have_content "Peep peep!" + end +end diff --git a/spec/features/homepage_spec.rb b/spec/features/homepage_spec.rb new file mode 100644 index 00000000..0d43a4e9 --- /dev/null +++ b/spec/features/homepage_spec.rb @@ -0,0 +1,6 @@ +feature 'Viewing homepage' do + scenario 'visiting homepage' do + visit('/') + expect(page).to have_content "Welcome to Chitter!" + end +end diff --git a/spec/features/timestamp_rspec.rb b/spec/features/timestamp_rspec.rb new file mode 100644 index 00000000..727e575b --- /dev/null +++ b/spec/features/timestamp_rspec.rb @@ -0,0 +1,8 @@ +feature 'Adding a peep' do + scenario 'adding a peep' do + visit('/peeps/add') + fill_in('peep', with: 'Peep peep!') + click_button('Post') + expect(page).to have_content "" + end +end diff --git a/spec/features/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb new file mode 100644 index 00000000..9af481c1 --- /dev/null +++ b/spec/features/viewing_peeps_spec.rb @@ -0,0 +1,26 @@ +require 'pg' +require './lib/chitter' + +feature 'Viewing peeps' do + scenario 'A user can see peeps' do + # connection = PG.connect(dbname: 'chitter_test') + + # # Add the test data + # connection.exec("INSERT INTO peeps VALUES(1, 'Peep one');") + # connection.exec("INSERT INTO peeps VALUES(2, 'Peep two');") + # connection.exec("INSERT INTO peeps VALUES(3, 'Peep three');") + + # Bookmark.create(website: "http://www.makersacademy.com") + # Bookmark.create(website: "http://www.destroyallsoftware.com") + # Bookmark.create(website: "http://www.google.com") + + Peep.create(peep: 'Peep one') + Peep.create(peep: 'Peep two') + Peep.create(peep: 'Peep three') + visit('/peeps') + + expect(page).to have_content "Peep one" + expect(page).to have_content "Peep two" + expect(page).to have_content "Peep three" + end +end diff --git a/spec/setup_test_database.rb b/spec/setup_test_database.rb index af832f7d..7c7e7e23 100644 --- a/spec/setup_test_database.rb +++ b/spec/setup_test_database.rb @@ -9,3 +9,4 @@ def add_row_to_test_database connection = PG.connect(dbname: 'chitter_test') connection.exec("INSERT INTO peeps (message) values ('This is a peep!');") end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74046cad..4870747b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,6 +19,7 @@ require 'rspec' require 'simplecov' require 'simplecov-console' +require 'timecop' require_relative './setup_test_database' @@ -41,6 +42,11 @@ RSpec.configure do |config| config.before(:each) do setup_test_database + +# new_time = Time.local(2008, 9, 1, 12, 0, 0) +# Timecop.freeze(new_time) + + end config.after(:suite) do diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..3755b0d1 --- /dev/null +++ b/views/index.erb @@ -0,0 +1 @@ +