-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.rb
executable file
·101 lines (88 loc) · 2.21 KB
/
server.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
require 'sinatra'
require 'json'
require 'active_record'
require 'sqlite3'
require 'rufus-scheduler'
require 'sms-easy'
require 'mail'
class Banana < ActiveRecord::Base
end
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'dbfile.sqlite3')
if ARGV[0] == 'reset'
if ActiveRecord::Base.connection.table_exists? 'bananas'
ActiveRecord::Migration.class_eval do
drop_table :bananas do |t|
end
end
end
ActiveRecord::Migration.class_eval do
create_table :bananas do |t|
t.string :phone
t.string :carrier
t.datetime :notifyDate
end
end
end
configure do
set :public_folder, '.'
end
after { ActiveRecord::Base.connection.close }
get "/" do
send_file File.join(settings.public_folder, 'index.html')
end
post '/bananas' do
msg = ""
begin
request.body.rewind
o = JSON.parse(request.body.read)
b = Banana.new
if o['notifyDate']
b.notifyDate = o['notifyDate'].to_datetime
else
msg = "notifyDate not specified"
throw msg
end
if o['phone']
b.phone = o['phone'].to_s
else
msg = "phone not specified"
throw msg
end
if o['carrier']
b.carrier = o['carrier'].to_s
else
msg = "carrier not specified"
throw msg
end
b.save!
r = b.to_json
rescue Exception => e
r = "{\"error\": #{msg}}"
raise e
end
r
end
def check_for_alerts
puts "== checking times in db #{Time.now}"
SMSEasy::Client.config['from_address'] = "[email protected]"
sms = SMSEasy::Client.new
bananas = Banana.all
bananas.each do |b|
puts "#{b.inspect} == #{Time.now}"
if b.notifyDate.past?
puts "sending text to #{b.phone} on carrier #{b.carrier}"
Mail.deliver do
from '[email protected]'
to SMSEasy::Client.sms_address(b.phone, b.carrier)
body "BANANALERT! Your banana is ready to be eaten based on your personal preferences!"
end
b.destroy!
end
end
ActiveRecord::Base.connection.close
end
check_for_alerts
scheduler = Rufus::Scheduler.new
scheduler.every '3' do
check_for_alerts
end