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

WIP: Add user profile #47

Open
wants to merge 7 commits into
base: master
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: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'https://rubygems.org'

gemspec

gem 'thin'
10 changes: 7 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require './app'
require 'sinatra/activerecord/rake'

require 'rspec/core/rake_task'
begin
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task :default => :spec
rescue LoadError
puts "We don't have rspec"
end

namespace :db do
desc "Populate the database with example data"
Expand Down
14 changes: 12 additions & 2 deletions lib/ppwm_matcher/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,18 @@ def authorized?
end
end

get '/profile/:github_login' do
# TODO revisit how to ensure we get a safe string
github_login = params[:github_login].to_s.gsub(/[\s\-\/\\\.]/, '')
@user = User.current(github_login)
if @user
erb :profile, layout: :layout
else
"No such user"
end
end
get '/code' do
user = User.current(github_user) # TODO: refactor to helper method ?
user = User.current(github_user.login) # TODO: refactor to helper method ?
redirect '/' unless user && user.code

@pair = user.pair
Expand Down Expand Up @@ -136,7 +146,7 @@ def setup_for_root_path(messages = nil)
@email = params['email'] || github_user.email
@name = github_user.name || github_user.login

user = User.current(github_user)
user = User.current(github_user.login)
if user && user.has_code?
@has_code = true
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ppwm_matcher/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def pair
User.where(:code_id => self.code_id).detect{|u| u != self }
end

def self.current(github_user)
where(:github_login => github_user.login).limit(1).first
def self.current(github_login)
where(:github_login => github_login).limit(1).first
end

def self.update_or_create(email, github_user)
Expand Down
7 changes: 7 additions & 0 deletions lib/ppwm_matcher/views/profile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= @user.name %>'s Profile
<br>
<ul>
<li>Email : <%= @user.email %></li>
<li>Github user : <%= @user.github_login %></li>
<li><img src="https://secure.gravatar.com/avatar/<%= @user.gravatar_id %>" alt="Gravatar" style="margin-right: 18px" /></li>
</ul>
1 change: 0 additions & 1 deletion ppwm-matcher.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Gem::Specification.new do |s|
s.add_dependency 'rake'
s.add_dependency 'pony'

s.add_development_dependency 'thin'
s.add_development_dependency 'rspec'
s.add_development_dependency 'faker'
s.add_development_dependency 'factory_girl'
Expand Down
23 changes: 23 additions & 0 deletions spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ def app
end
end

describe "GET /profile/:github_login" do
context 'the user is authorized' do
it 'if the user exists, says Hello github_login' do
login_as github_user

user = PpwmMatcher::User.update_or_create('[email protected]', github_user)
get "/profile/#{user.github_login}"

[:github_login, :name, :email, :gravatar_id].each do |profile_attribute|
expect(last_response.body).to include(user.public_send(profile_attribute))
end

end
it 'if user does not exists,says No such user' do
login_as github_user

get "/profile/#{github_user.login}"

expect(last_response.body).to include('No such user')
end
end
end

describe "GET /unauthenticated" do
it "should not redirect for authentication" do
get '/unauthenticated'
Expand Down