Skip to content
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
16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ source 'https://rubygems.org'

ruby '3.0.2'

gem 'bcrypt'

gem "pg", "~> 1.3"

gem "rack-test", "~> 1.1"

gem "sinatra", "~> 2.2"

gem "webrick", "~> 1.7"

gem "sinatra-contrib", "~> 2.2"

gem "rackup", "~> 1.0"

gem 'actionmailer'

group :test do
gem 'rspec'
gem 'simplecov', require: false
Expand Down
153 changes: 130 additions & 23 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,29 +1,111 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (7.0.4.3)
actionpack (= 7.0.4.3)
actionview (= 7.0.4.3)
activejob (= 7.0.4.3)
activesupport (= 7.0.4.3)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.4.3)
actionview (= 7.0.4.3)
activesupport (= 7.0.4.3)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actionview (7.0.4.3)
activesupport (= 7.0.4.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.4.3)
activesupport (= 7.0.4.3)
globalid (>= 0.3.6)
activesupport (7.0.4.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ansi (1.5.0)
ast (2.4.2)
diff-lcs (1.4.4)
bcrypt (3.1.18)
builder (3.2.4)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
diff-lcs (1.5.0)
docile (1.4.0)
parallel (1.20.1)
parser (3.0.2.0)
erubi (1.12.0)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
loofah (2.20.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
mini_mime (1.1.2)
mini_portile2 (2.8.1)
minitest (5.18.0)
multi_json (1.15.0)
mustermann (2.0.2)
ruby2_keywords (~> 0.0.1)
net-imap (0.3.4)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
timeout
net-smtp (0.3.3)
net-protocol
nokogiri (1.14.3)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
rainbow (3.0.0)
regexp_parser (2.1.1)
pg (1.4.6)
racc (1.6.2)
rack (2.2.6.4)
rack-protection (2.2.4)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rackup (1.0.0)
rack (< 3)
webrick
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
rainbow (3.1.1)
regexp_parser (2.7.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.1)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.20.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
Expand All @@ -33,10 +115,11 @@ GEM
rubocop-ast (>= 1.9.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
simplecov (0.21.2)
rubocop-ast (1.28.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
Expand All @@ -45,19 +128,43 @@ GEM
simplecov
terminal-table
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
terminal-table (3.0.1)
simplecov_json_formatter (0.1.4)
sinatra (2.2.4)
mustermann (~> 2.0)
rack (~> 2.2)
rack-protection (= 2.2.4)
tilt (~> 2.0)
sinatra-contrib (2.2.4)
multi_json
mustermann (~> 2.0)
rack-protection (= 2.2.4)
sinatra (= 2.2.4)
tilt (~> 2.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
unicode-display_width (2.0.0)
tilt (2.1.0)
timeout (0.3.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
webrick (1.8.1)

PLATFORMS
ruby

DEPENDENCIES
actionmailer
bcrypt
pg (~> 1.3)
rack-test (~> 1.1)
rackup (~> 1.0)
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra (~> 2.2)
sinatra-contrib (~> 2.2)
webrick (~> 1.7)

RUBY VERSION
ruby 3.0.2p107
Expand Down
9 changes: 9 additions & 0 deletions bcrypt.rb

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this contributes to Chitter - could we delete it?

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'bcrypt'

password = BCrypt::Password.create("Just some silly")

puts password

decrypt = BCrypt::Password.new("$2a$12$anT7x2jJ4Ofj0qmCeahahORBu8NQI76lG2EPKDlUXIQgug0hp5KjG")

puts decrypt == "blue"
6 changes: 6 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# file: config.ru
require './controllers/application_controller'
require './controllers/user_controller'

use UserController
run Application
40 changes: 40 additions & 0 deletions controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# file: app.rb
require 'sinatra'
require_relative '../repo/peep_repository'
require_relative '../repo/user_repository'
#require "sinatra/reloader"
require_relative './database_connection'
require_relative '../models/mailer'

DatabaseConnection.connect

class Application < Sinatra::Base

configure :development do
#register Sinatra::Reloader
#also_reload 'lib/peep'
#also_reload 'lib/user'
enable :sessions
end

get '/' do
peeps_repo = PeepRepository.new
@user_repo = UserRepository.new
@peeps = peeps_repo.all
erb(:"app/index")
end
Comment on lines +20 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this, super clean and clear!


post '/new' do
content = params[:content]
author = session[:user_id]
peep = Peep.new
peep.content = content
peep.author = author
peep_repository = PeepRepository.new
if peep.content.match(/@\w+/)
puts "we are here"
end
peep_repository.create(peep)
redirect '/'
end
Comment on lines +27 to +39

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it's doing a lot - could it be broken up to make it more digestible?

end
33 changes: 33 additions & 0 deletions controllers/database_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# file: lib/database_connection.rb

require 'pg'

# This class is a thin "wrapper" around the
# PG library. We'll use it in our project to interact
# with the database using SQL.

class DatabaseConnection
# This method connects to PostgreSQL using the
# PG gem. We connect to 127.0.0.1, and select
# the database name given in argument.
def self.connect
if ENV['ENV'] == 'test'
database_name = 'chitter'
else
database_name = 'chitter'
end
@connection = PG.connect({ host: '127.0.0.1', dbname: database_name })
end

# This method executes an SQL query
# on the database, providing some optional parameters
# (you will learn a bit later about when to provide these parameters).
def self.exec_params(query, params)
if @connection.nil?
raise 'DatabaseConnection.exec_params: Cannot run a SQL query as the connection to'\
'the database was never opened. Did you make sure to call first the method '\
'`DatabaseConnection.connect` in your app.rb file (or in your tests spec_helper.rb)?'
end
@connection.exec_params(query, params)
end
end
65 changes: 65 additions & 0 deletions controllers/user_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#require_relative '../models/user'
require_relative '../repo/user_repository'
require 'sinatra'
require 'bcrypt'
#require "sinatra/reloader"
require_relative './database_connection'

DatabaseConnection.connect

class UserController < Application
configure :development do
enable :sessions
end

def user_form_invalid
return true if params[:name].length == 0 || params[:email].length == 0 || params[:username].length == 0 || params[:password].length == 0
return false
end

def sanitise(list)
['<html>', '<script>'].any? { |inj| list.include?(inj) }
end

post '/signup' do
if user_form_invalid
status 400
return ''
end
name = params[:name]
username = params[:username]
email = params[:email]
password = params[:password]
sanitise_list = [name, username, email, password]
if sanitise(sanitise_list)
return "Nice try Buddy!"
end
user_repo = UserRepository.new
user = User.new
user.name = name
user.username = username
user.email = email
user.password = BCrypt::Password.create(password)

user_repo.create(user)

session[:user_id] = user.user_id
redirect '/'
end

post '/login' do
user_repo = UserRepository.new
user = user_repo.find_user_by_username(params[:username])
if user && BCrypt::Password.new(user.password) == params[:password]
session[:user_id] = user.user_id
redirect '/'
else
return "Username not found or incorrect credentials"
end
end

get '/logout' do
session[:user_id] = nil
redirect '/'
end
end
Loading