summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--javascripts/dashing.coffee11
-rw-r--r--lib/dashing.rb28
-rw-r--r--test/app_test.rb14
3 files changed, 46 insertions, 7 deletions
diff --git a/javascripts/dashing.coffee b/javascripts/dashing.coffee
index ebf5c0a..5712e98 100644
--- a/javascripts/dashing.coffee
+++ b/javascripts/dashing.coffee
@@ -25,6 +25,9 @@ Batman.Filters.shortenedNumber = (num) ->
num
class window.Dashing extends Batman.App
+ @on 'reload', (data) ->
+ window.location.reload(true)
+
@root ->
Dashing.params = Batman.URI.paramsFromQuery(window.location.search.slice(1));
@@ -98,7 +101,7 @@ source.addEventListener 'error', (e)->
if (e.readyState == EventSource.CLOSED)
console.log("Connection closed")
-source.addEventListener 'message', (e) =>
+source.addEventListener 'message', (e) ->
data = JSON.parse(e.data)
if lastEvents[data.id]?.updatedAt != data.updatedAt
if Dashing.debugMode
@@ -108,6 +111,12 @@ source.addEventListener 'message', (e) =>
for widget in widgets[data.id]
widget.receiveData(data)
+source.addEventListener 'dashboards', (e) ->
+ data = JSON.parse(e.data)
+ if Dashing.debugMode
+ console.log("Received data for dashboards", data)
+ if data.dashboard is '*' or window.location.pathname is "/#{data.dashboard}"
+ Dashing.fire data.event, data
$(document).ready ->
Dashing.run()
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
diff --git a/test/app_test.rb b/test/app_test.rb
index 0546814..0032165 100644
--- a/test/app_test.rb
+++ b/test/app_test.rb
@@ -42,6 +42,16 @@ class AppTest < Dashing::Test
assert_equal 8, parse_data(@connection[0])['value']
end
+ def test_dashboard_events
+ post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'})
+ assert_equal 204, last_response.status
+
+ get '/events'
+ assert_equal 200, last_response.status
+ assert_equal 'dashboards', parse_event(@connection[0])
+ assert_equal 'reload', parse_data(@connection[0])['event']
+ end
+
def test_redirect_to_default_dashboard
with_generated_project do
Sinatra::Application.settings.default_dashboard = 'test1'
@@ -142,4 +152,8 @@ class AppTest < Dashing::Test
def parse_data(string)
JSON.parse string[/data: (.+)/, 1]
end
+
+ def parse_event(string)
+ string[/event: (.+)/, 1]
+ end
end