-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
157 lines (128 loc) · 3.09 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
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
require "sinatra"
require 'sinatra/flash'
require_relative "authentication.rb"
require "combine_pdf"
require 'stripe'
set :publishable_key, ENV['PUBLISHABLE_KEY']
set :secret_key, ENV['SECRET_KEY']
Stripe.api_key = settings.secret_key
#the following urls are included in authentication.rb
# GET /login
# GET /logout
# GET /sign_up
# authenticate! will make sure that the user is signed in, if they are not they will be redirected to the login page
# if the user is signed in, current_user will refer to the signed in user object.
# if they are not signed in, current_user will be nil
def reached_limit?
if session[:times_used].nil?
return false
else
return session[:times_used] > 3
end
end
def signed_in?
return !current_user.nil?
end
def impose_limit!
if reached_limit? && signed_in? && !current_user.pro?
flash[:error] = "Error: You have reached the free limit. Please <a href='/upgrade'>upgrade</a> to continue."
redirect "/"
end
if reached_limit? && !signed_in?
flash[:error] = "Error: You have reached the free limit. Please <a href='/upgrade'>upgrade</a> to continue."
redirect "/"
end
end
def load_file1
if !params[:file1].nil?
unloaded_file = params[:file1][:tempfile]
loaded_file = unloaded_file.read
return CombinePDF.parse(loaded_file)
else
return nil
end
end
def load_file2
if !params[:file2].nil?
unloaded_file = params[:file2][:tempfile]
loaded_file = unloaded_file.read
return CombinePDF.parse(loaded_file)
else
return nil
end
end
def load_file3
if !params[:file3].nil?
unloaded_file = params[:file3][:tempfile]
loaded_file = unloaded_file.read
return CombinePDF.parse(loaded_file)
else
return nil
end
end
def load_file4
if !params[:file4].nil?
unloaded_file = params[:file4][:tempfile]
loaded_file = unloaded_file.read
return CombinePDF.parse(loaded_file)
else
return nil
end
end
def increase_times_used
if session[:times_used].nil?
session[:times_used] = 1
else
session[:times_used] += 1
end
end
get "/" do
erb :index
end
post "/merge" do
impose_limit!
merged = CombinePDF.new
cpdf1 = load_file1
cpdf2 = load_file2
cpdf3 = load_file3
cpdf4 = load_file4
merged << cpdf1 if !cpdf1.nil?
merged << cpdf2 if !cpdf2.nil?
merged << cpdf3 if !cpdf3.nil?
merged << cpdf4 if !cpdf4.nil?
increase_times_used
status 200
headers 'content-type' => "application/pdf"
body merged.to_pdf
end
get "/upgrade" do
authenticate!
if current_user.pro? || current_user.administrator?
flash[:error] = "Error: You are not eligible to upgrade."
redirect "/"
end
erb :pay
end
post "/charge" do
begin
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => '[email protected]',
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:amount => @amount,
:description => 'Sinatra Charge',
:currency => 'usd',
:customer => customer.id
)
current_user.role_id = 2
current_user.save
flash[:success] = "Success: You have upgraded to PRO."
redirect "/"
rescue Stripe::CardError
flash[:error] = "Error: Please try a new card."
redirect "/"
end
end