This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.rb
75 lines (62 loc) · 1.67 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'logger'
require 'sinatra'
require 'sinatra_auth_github'
require 'sinatra/activerecord'
require './models/code'
require './models/user'
require './models/github_auth'
module PpwmMatcher
class App < Sinatra::Base
enable :sessions
set :github_options, PpwmMatcher::GithubAuth.options
LOGGER = Logger.new(STDOUT)
register Sinatra::Auth::Github
helpers do
def repos
github_request("user/repos")
end
end
get '/' do
authenticate!
message = params['error'] ? "<strong>Unknown code, try again</strong>" : ""
<<-PAIR
<h1>Hello there, #{github_user.login}!</h1>
#{message}
<form action='/code' method='POST'>
<p>
Enter your code:
<input type='text' name='code' value=''>
</p>
<p>
Email:
<input type='text' name='email' value='#{github_user.email}'>
</p>
<input type='submit'>
</form>
PAIR
end
get '/code/create' do
codes = Code.create_pair
codes.map { |c| [c.id, c.value] }.inspect
end
post '/code' do
authenticate!
# Store the user, check code
user = User.find_or_create_by_email(params['email'])
code = Code.find_by_value(params['code'])
# Unknown code? Try again
redirect '/?error=1' unless code
LOGGER.info "Matched #{user.email} to #{code.value}"
user.update_with_code(code)
if code.pair_claimed?
message = "Your pair is #{code.paired_user.email}! Click here to send an email and set up a pairing session! Don't be shy!"
else
message = "Your pair hasn't signed in yet, keep your fingers crossed!"
end
<<-PAIR
You submitted code #{code.value}<br>
#{message}
PAIR
end
end
end