-
Notifications
You must be signed in to change notification settings - Fork 2
/
picklespears.rb
executable file
·185 lines (148 loc) · 4.31 KB
/
picklespears.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/local/ruby/bin/ruby
# frozen_string_literal: true
require 'active_support/all'
require 'bcrypt'
require 'bundler/setup'
require 'cgi'
require 'digest'
require 'icalendar'
require 'pony'
require 'prawn'
require 'rack-flash'
require 'sass'
require 'sinatra'
require 'sinatra/config_file'
require 'slim'
require 'time'
# Slim::Engine.options[:pretty] = true
config_file 'config/config.yml'
APP_DOMAIN = 'www.teamvite.com'
class PickleSpears < Sinatra::Application
enable :sessions
use Rack::Flash, accessorize: %i[errors messages]
DATE_FORMAT = '%a %b %e %I:%M %p'
# this should be enabled by default in Sinatra::Application subclasses
# http://sinatrarb.com/intro.html - see Logging
configure :production, :development do
enable :logging
end
configure :production do
set :clean_trace, true
error 400 do
'Invalid Request'
end
error do
send_email to: 'ben.prew@gmail.com', subject: 'error on teamvite.com',
body: "#{request.env['sinatra.error'].message} #{request.inspect}"
'Application error.'
end
end
not_found do
'This page was not found'
end
error 403 do
'Access forbidden'
end
get '/' do
@teams = []
slim :index, layout: false
end
get '/browse' do
league_id = params[:league_id].to_i
@divisions = Division.filter(league_id: league_id).order(Sequel.asc(:name)).all
@league = League[league_id]
unless @league
flash[:errors] = 'No league with that name was found'
redirect '/team/search'
end
slim :browse
end
get '/stylesheet.css' do
response['Content-Type'] = 'text/css'
sass :stylesheet
end
post '/players_team/delete' do
team = Team[params[:team_id]]
team.remove_player(@user)
flash[:messages] = "You have successfully left #{team.name}"
redirect uri_for(@user)
end
get '/send_game_reminders' do
output = ''
Team.all.each do |team|
next_game = team.next_game()
output += "\n<br/> working on team #{team.name} ..."
if !next_game || next_game.date > (Date.today + 5).to_time
output += 'no upcoming unreminded games'
next
end
output += "sending email about #{next_game.description}"
team.players.each do |player|
next unless player&.email_address&.match(/@/)
pg = PlayersGame.find_or_create(player_id: player.id, game_id: next_game.id)
next if pg.reminder_sent
send_email(
to: player.email_address,
subject: "Next Game: #{next_game.date.strftime(DATE_FORMAT)} #{next_game.description} ",
body: partial(:reminder, locals: { player: player, game: next_game }),
content_type: 'text/html'
)
pg.reminder_sent = true
pg.save
end
end
slim output
end
get '/game_reminder_email' do
pg = PlayersGame.find_or_create player_id: params[:player_id], game_id: params[:game_id]
partial(:reminder, locals: { player: pg.player, game: pg.game })
end
end
helpers do
def title(title = nil)
@title ||= ''
@title = title unless title.nil?
@title
end
def uri_for(item, method = 'index', args: {})
id = item.id
route = item.class.name.downcase
# id or method could be null, so we compact then join
"/#{[route, id, method].compact.join('/')}#{mk_params(args)}"
end
def mk_params(args)
return '' if args.empty?
%(?#{(args.map { |key, val| "#{key}=#{CGI.escape(val.to_s)}" }).join('&')})
end
def url_for(url, args = {})
"#{url}#{mk_params(args)}"
end
def send_email(options)
message = {
from: 'team@teamvite.com',
via: :smtp,
via_options: {
address: ENV['MAILGUN_SMTP_SERVER'],
port: ENV['MAILGUN_SMTP_PORT'],
user_name: ENV['MAILGUN_SMTP_LOGIN'],
password: ENV['MAILGUN_SMTP_PASSWORD'],
domain: 'teamvite.com',
enable_starttls_auto: true
}
}.merge(options)
if settings.environment == :production
Pony.mail(message)
elsif settings.environment != :test
p message
end
end
def partial(page, variables = {})
if File.exist?("#{settings.views}/#{page}.slim")
slim page.to_sym, { layout: false }.merge(variables)
else
slim :"partials/#{page}", { layout: false }.merge(variables)
end
end
end
require_relative 'routes/init'
require_relative 'models/init'