Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chitter challenge #1234

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rvm: '2.5.0'
rvm: '2.6.0'

script:
- mkdir -p spec && bundle exec rspec spec
Expand Down
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
source 'https://rubygems.org'

ruby '2.5.0'
ruby '2.6.0'

gem 'rake'
gem 'rubocop', '0.56.0'
gem 'sinatra'

group :test do
gem 'rspec'
gem 'capybara', '~> 3.20', '>= 3.20.2'
gem 'pg'
gem 'simplecov', require: false
gem 'simplecov-console', require: false
end
38 changes: 36 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
ansi (1.5.0)
ast (2.4.0)
capybara (3.21.0)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
diff-lcs (1.3)
docile (1.1.5)
hirb (0.7.3)
json (2.1.0)
mini_mime (1.0.1)
mini_portile2 (2.4.0)
mustermann (1.0.3)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
parallel (1.12.1)
parser (2.5.1.0)
ast (~> 2.4.0)
pg (1.1.4)
powerpack (0.1.1)
public_suffix (3.0.3)
rack (2.0.7)
rack-protection (2.0.5)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rainbow (3.0.0)
rake (12.3.0)
regexp_parser (1.5.1)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
Expand Down Expand Up @@ -43,20 +66,31 @@ GEM
hirb
simplecov
simplecov-html (0.10.2)
sinatra (2.0.5)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.5)
tilt (~> 2.0)
tilt (2.0.9)
unicode-display_width (1.3.2)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
capybara (~> 3.20, >= 3.20.2)
pg
rake
rspec
rubocop (= 0.56.0)
simplecov
simplecov-console
sinatra

RUBY VERSION
ruby 2.5.0p0
ruby 2.6.0p0

BUNDLED WITH
1.16.1
1.17.2
33 changes: 33 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'sinatra/base'
require './lib/chitter'

class ChitterFeed < Sinatra::Base

# enable :method_override

get '/' do
@chitter_feed = Chitter.all
erb :index
end

get '/new_peep' do
erb :new_peep
end

post '/new_peep/add' do
Chitter.create(message: params[:message])
p params
redirect '/'
end

get '/sign_up' do
erb :sign_up
end

post '/sign_up/welcome' do
Chitter.sign_up(email: params[:email], password: params[:password], name: params[:name], username: params[:username])
p params
redirect '/'
end

end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './app'
run ChitterFeed
45 changes: 45 additions & 0 deletions lib/chitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'pg'

class Chitter

attr_reader :id, :peep, :date_time

def initialize(id:, peep:, date_time:)
@id = id
@peep = peep
@date_time = date_time
end

def self.all
if ENV['ENVIRONMENT'] == 'test'
con = PG.connect :dbname => 'chitter_test'
else
con = PG.connect :dbname => 'chitter'
end
chitter_feed = con.exec "SELECT * FROM chitter ORDER BY id DESC;"
chitter_feed.map do |feed|
Chitter.new(id: feed['id'], peep: feed['peep'], date_time: feed['date_time'])
end
end

def self.create(message:)
if ENV['ENVIRONMENT'] == 'test'
con = PG.connect :dbname => 'chitter_test'
else
con = PG.connect :dbname => 'chitter'
end
date = Time.now.strftime("%d/%m/%Y %H:%M")
result = con.exec("INSERT INTO chitter(peep, date_time) VALUES('#{message}', '#{date}' ) RETURNING id, peep, date_time;")
Chitter.new(id: result[0]['id'], peep: result[0]['peep'], date_time: result[0]['date_time'])
end

def self.sign_up(email:, password:, name:, username:)
if ENV['ENVIRONMENT'] == 'test'
con = PG.connect :dbname => 'chitter_test'
else
con = PG.connect :dbname => 'chitter'
end
con.exec("INSERT INTO users(email, password, name, username) VALUES('#{email}', '#{password}', '#{name}', '#{username}' );")
end

end
41 changes: 41 additions & 0 deletions spec/chitter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'chitter'

describe Chitter do
subject(:chitter) { described_class }

describe '.all' do
it 'returns a list of all bookmarks' do

Chitter.create(message: 'hello, welcome to Chitter!')
Chitter.create(message: 'photo of my lovely piece of broccoli')
chitter = Chitter.create(message: 'selfie photo of me')

chitter_feed = Chitter.all

