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 #1223

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4dd5d75
Initial commit
laurencetaylor May 24, 2019
7f92bed
Satisfied the first and second user stories - implemented the .post a…
laurencetaylor May 26, 2019
41989c2
Added time column
laurencetaylor May 26, 2019
c5a0b9c
Added timestamps and sign up page
laurencetaylor May 26, 2019
18b05d8
Signing up adds a user entry to the users database
laurencetaylor May 26, 2019
5a17362
Added database connection class and spec, refactored peep.rb and user.rb
laurencetaylor May 26, 2019
bca4091
Refactored sql queries in peep.rb, refactored specs to use database h…
laurencetaylor May 26, 2019
1c3f129
Added unique_email method, added web helper methods
laurencetaylor May 27, 2019
f70ad50
Added the unique_email? and unique_username? methods to user.rb
laurencetaylor May 27, 2019
af9d7fd
Added prompts to signup fail page
laurencetaylor May 27, 2019
4073c16
Added tests for showing name/ username, added userid column to peeps
laurencetaylor May 27, 2019
f36bcee
Post button only appears on index page if you are signed in
laurencetaylor May 27, 2019
3b3494b
Added get_user method in peep.rb
laurencetaylor May 27, 2019
e2ca37b
Fully implemented the sign up feature
laurencetaylor May 27, 2019
55b22dd
Refactoring
laurencetaylor May 27, 2019
4d63ea3
Added welcome message to homepage after signing up
laurencetaylor May 27, 2019
5c91261
Incorrect email on login produces a prompt
laurencetaylor May 27, 2019
1599480
Completed login functionality
laurencetaylor May 27, 2019
d185ef8
Implemented log out functionality
laurencetaylor May 27, 2019
c3c30cb
Added basic CSS styling
laurencetaylor May 27, 2019
438870f
Updated README
laurencetaylor May 28, 2019
647a270
Updated README
laurencetaylor May 28, 2019
bbf405e
Updated .travis file, trying to get tests to pass
laurencetaylor May 28, 2019
4d8188d
Updated README
laurencetaylor May 28, 2019
93d3997
Updated README
laurencetaylor May 28, 2019
f0287cb
Updated README
laurencetaylor May 28, 2019
4a0fd13
Updated README
laurencetaylor Jun 18, 2019
f9a13ee
Fixed README images
laurencetaylor Jun 18, 2019
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
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
rvm: '2.5.0'

services:
- postgresql

script:
- mkdir -p spec && bundle exec rspec spec

Expand All @@ -9,3 +12,7 @@ before_install:

before_script:
- psql -c 'create database chitter_test;' -U postgres
- psql chitter_test -c 'CREATE TABLE peeps(id SERIAL PRIMARY KEY, text VARCHAR(280));'
- psql chitter_test -c 'ALTER TABLE peeps ADD time timestamp DEFAULT NOW();'
- psql chitter_test -c 'CREATE TABLE users(id SERIAL PRIMARY KEY, email VARCHAR(60), password VARCHAR(60), name VARCHAR(60), username VARCHAR(60));'
- psql chitter_test -c 'ALTER TABLE peeps ADD userid INT;'
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ source 'https://rubygems.org'

ruby '2.5.0'

gem 'pg'
gem 'rake'
gem 'rubocop', '0.56.0'
gem 'sinatra'
gem 'sinatra-flash'

group :test do
gem 'capybara'
gem 'rspec'
gem 'simplecov', require: false
gem 'simplecov-console', require: false
Expand Down
39 changes: 38 additions & 1 deletion 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.20.2)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.2)
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,34 @@ 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)
sinatra-flash (0.3.0)
sinatra (>= 1.0.0)
tilt (2.0.9)
unicode-display_width (1.3.2)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
capybara
pg
rake
rspec
rubocop (= 0.56.0)
simplecov
simplecov-console
sinatra
sinatra-flash

RUBY VERSION
ruby 2.5.0p0

BUNDLED WITH
1.16.1
1.17.2
114 changes: 46 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
Chitter Challenge
=================

* Challenge time: rest of the day and weekend, until Monday 9am
* Feel free to use Google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning

