summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPieter van de Bruggen <pvande@gmail.com>2012-11-04 17:10:36 -0800
committerChad Jolly <cjolly@lawgical.com>2013-07-14 15:13:05 -0600
commit3f58bbd626cd84d1cb24375b4d8fe6df75ce85ba (patch)
treea8f2592a685c145508b91239ef2b781dc7f21a8c /lib
parent326c5b9c9956fd832b850d60982a677660680a7c (diff)
Adding application messaging.
While widgets are the most common target for events, there is good reason to sometimes target the application as a whole. This patch adds support for such events using the /-prefixed notation. (This convention is safe for use since it represents an extremely uncommon widget ID, and one which data could not be POSTed to.) Such events are NOT subject to replay, and are handled on the client-side by firing corresponding events on the Dashing application class. As a proof of concept (and to fufill a feature request), this patch also introduces a POST /reload endpoint, which when provided with a valid authkey (and optionally a dashboard name) fires the appropriate 'reload' event on all of the loaded dashboards. The 'reload' event handler then reloads the dashboard, unless a different dashboard was specified.
Diffstat (limited to 'lib')
-rw-r--r--lib/dashing.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/dashing.rb b/lib/dashing.rb
index dc62395..4c1b44b 100644
--- a/lib/dashing.rb
+++ b/lib/dashing.rb
@@ -81,9 +81,22 @@ get '/views/:widget?.html' do
end
end
+post '/reload' do
+ request.body.rewind
+ body = JSON.parse(request.body.read)
+ auth_token = body.delete("auth_token")
+ if !settings.auth_token || settings.auth_token == auth_token
+ send_event('/reload', body)
+ 204 # response without entity body
+ else
+ status 401
+ "Invalid API key\n"
+ end
+end
+
post '/widgets/:id' do
request.body.rewind
- body = JSON.parse(request.body.read)
+ body = JSON.parse(request.body.read)
auth_token = body.delete("auth_token")
if !settings.auth_token || settings.auth_token == auth_token
send_event(params['id'], body)
@@ -110,7 +123,7 @@ def send_event(id, body)
body[:id] = id
body[:updatedAt] ||= Time.now.to_i
event = format_event(body.to_json)
- Sinatra::Application.settings.history[id] = event
+ Sinatra::Application.settings.history[id] = event unless id =~ /^\//
Sinatra::Application.settings.connections.each { |out| out << event }
end