summaryrefslogtreecommitdiff
path: root/lib/dashing.rb
diff options
context:
space:
mode:
authorDavid Underwood <david.underwood@jadedpixel.com>2013-07-29 11:56:43 -0400
committerDavid Underwood <david.underwood@jadedpixel.com>2013-07-29 11:56:43 -0400
commitff467e28a14163f6257c260fdd570c0ccd95228c (patch)
tree44ec670aa78aa0c1b5f998d947662e5ed0251702 /lib/dashing.rb
parentc2fad09e3b3845f80fb6e60f7ceb7e64c60dd633 (diff)
parent60dcd73bac913afe0a49b6c6c07b709aa288fd7d (diff)
Merge remote-tracking branch 'cjolly/cjolly-application-messages'
* cjolly/cjolly-application-messages: only fire dashboard events when target is current dashboard dashboard events - use SSE event names Adding application messaging. Conflicts: test/app_test.rb
Diffstat (limited to 'lib/dashing.rb')
-rw-r--r--lib/dashing.rb28
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/dashing.rb b/lib/dashing.rb
index dc62395..935a031 100644
--- a/lib/dashing.rb
+++ b/lib/dashing.rb
@@ -81,9 +81,23 @@ get '/views/:widget?.html' do
end
end
+post '/dashboards/:id' do
+ request.body.rewind
+ body = JSON.parse(request.body.read)
+ body['dashboard'] ||= params['id']
+ auth_token = body.delete("auth_token")
+ if !settings.auth_token || settings.auth_token == auth_token
+ send_event(params['id'], body, 'dashboards')
+ 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)
@@ -106,16 +120,18 @@ def production?
ENV['RACK_ENV'] == 'production'
end
-def send_event(id, body)
+def send_event(id, body, target=nil)
body[:id] = id
body[:updatedAt] ||= Time.now.to_i
- event = format_event(body.to_json)
- Sinatra::Application.settings.history[id] = event
+ event = format_event(body.to_json, target)
+ Sinatra::Application.settings.history[id] = event unless target == 'dashboards'
Sinatra::Application.settings.connections.each { |out| out << event }
end
-def format_event(body)
- "data: #{body}\n\n"
+def format_event(body, name=nil)
+ str = ""
+ str << "event: #{name}\n" if name
+ str << "data: #{body}\n\n"
end
def latest_events