Challenge:
The challenge:
-------

As usual please start by forking this repo.

We are going to write a small Twitter clone that will allow the users to post messages to a public stream.
* The goal was to create a twitter clone, which lets a user sign up, log in, post, and log out

Features:
-------
* User stories as follows:

```
STRAIGHT UP

As a Maker
So that I can let people know what I am doing
I want to post a message (peep) to chitter
Expand All @@ -36,8 +25,6 @@ As a Maker
So that I can post messages on Chitter as me
I want to sign up for Chitter

HARDER

As a Maker
So that only I can post messages on Chitter as me
I want to log in to Chitter
Expand All @@ -46,88 +33,79 @@ As a Maker
So that I can avoid others posting messages on Chitter as me
I want to log out of Chitter

ADVANCED

As a Maker
So that I can stay constantly tapped in to the shouty box of Chitter
I want to receive an email if I am tagged in a Peep
```

Technical Approach:
-----

This week you integrated a database into Bookmark Manager using the `PG` gem and `SQL` queries. You can continue to use this approach when building Chitter Challenge.

If you'd like more technical challenge this weekend, try using an [Object Relational Mapper](https://en.wikipedia.org/wiki/Object-relational_mapping) as the database interface.
* Signing up with an in-use email or username should display a relevant prompt

Some useful resources:
**DataMapper**
- [DataMapper ORM](https://datamapper.org/)
- [Sinatra, PostgreSQL & DataMapper recipe](http://recipes.sinatrarb.com/p/databases/postgresql-datamapper)
* Logging in with incorrect details should display a prompt

**ActiveRecord**
- [ActiveRecord ORM](https://guides.rubyonrails.org/active_record_basics.html)
- [Sinatra, PostgreSQL & ActiveRecord recipe](http://recipes.sinatrarb.com/p/databases/postgresql-activerecord?#article)

Notes on functionality:
------

* You don't have to be logged in to see the peeps.
* Makers sign up to chitter with their email, password, name and a username (e.g. [email protected], password123, Sam Morgan, sjmog).
* Users sign up to chitter with their email, password, name and a username (e.g. [email protected], password123, Sam Morgan, sjmog).
* The username and email are unique.
* Peeps (posts to chitter) have the name of the maker and their user handle.
* Your README should indicate the technologies used, and give instructions on how to install and run the tests.

Bonus:

How to use:
-----

If you have time you can implement the following:
* Clone this directory

* In order to start a conversation as a maker I want to reply to a peep from another maker.
* Download postgres sql (e.g. by typing brew install postgressql in the command line)

And/Or:
* Open postgres (psql in the command line) and create two databases called chitter and chitter_test using the following command:

* Work on the CSS to make it look good.
```
CREATE DATABASE database_name
```

* For each database enter the commands in db/migrations (in order!)

* Run bundle install in the command line

* Run rackup in the command line and visit localhost in a browser with the correct port number

* To run tests enter rspec in the command line from the root directory

Good luck and let the chitter begin!
* Visit the /peeps route and interact! Click 'Sign up' to register a username/ email,
'Peep' to post a peep, 'Log out' to sign out and 'Log in' to sign in

Code Review
-----------
![homepagescreenshot](./images/screenshot1.png)
Viewing the page while logged out: only the sign up and sign in options are available.

In code review we'll be hoping to see:
![signuppagescreenshot](./images/screenshot2.png)
Viewing the sign up page.

* All tests passing
* High [Test coverage](https://github.com/makersacademy/course/blob/master/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.
![signedinhomepage](./images/screenshot3.png)
While signed in users have the options to post and sign out!

Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance may make the challenge somewhat easier. You should be the judge of how much challenge you want this weekend.

Automated Tests:
Technical Approach:
-----

Opening a pull request against this repository will will trigger Travis CI to perform a build of your application and run your full suite of RSpec tests. If any of your tests rely on a connection with your database - and they should - this is likely to cause a problem. The build of your application created by has no connection to the local database you will have created on your machine, so when your tests try to interact with it they'll be unable to do so and will fail.
* TDD approach, outside-in (i.e. feature tests first, then unit tests)

If you want a green tick against your pull request you'll need to configure Travis' build process by adding the necessary steps for creating your database to the `.travis.yml` file.
* Used the MVC pattern

- [Travis Basics](https://docs.travis-ci.com/user/tutorial/)
- [Travis - Setting up Databases](https://docs.travis-ci.com/user/database-setup/)
* Stored user and peep information (timestamp, text, username and so on) within tables of a database. Accessed these using sql and wrapped them in user and peep ruby objects respectively

Notes on test coverage
----------------------
* One-to-many relationship between users and peeps meant storing userid as a foreign key within the peeps table

Please ensure you have the following **AT THE TOP** of your spec_helper.rb in order to have test coverage stats generated
on your pull request:

```ruby
require 'simplecov'
require 'simplecov-console'
Thoughts
-----

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
])
SimpleCov.start
```
* Storing passwords in plaintext, probably not a good idea! Could use a gem to encrypt these

* Did not test the timestamping, could use Time.now in ruby rather than handling this on the database
side. Would make testing easier (e.g. using the gem timecop or a mock)

* Parts of the controller are a little long, will have to consider if it's possible to refactor

* Could have used an ORM such as datamapper

You can see your test coverage when you run your tests. If you want this in a graphical form, uncomment the `HTMLFormatter` line and see what happens!
* Improvements to HTML/ CSS would be nice to have
73 changes: 73 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'sinatra/base'
require 'sinatra/flash'
require_relative './lib/peep.rb'
require_relative './lib/user.rb'
require_relative './lib/database_connection_setup.rb'

class Chitter < Sinatra::Base
enable :sessions
register Sinatra::Flash

get '/peeps' do
@peeps = Peep.all
@logged_in = !session[:userid].nil?
@name = session[:name]
erb(:'peeps/index')
end

get '/peeps/post' do
erb(:'peeps/post')
end

post '/peeps' do
Peep.post(text: params[:text], userid: session[:userid])
redirect '/peeps'
end

get '/users/signup' do
erb(:'users/signup')
end

post '/users' do
if !User.unique_email?(params[:email])
flash[:notice] = 'Email in use'
redirect '/users/signup'
elsif !User.unique_username?(params[:username])
flash[:notice] = 'Username in use'
redirect '/users/signup'
end

user = User.sign_up(email: params[:email], password: params[:password],
name: params[:name], username: params[:username])

session[:userid] = user.id
session[:name] = user.name

redirect '/peeps'
end

get '/login/new' do
erb(:'users/login')
end

post '/login' do
user = User.authenticate(email: params[:email], password: params[:password])

if user
session[:userid] = user.id
session[:name] = user.name
redirect '/peeps'
else
flash[:notice] = 'Your email or password is incorrect'
redirect '/login/new'
end
end

post '/logout' do
session.clear
flash[:notice] = 'You have logged out'
redirect '/peeps'
end

run! if app_file == $0
end
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative './app.rb'

run Chitter
1 change: 1 addition & 0 deletions db/migrations/01_create_peeps_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE peeps(id SERIAL PRIMARY KEY, text VARCHAR(280));
1 change: 1 addition & 0 deletions db/migrations/02_add_time_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE peeps ADD time timestamp DEFAULT NOW();
1 change: 1 addition & 0 deletions db/migrations/03_create_users_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE users(id SERIAL PRIMARY KEY, email VARCHAR(60), password VARCHAR(60), name VARCHAR(60), username VARCHAR(60));
1 change: 1 addition & 0 deletions db/migrations/04_add_userid_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE peeps ADD userid INT;
Binary file added images/screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/screenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/screenshot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions lib/database_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'pg'

class DatabaseConnection
def self.setup(dbname)
@connection = PG.connect(dbname: dbname)
end

def self.query(sql)
@connection.exec(sql)
end

class << self
attr_reader :connection
end
end
7 changes: 7 additions & 0 deletions lib/database_connection_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative './database_connection.rb'

if ENV['ENVIRONMENT'] == 'test'
DatabaseConnection.setup('chitter_test')
else
DatabaseConnection.setup('chitter')
end
Loading