-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticketmaster.rb
97 lines (84 loc) · 2.47 KB
/
ticketmaster.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
require "sinatra/base"
require "yaml"
require "./helpers/unfuddle_api.rb"
require "./helpers/utest_aids.rb"
# ticketmaster requires the following to be set, either as environment
# variables or in ./config/credentials.yaml
# TM_USER
# TM_PASS - credetionals to access ticketmaster
# UNFUDDLE_USER
# UNFUDDLE_PASS - credentials to access unfuddle.
# UNFUDDLE_PROJECT_ID - unfuddle project ID, e.g. 313388
# UNFUDDLE_MILESTONE_ID - ID of milestone to associate with this project, e.g. 307163
class String
def safe
Rack::Utils.escape_html self
end
end
class Ticketmaster < Sinatra::Base
class << self
attr_accessor :config
end
use Rack::Session::Pool
self.config = YAML::load(File.open("./config/credentials.yaml")) rescue {}
use Rack::Auth::Basic, "Restricted Area" do |user, pass|
[user, pass] == [
Ticketmaster.config["TM_USER"] || ENV["TM_USER"],
Ticketmaster.config["TM_PASS"] || ENV["TM_PASS"]
]
end
get "/" do
erb :index
end
post "/upyougo" do
begin
session[:tickets] = UtestAids::ParseCsv.fromfile params[:tickets][:tempfile]
session[:notice] = "#{session[:tickets].count} ticket(s) uploaded."
redirect "/verify"
rescue
session[:notice] = "Unrecognized file format."
redirect "/"
end
end
get "/verify" do
@tickets = session[:tickets] || []
redirect "/" if @tickets.empty?
erb :verify
end
post "/awayyougo" do
tickets = session[:tickets]
if tickets.empty?
session[:notice] = "No tickets."
redirect "/"
end
submitted = 0
failed = []
errors = []
fu = UnfuddleApi::Futicket.new(
Ticketmaster.config["UNFUDDLE_USER"] || ENV["UNFUDDLE_USER"],
Ticketmaster.config["UNFUDDLE_PASS"] || ENV["UNFUDDLE_PASS"],
Ticketmaster.config["UNFUDDLE_PROJECT_ID"] || ENV["UNFUDDLE_PROJECT_ID"],
Ticketmaster.config["UNFUDDLE_MILESTONE_ID"] || ENV["UNFUDDLE_MILESTONE_ID"]
)
begin
tickets.each do |t|
ok, message = fu.submit(t[:title], t[:description])
if ok
submitted += 1
else
failed << t
errors << message
end
end
rescue UnfuddleApi::Authentication
failed += tickets[(submitted+failed.count)..-1]
errors << "Authentication error."
end
errors.uniq!
session[:notice] = "#{submitted} ticket(s) submitted."
redirect "/" if failed.empty?
session[:errors] = errors.join(", ")
session[:tickets] = failed
redirect "/verify"
end
end