-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
61 lines (51 loc) · 1.16 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
require 'sinatra'
require 'sequel'
require 'mysql2'
require 'htmlentities'
require 'gon-sinatra'
require 'json'
require 'pusher'
require 'dotenv/load'
require 'pg'
Sinatra::register Gon::Sinatra
DB = Sequel.connect(ENV['DB'])
class Status < Sequel::Model(:status)
end
Status.set_allowed_columns :status
pusher_client = Pusher::Client.new(
app_id: ENV['PUSHER_ID'],
key: ENV['PUSHER_KEY'],
secret: ENV['PUSHER_SECRET'],
cluster: ENV['PUSHER_CLUSTER'],
encrypted: true
)
def get_request_body(request)
data = JSON.parse request.body.read
return data
end
def get_model_values(model)
data = []
model.each{|m| data << m.values}
return data
end
get '/' do
gon.status = get_model_values Status.reverse_order(:id)
@status = Status.all
erb :index
end
post '/' do
status = get_request_body(request)['data']['status']
Status.create(:status => status)
pusher_client.trigger('status','status-update',{
status: get_model_values(Status.reverse_order(:id))
})
"Ok"
end
delete '/' do
status_id = get_request_body(request)['id']
Status[status_id].delete
pusher_client.trigger('status','status-update',{
status: get_model_values(Status.reverse_order(:id))
})
"ok"
end