/
quickbase-inbox.rb
172 lines (157 loc) · 5.57 KB
/
quickbase-inbox.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
require 'sinatra'
require 'rack/ssl-enforcer'
require 'haml'
require 'yaml'
require 'quickbase_client'
require 'gmail_sender'
CONFIG = "config.yml"
APP = "QuickBase Email Inbox"
TABLE = "Emails"
FROM = "From"
TO = "To"
SUBJECT = "Subject"
MESSAGE = "Message"
configure do
config = {}
config = YAML.load_file(CONFIG)
config.each{|k,v| config[k] = ENV[k.to_s] if ENV[k.to_s]}
config.each{|k,v| set k.to_sym, v }
if settings.environment == :production
use Rack::SslEnforcer
end
end
not_found do
redirect '/not_found.html'
end
get '/' do
redirect '/home.html'
end
post '/:quickbase_username/:quickbase_password/:quickbase_realm/:dbid/:apptoken/:from_fid/:to_fid/:subject_fid/:message_fid' do
begin
quickbase_username = param?(params,:quickbase_username) || settings.quickbase_username
quickbase_password = param?(params,:quickbase_password) || settings.quickbase_password
quickbase_realm = param?(params,:quickbase_realm) || settings.quickbase_realm
if quickbase_username and quickbase_password
qbc = get_qbc(quickbase_username,quickbase_password,quickbase_realm)
if qbc
inbox_table = nil
dbid = param?(params,:dbid) || settings.dbid
if dbid
inbox_table = get_inbox_table(qbc,dbid,params)
else
app_dbid = qbc.findDBByName(APP)
if qbc.requestSucceeded and app_dbid
dbid = qbc.lookupChdbid(TABLE,app_dbid)
if dbid
inbox_table = get_inbox_table(qbc,dbid,params)
else
puts "Unable to find #{TABLE} table in #{APP} application."
end
else
inbox_table = create_inbox_table(qbc)
end
end
if inbox_table
qbc.addFieldValuePair(nil,inbox_table[:from_fid],nil,params["from"]) if inbox_table[:from_fid] and params["from"]
qbc.addFieldValuePair(nil,inbox_table[:to_fid],nil,params["to"]) if inbox_table[:to_fid] and params["to"]
qbc.addFieldValuePair(nil,inbox_table[:subject_fid],nil,params["subject"]) if inbox_table[:subject_fid] and params["subject"]
qbc.addFieldValuePair(nil,inbox_table[:message_fid],nil,params["plain"]) if inbox_table[:message_fid] and params["plain"]
qbc.addRecord(inbox_table[:dbid],qbc.fvlist)
else
puts "Unable to find or create Inbox table."
end
end
end
rescue StandardError => error
puts "Error processing email message: #{error}"
end
end
post '/request_info' do
if params[:info_request_email].length > 0 and params[:info_request_message].length > 0
if email_info_request(params[:info_request_email],params[:info_request_message],request.host)
@request_info_text = "Your request for information has been submitted."
else
@request_info_text = "Oops! Something went wrong while your message was being submitted - sorry."
end
else
@request_info_text = "Sorry - please enter your email address a message."
end
haml :request_info
end
private
def create_inbox_table(qbc)
inbox_table = nil
dummy, app_dbid = qbc.createDatabase(APP,APP)
if qbc.requestSucceeded and app_dbid
dbid = qbc.createTable(TABLE,TABLE,app_dbid)
if qbc.requestSucceeded and dbid
inbox_table = {}
inbox_table[:dbid] = dbid
inbox_table[:from_fid], dummy = qbc.addField(dbid,FROM,"email")
inbox_table[:to_fid], dummy = qbc.addField(dbid,TO,"text") if qbc.requestSucceeded
inbox_table[:subject_fid], dummy = qbc.addField(dbid,SUBJECT,"text") if qbc.requestSucceeded
inbox_table[:message_fid], dummy = qbc.addField(dbid,MESSAGE,"text") if qbc.requestSucceeded
if qbc.requestSucceeded
qbc.setFieldProperties(dbid,{"num_lines" => "10"},inbox_table[:message_fid])
else
puts "Error creating field: #{qbc.lastError}"
end
else
puts "Error creating #{TABLE} table: #{qbc.lastError}"
end
else
puts "Error creating #{APP} application: #{qbc.lastError}"
end
inbox_table
end
def get_inbox_table(qbc,dbid,params)
inbox_table = nil
qbc.getSchema(dbid)
if qbc.requestSucceeded
inbox_table = {}
inbox_table[:dbid] = dbid
inbox_table[:from_fid] = param?(params,:from_fid) || qbc.lookupFieldIDByName(FROM,dbid)
inbox_table[:to_fid] = param?(params,:to_fid) || qbc.lookupFieldIDByName(TO,dbid)
inbox_table[:subject_fid] = param?(params,:subject_fid) || qbc.lookupFieldIDByName(SUBJECT,dbid)
inbox_table[:message_fid] = param?(params,:message_fid) || qbc.lookupFieldIDByName(MESSAGE,dbid)
else
puts "Error retrieving QuickBase schema for Inbox table #{dbid}: #{qbc.lastError}"
end
inbox_table
end
def param?(params,sym)
params[sym] = nil if params[sym] and params[sym] == "-"
params[sym]
end
def get_qbc(quickbase_username,quickbase_password,quickbase_realm)
options = {"username" => quickbase_username, "password" => quickbase_password, "org" => quickbase_realm}
qbc = QuickBase::Client.init(options)
qbc.cacheSchemas=true
qbc
end
def email_info_request(from,body,host)
ret = true
begin
gms = GmailSender.new(settings.gmail_username, settings.gmail_password)
gms.send({ :to => settings.gmail_username, :subject => "Request for info from #{from} on #{host}", :content => body})
rescue StandardError => error
puts "Error sending email: #{error}"
ret = false
end
ret
end
__END__
@@ request_info
%html<
%head<
%title
QuickBase Inbox - Request for Information
%link{ :rel => "stylesheet", :href => "/site.css", :type => "text/css"}
%body<
%h2
%a{ :href => "/home.html" }
QuickBase Inbox
%center
%h3
#{@request_info_text}
%hr