lib/dashing.rb in dashing-1.1.0 vs lib/dashing.rb in dashing-1.2.0

- old
+ new

@@ -79,13 +79,27 @@ file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}") return engines.first.new(file).render if File.exist? file 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) 204 # response without entity body else @@ -104,20 +118,22 @@ 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 settings.history.inject("") do |str, (id, body)| str << body @@ -139,7 +155,7 @@ Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file } {}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start. job_path = ENV["JOB_PATH"] || 'jobs' -files = Dir[File.join(settings.root, job_path, '/*.rb')] +files = Dir[File.join(settings.root, job_path, '**', '/*.rb')] files.each { |job| require(job) }