expect(chitter_feed.length).to eq 3
expect(chitter_feed.first).to be_a Chitter
expect(chitter_feed.first.id).to eq chitter.id
expect(chitter_feed.first.peep).to eq 'selfie photo of me'
end
end

describe '.create' do
it 'adds a new url to bookmarks list' do
chitters = Chitter.create(message: 'hello, welcome to Chitter!')

expect(chitters).to be_a Chitter
# expect(bookmark.id).to eq persisted_data['id']
expect(chitters.peep).to eq 'hello, welcome to Chitter!'
end
end

# describe '.delete' do
# it 'deletes a url to bookmarks list' do
# bookmark = Bookmark.create(url: 'https://www.bbc.co.uk', title: 'BBC')

# Bookmark.delete(id: bookmark.id)

# expect(Bookmark.all.length).to eq 0
# end
# end
end
17 changes: 17 additions & 0 deletions spec/features/add_peep_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
feature 'post message' do

scenario 'checks that user can post a message on chitter' do

visit '/'

click_button 'Peep'

fill_in 'message', with: 'hello! welcome to chitter'

click_button 'Post Peep'

expect(page). to have_content('hello! welcome to chitter')

end

end
8 changes: 8 additions & 0 deletions spec/features/setup_test_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'pg'

def setup_test_db
p "Setting up test database..."
con = PG.connect :dbname => 'chitter_test'
con.exec "TRUNCATE TABLE chitter;"
end

28 changes: 28 additions & 0 deletions spec/features/sign_up_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# require 'pg'

# feature 'sign up' do

# scenario 'checks that user can sign up to chitter' do

# visit '/'

# click_button 'Sign Up'

# fill_in 'email', with: '[email protected]'
# fill_in 'password', with: 'carlton123'
# fill_in 'name', with: 'carlton'
# fill_in 'username', with: 'carlton7'

# click_button 'Submit'

# con = PG.connect :dbname => 'chitter_test'
# chitter_feed = con.exec "SELECT * FROM users;"
# chitter_feed.map do |feed|
# feed[1]
# end
# p chitter_feed
# expect(chitter_feed).to eq '[email protected]'

# end

# end
16 changes: 16 additions & 0 deletions spec/features/viewing_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
feature 'chitter feed' do

scenario 'checks that the / allows us to view our peeps' do

Chitter.create(message: 'hello, welcome to Chitter!')
Chitter.create(message: 'photo of my lovely piece of broccoli')
Chitter.create(message: 'selfie photo of me')

visit '/'

expect(page). to have_content('hello, welcome to Chitter!')
expect(page). to have_content('photo of my lovely piece of broccoli')
expect(page). to have_content('selfie photo of me')
end

end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
ENV['ENVIRONMENT'] = 'test'

# require our Sinatra app file
require 'simplecov'
require 'simplecov-console'
# require our Sinatra app file
require_relative '../app'
require 'features/setup_test_db'
require 'capybara/rspec'

# tell Capybara about our app class
Capybara.app = ChitterFeed

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
Expand All @@ -9,9 +19,13 @@
SimpleCov.start

RSpec.configure do |config|
config.before(:each) do
setup_test_db
end
config.after(:suite) do
puts
puts "\e[33mHave you considered running rubocop? It will help you improve your code!\e[0m"
puts "\e[33mTry it now! Just run: rubocop\e[0m"
end
end

20 changes: 20 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Welcome to Chitter</h1>
<%# <h2>Provide Player Names</h2> %>

<form action='/new_peep'>
<input type='submit' value='Peep'>
</form>

<form action='/sign_up'>
<input type='submit' value='Sign Up'>
</form>

<ul>
<% @chitter_feed.each do |feed| %>
<li>
<a><%= feed.date_time %> - <%= feed.peep %>
</a>
</li>
<% end %>
</ul>

5 changes: 5 additions & 0 deletions views/new_peep.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>Add a new post</h2>
<form action='/new_peep/add' method='post'>
<input type='text' name='message' placeholder='message'>
<input type='submit' value='Post Peep'>
</form>
8 changes: 8 additions & 0 deletions views/sign_up.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h2>Sign Up to Chitter</h2>
<form action='/sign_up/welcome' method='post'>
<input type='text' name='email' placeholder='email'>
<input type='text' name='password' placeholder='password'>
<input type='text' name='name' placeholder='name'>
<input type='text' name='username' placeholder='username'>
<input type='submit' value='Submit'>
</form>