This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
63 lines (54 loc) · 1.34 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
require "sinatra"
require "tilt/haml"
require "faye/websocket"
require "rethinkdb"
require "opal"
require "json"
include RethinkDB::Shortcuts
DBHOST = ENV["RETHINKDB_PORT_28015_TCP_ADDR"]
DBPORT = ENV["RETHINKDB_PORT_28015_TCP_PORT"]
def query rql
conn = r.connect host: DBHOST, port: DBPORT
rql.run(conn).to_json
ensure
conn.close
end
class App < Sinatra::Base
def initialize
super
@clients = []
EM.next_tick do
conn = r.connect host: DBHOST, port: DBPORT
r.table("todo").changes.em_run(conn) do |err, change|
@clients.each {|c| c.send change.to_json }
end
end
end
def setup_websocket ws
ws.on(:close) { @clients.delete ws }
ws.on(:open) { @clients << ws }
ws.on :message do |msg|
data = JSON.parse msg.data
case data["command"]
when "add"
query r.table("todo").insert text: data["text"], status: false
when "update"
query r.table("todo").get(data["id"]).update status: data["status"]
when "delete"
query r.table("todo").get(data["id"]).delete()
end
end
end
get "/" do
if Faye::WebSocket.websocket? request.env
ws = Faye::WebSocket.new request.env
setup_websocket ws
ws.rack_response
else
haml :index
end
end
get "/api/items" do
query r.table("todo").coerce_to("array")
end
end