From 7877ba23d49bd9b6cd527d418a095d47746fa2b7 Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Mon, 19 Aug 2013 14:24:45 -0700 Subject: [PATCH 01/12] Better email formatting for events --- app/mailers/system_mailer.rb | 2 +- app/models/agents/digest_email_agent.rb | 22 +++++++++---------- app/views/system_mailer/send_message.html.erb | 13 +++++++---- app/views/system_mailer/send_message.text.erb | 6 +++-- spec/models/agents/digest_email_agent_spec.rb | 7 ++++-- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/app/mailers/system_mailer.rb b/app/mailers/system_mailer.rb index c010ad9a..0aa1fb51 100644 --- a/app/mailers/system_mailer.rb +++ b/app/mailers/system_mailer.rb @@ -2,7 +2,7 @@ class SystemMailer < ActionMailer::Base default :from => ENV['EMAIL_FROM_ADDRESS'] || 'you@example.com' def send_message(options) - @lines = options[:lines] + @groups = options[:groups] @headline = options[:headline] mail :to => options[:to], :subject => options[:subject] end diff --git a/app/models/agents/digest_email_agent.rb b/app/models/agents/digest_email_agent.rb index 24ab60a9..59a3b2e8 100644 --- a/app/models/agents/digest_email_agent.rb +++ b/app/models/agents/digest_email_agent.rb @@ -37,30 +37,28 @@ module Agents def check if self.memory[:queue] && self.memory[:queue].length > 0 - lines = self.memory[:queue].map {|item| present(item) } + groups = self.memory[:queue].map { |payload| present(payload) } puts "Sending mail to #{user.email}..." unless Rails.env.test? - SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :lines => lines) + SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :groups => groups) self.memory[:queue] = [] end end - def present(item) - if item.is_a?(Hash) + def present(payload) + if payload.is_a?(Hash) + payload = ActiveSupport::HashWithIndifferentAccess.new(payload) MAIN_KEYS.each do |key| - if item.has_key?(key) - return "#{item[key]}" + ((item.length > 1 && item.length < 5) ? " (#{present_hash item, key})" : "") - elsif item.has_key?(key.to_s) - return "#{item[key.to_s]}" + ((item.length > 1 && item.length < 5) ? " (#{present_hash item, key.to_s})" : "") - end + return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key) end - present_hash item + + { :title => "Event", :entries => present_hash(payload) } else - item.to_s + { :title => payload.to_s, :entries => [] } end end def present_hash(hash, skip_key = nil) - hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless [skip_key].include?(k) }.compact.to_sentence + hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact end end end \ No newline at end of file diff --git a/app/views/system_mailer/send_message.html.erb b/app/views/system_mailer/send_message.html.erb index 4cb2e4e7..d18a61f1 100644 --- a/app/views/system_mailer/send_message.html.erb +++ b/app/views/system_mailer/send_message.html.erb @@ -7,10 +7,15 @@ <% if @headline %>

<%= @headline %>

<% end %> - <% @lines.each do |line| %> -

- <%= line %> -

+ <% @groups.each do |group| %> +
+
<%= group[:title] %>
+ <% group[:entries].each do |entry| %> +
+ <%= group[:entry] %> +
+ <% end %> +
<% end %> \ No newline at end of file diff --git a/app/views/system_mailer/send_message.text.erb b/app/views/system_mailer/send_message.text.erb index b6bfe174..a38b1dc0 100644 --- a/app/views/system_mailer/send_message.text.erb +++ b/app/views/system_mailer/send_message.text.erb @@ -1,5 +1,7 @@ <% if @headline %><%= @headline %> -<% end %><% @lines.each do |line| %><%= line %> - +<% end %><% @groups.each do |group| %><%= group[:title] %> +<% group[:entries].each do |entry| %> <%= entry %> <% end %> + +<% end %> \ No newline at end of file diff --git a/spec/models/agents/digest_email_agent_spec.rb b/spec/models/agents/digest_email_agent_spec.rb index 94a0a57b..df249b32 100644 --- a/spec/models/agents/digest_email_agent_spec.rb +++ b/spec/models/agents/digest_email_agent_spec.rb @@ -33,13 +33,16 @@ describe Agents::DigestEmailAgent do Agents::DigestEmailAgent.async_check(@checker.id) ActionMailer::Base.deliveries.should == [] - @checker.memory[:queue] = ["Something you should know about", { :title => "Foo", :url => "http://google.com", :bar => 2 }, { "message" => "hi", :woah => "there" }] + @checker.memory[:queue] = ["Something you should know about", + { :title => "Foo", :url => "http://google.com", :bar => 2 }, + { "message" => "hi", :woah => "there" }, + { "test" => 2 }] @checker.save! Agents::DigestEmailAgent.async_check(@checker.id) ActionMailer::Base.deliveries.last.to.should == ["bob@example.com"] ActionMailer::Base.deliveries.last.subject.should == "something interesting" - get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Something you should know about\n\nFoo (bar: 2 and url: http://google.com)\n\nhi (woah: there)" + get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Something you should know about\n\nFoo\n bar: 2\n url: http://google.com\n\nhi\n woah: there\n\nEvent\n test: 2" @checker.reload.memory[:queue].should == [] end end From 6a82f27034ac64f47e9069e54b8c3024874aa64a Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Sun, 1 Sep 2013 15:50:42 -0600 Subject: [PATCH 02/12] Add quick navigation box for Agents --- .../javascripts/application.js.coffee.erb | 29 +++++++++++++++++-- .../stylesheets/application.css.scss.erb | 21 +++++++++----- app/views/agents/edit.html.erb | 2 +- app/views/agents/new.html.erb | 2 +- app/views/layouts/_navigation.html.erb | 8 +++++ app/views/layouts/application.html.erb | 10 +++++++ lib/utils.rb | 4 +++ 7 files changed, 64 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/application.js.coffee.erb b/app/assets/javascripts/application.js.coffee.erb index ef956ccf..71ea6c7e 100644 --- a/app/assets/javascripts/application.js.coffee.erb +++ b/app/assets/javascripts/application.js.coffee.erb @@ -54,7 +54,30 @@ $(document).ready -> if $(".flash").length setTimeout((-> $(".flash").slideUp(-> $(".flash").remove())), 5000) - # Agent Show + # Agent Navigation + $agentNavigate = $('#agent-navigate') + $agentNavigate.typeahead( + minLength: 0, + items: 15, + source: agentNames + ).on("change", (e) -> + if agentPaths[$agentNavigate.val()] + $('#agent-navigate').closest(".navbar-search").find(".spinner").show(); + window.location = agentPaths[$agentNavigate.val()] + ).on("focus", (e) -> + $agentNavigate.val '' + ).on("blur", (e) -> + $agentNavigate.val '' + ) + + # Pressing '/' selects the search box. + $("body").on "keypress", (e) -> + if e.keyCode == 47 # The '/' key + if e.target.nodeName == "BODY" + e.preventDefault() + $agentNavigate.focus() + +# Agent Show fetchLogs = (e) -> agentId = $(e.target).closest("[data-agent-id]").data("agent-id") e.preventDefault() @@ -85,7 +108,7 @@ $(document).ready -> $("#agent_type").on "change", -> if window.jsonEditor? - $(".spinner").fadeIn(); + $("#agent-spinner").fadeIn(); $("#agent_source_ids").select2("val", {}); $(".event-descriptions").html("").hide() $.getJSON "/agents/type_details", { type: $(@).val() }, (json) => @@ -104,7 +127,7 @@ $(document).ready -> window.jsonEditor.json = json.options window.jsonEditor.rebuild() - $(".spinner").stop(true, true).fadeOut(); + $("#agent-spinner").stop(true, true).fadeOut(); $("#agent_type").change() if $("#agent_type").length diff --git a/app/assets/stylesheets/application.css.scss.erb b/app/assets/stylesheets/application.css.scss.erb index 5c530ae7..29d1acd9 100644 --- a/app/assets/stylesheets/application.css.scss.erb +++ b/app/assets/stylesheets/application.css.scss.erb @@ -51,10 +51,6 @@ table.events { margin-left: 0 !important; } -#job-indicator, #event-indicator { - display: none; -} - img.odin { position: relative; top: -32px; @@ -86,14 +82,25 @@ img.spinner { overflow: hidden; } +// Navbar + +#job-indicator, #event-indicator { + display: none; +} + +.navbar-search > .spinner { + position: absolute; + top: -1px; + right: 1px; +} + // Flash .flash { position: fixed; - width: 500px; + width: 210px; z-index: 99999; - top: 1px; - margin-left: 250px; + right: 20px; .alert { } diff --git a/app/views/agents/edit.html.erb b/app/views/agents/edit.html.erb index ed20571a..e5b883b6 100644 --- a/app/views/agents/edit.html.erb +++ b/app/views/agents/edit.html.erb @@ -4,7 +4,7 @@ diff --git a/app/views/agents/new.html.erb b/app/views/agents/new.html.erb index b0b7a2b3..45c52628 100644 --- a/app/views/agents/new.html.erb +++ b/app/views/agents/new.html.erb @@ -4,7 +4,7 @@ diff --git a/app/views/layouts/_navigation.html.erb b/app/views/layouts/_navigation.html.erb index 697e465f..ef00b9d3 100644 --- a/app/views/layouts/_navigation.html.erb +++ b/app/views/layouts/_navigation.html.erb @@ -9,11 +9,18 @@ + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2ce5fd61..ec0cf9bf 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -32,5 +32,15 @@ + + \ No newline at end of file diff --git a/lib/utils.rb b/lib/utils.rb index 327165ea..2c52008c 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -51,4 +51,8 @@ module Utils result end end + + def self.jsonify(thing) + thing.to_json.gsub(' Date: Sun, 1 Sep 2013 16:24:53 -0600 Subject: [PATCH 03/12] add cannot_create_events! annotation --- app/models/agent.rb | 22 +++++++++++++++++++++- app/models/agents/digest_email_agent.rb | 2 ++ app/models/agents/post_agent.rb | 1 + app/models/agents/twilio_agent.rb | 1 + app/views/agents/_form.html.erb | 3 ++- spec/models/agent_spec.rb | 22 ++++++++++++++++------ 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app/models/agent.rb b/app/models/agent.rb index b4fcb83e..94b32037 100644 --- a/app/models/agent.rb +++ b/app/models/agent.rb @@ -88,7 +88,11 @@ class Agent < ActiveRecord::Base end def create_event(attrs) - events.create!({ :user => user }.merge(attrs)) + if can_create_events? + events.create!({ :user => user }.merge(attrs)) + else + error "This Agent cannot create events!" + end end def validate_schedule @@ -140,6 +144,14 @@ class Agent < ActiveRecord::Base !cannot_receive_events? end + def cannot_create_events? + self.class.cannot_create_events? + end + + def can_create_events? + !cannot_create_events? + end + def set_last_checked_event_id if newest_event_id = Event.order("id desc").limit(1).pluck(:id).first self.last_checked_event_id = newest_event_id @@ -169,6 +181,14 @@ class Agent < ActiveRecord::Base @default_schedule end + def cannot_create_events! + @cannot_create_events = true + end + + def cannot_create_events? + !!@cannot_create_events + end + def cannot_receive_events! @cannot_receive_events = true end diff --git a/app/models/agents/digest_email_agent.rb b/app/models/agents/digest_email_agent.rb index 4f6d2425..9ef6d283 100644 --- a/app/models/agents/digest_email_agent.rb +++ b/app/models/agents/digest_email_agent.rb @@ -3,6 +3,8 @@ module Agents MAIN_KEYS = %w[title message text main value].map(&:to_sym) default_schedule "5am" + cannot_create_events! + description <<-MD The DigestEmailAgent collects any Events sent to it and sends them all via email when run. The email will be sent to your account's address and will have a `subject` and an optional `headline` before diff --git a/app/models/agents/post_agent.rb b/app/models/agents/post_agent.rb index 35c2ccc8..d3582e25 100644 --- a/app/models/agents/post_agent.rb +++ b/app/models/agents/post_agent.rb @@ -1,6 +1,7 @@ module Agents class PostAgent < Agent cannot_be_scheduled! + cannot_create_events! description <<-MD Post Agent receives events from other agents and send those events as the contents of a post request to a specified url. `post_url` field must specify where you would like to receive post requests and do not forget to include URI scheme (`http` or `https`) diff --git a/app/models/agents/twilio_agent.rb b/app/models/agents/twilio_agent.rb index 9ee75f0b..f22ef31b 100644 --- a/app/models/agents/twilio_agent.rb +++ b/app/models/agents/twilio_agent.rb @@ -4,6 +4,7 @@ require 'securerandom' module Agents class TwilioAgent < Agent cannot_be_scheduled! + cannot_create_events! description <<-MD The TwilioAgent receives and collects events and sends them via text message or gives you a call when scheduled. diff --git a/app/views/agents/_form.html.erb b/app/views/agents/_form.html.erb index de128076..af5358e2 100644 --- a/app/views/agents/_form.html.erb +++ b/app/views/agents/_form.html.erb @@ -47,8 +47,9 @@
<%= f.label :sources, :class => 'control-label' %> diff --git a/spec/models/agents/digest_email_agent_spec.rb b/spec/models/agents/digest_email_agent_spec.rb index df249b32..1c118af9 100644 --- a/spec/models/agents/digest_email_agent_spec.rb +++ b/spec/models/agents/digest_email_agent_spec.rb @@ -11,6 +11,10 @@ describe Agents::DigestEmailAgent do @checker.save! end + after do + ActionMailer::Base.deliveries = [] + end + describe "#receive" do it "queues any payloads it receives" do event1 = Event.new @@ -43,7 +47,28 @@ describe Agents::DigestEmailAgent do ActionMailer::Base.deliveries.last.to.should == ["bob@example.com"] ActionMailer::Base.deliveries.last.subject.should == "something interesting" get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Something you should know about\n\nFoo\n bar: 2\n url: http://google.com\n\nhi\n woah: there\n\nEvent\n test: 2" - @checker.reload.memory[:queue].should == [] + @checker.reload.memory[:queue].should be_empty + end + + it "can receive complex events and send them on" do + stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200) + stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true } + @checker.sources << agents(:bob_weather_agent) + + Agent.async_check(agents(:bob_weather_agent).id) + + Agent.receive! + @checker.reload.memory[:queue].should_not be_empty + + Agents::DigestEmailAgent.async_check(@checker.id) + + plain_email_text = get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip + html_email_text = get_message_part(ActionMailer::Base.deliveries.last, /html/).strip + + plain_email_text.should =~ /avehumidity/ + html_email_text.should =~ /avehumidity/ + + @checker.reload.memory[:queue].should be_empty end end end \ No newline at end of file From c2fa67dd9b98bdd6b6bf5eeaa6bc574de1173a1c Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Tue, 3 Sep 2013 20:25:54 -0700 Subject: [PATCH 10/12] Allow the quick navigation menu to run propagation and show the diagram. --- app/assets/javascripts/application.js.coffee.erb | 9 +++++++-- app/views/layouts/application.html.erb | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/application.js.coffee.erb b/app/assets/javascripts/application.js.coffee.erb index bf47551a..311699fe 100644 --- a/app/assets/javascripts/application.js.coffee.erb +++ b/app/assets/javascripts/application.js.coffee.erb @@ -62,8 +62,13 @@ $(document).ready -> source: agentNames ).on("change", (e) -> if agentPaths[$agentNavigate.val()] - $('#agent-navigate').closest(".navbar-search").find(".spinner").show(); - window.location = agentPaths[$agentNavigate.val()] + $('#agent-navigate').closest(".navbar-search").find(".spinner").show() + navigationData = agentPaths[$agentNavigate.val()] + if !(navigationData instanceof Object) || !navigationData.method || navigationData.method == 'GET' + window.location = navigationData.url || navigationData + else + $("").appendTo($("body")).click() + ).on("focus", (e) -> $agentNavigate.val '' ).on("blur", (e) -> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ec0cf9bf..056e1e81 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -39,6 +39,8 @@ agentPaths["New Agent"] = <%= Utils.jsonify new_agent_path %>; agentPaths["Account"] = <%= Utils.jsonify edit_user_registration_path %>; agentPaths["Events Index"] = <%= Utils.jsonify events_path %>; + agentPaths["View Agent Diagram"] = <%= Utils.jsonify diagram_agents_path %>; + agentPaths["Run Event Propagation"] = { url: <%= Utils.jsonify propagate_agents_path %>, method: 'POST' }; var agentNames = []; $.each(agentPaths, function(name, v) { agentNames.push(name); }); From 56cbe8bd689883a9dfe92e7445a16b495be2b86d Mon Sep 17 00:00:00 2001 From: JT Zemp Date: Fri, 6 Sep 2013 10:54:25 -0700 Subject: [PATCH 11/12] current_user.agents fails if current_user == nil I didn't want to mess with organization and style too much, so I didn't do a full-on refactor, but this might want to live as an AJAX call, or if not, maybe a helper. --- app/views/layouts/application.html.erb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 056e1e81..fcfc0d56 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -34,7 +34,11 @@
- \ No newline at end of file + From 8d7dde6fb55feba978a1469d1a3dcbc2ff7c346c Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Fri, 6 Sep 2013 23:46:31 -0700 Subject: [PATCH 12/12] Add a POST endpoint called handle_details_post that Agents' details views can use to send data to the Agent from the UI. Add a new ManualEventAgent that uses the handle_details_post functionality to create simple events on demand for testing / debugging. --- .../javascripts/application.js.coffee.erb | 13 +++--- app/controllers/agents_controller.rb | 10 +++++ app/models/agents/manual_event_agent.rb | 32 ++++++++++++++ .../manual_event_agent/_show.html.erb | 42 +++++++++++++++++++ app/views/agents/show.html.erb | 4 +- app/views/events/index.html.erb | 1 + app/views/events/show.html.erb | 2 +- config/routes.rb | 1 + spec/controllers/agents_controller_spec.rb | 16 +++++++ spec/fixtures/agents.yml | 5 +++ .../models/agents/peak_detector_agent_spec.rb | 6 +-- 11 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 app/models/agents/manual_event_agent.rb create mode 100644 app/views/agents/agent_views/manual_event_agent/_show.html.erb diff --git a/app/assets/javascripts/application.js.coffee.erb b/app/assets/javascripts/application.js.coffee.erb index 311699fe..bc53a520 100644 --- a/app/assets/javascripts/application.js.coffee.erb +++ b/app/assets/javascripts/application.js.coffee.erb @@ -8,13 +8,14 @@ #= require ./worker-checker #= require_self -setupJsonEditor = -> +window.setupJsonEditor = ($editor = $(".live-json-editor")) -> JSONEditor.prototype.ADD_IMG = '<%= image_path 'json-editor/add.png' %>' JSONEditor.prototype.DELETE_IMG = '<%= image_path 'json-editor/delete.png' %>' - if $(".live-json-editor").length - window.jsonEditor = new JSONEditor($(".live-json-editor"), 400, 500) - window.jsonEditor.doTruncation true - window.jsonEditor.showFunctionButtons() + if $editor.length + jsonEditor = new JSONEditor($editor, $editor.data('width') || 400, $editor.data('height') || 500) + jsonEditor.doTruncation true + jsonEditor.showFunctionButtons() + return jsonEditor hideSchedule = -> $(".schedule-region select").hide() @@ -45,7 +46,7 @@ showEventDescriptions = -> $(document).ready -> # JSON Editor - setupJsonEditor() + window.jsonEditor = setupJsonEditor() # Select2 Selects $(".select2").select2(width: 'resolve') diff --git a/app/controllers/agents_controller.rb b/app/controllers/agents_controller.rb index b1b3722d..831e83be 100644 --- a/app/controllers/agents_controller.rb +++ b/app/controllers/agents_controller.rb @@ -8,6 +8,16 @@ class AgentsController < ApplicationController end end + def handle_details_post + @agent = current_user.agents.find(params[:id]) + if @agent.respond_to?(:handle_details_post) + render :json => @agent.handle_details_post(params) || {} + else + @agent.error "#handle_details_post called on an instance of #{@agent.class} that does not define it." + head 500 + end + end + def run agent = current_user.agents.find(params[:id]) Agent.async_check(agent.id) diff --git a/app/models/agents/manual_event_agent.rb b/app/models/agents/manual_event_agent.rb new file mode 100644 index 00000000..7b9f6f14 --- /dev/null +++ b/app/models/agents/manual_event_agent.rb @@ -0,0 +1,32 @@ +module Agents + class ManualEventAgent < Agent + cannot_be_scheduled! + cannot_receive_events! + + description <<-MD + Use this Agent to manually create Events for testing or other purposes. + MD + + event_description "User determined" + + def default_options + { "no options" => "are needed" } + end + + def handle_details_post(params) + if params[:payload] + create_event(:payload => params[:payload]) + { :success => true } + else + { :success => false, :error => "You must provide a JSON payload" } + end + end + + def working? + true + end + + def validate_options + end + end +end \ No newline at end of file diff --git a/app/views/agents/agent_views/manual_event_agent/_show.html.erb b/app/views/agents/agent_views/manual_event_agent/_show.html.erb new file mode 100644 index 00000000..b2aec0e6 --- /dev/null +++ b/app/views/agents/agent_views/manual_event_agent/_show.html.erb @@ -0,0 +1,42 @@ +

Manually Create Events

+ +

+ +<%= form_tag handle_details_post_agent_path(@agent), :id => "create-event-form" do %> +
+ +
+ +
+ <%= submit_tag "Submit", :class => "btn btn-primary" %> +
+<% end %> + + \ No newline at end of file diff --git a/app/views/agents/show.html.erb b/app/views/agents/show.html.erb index 04fa02e2..6c20ed1f 100644 --- a/app/views/agents/show.html.erb +++ b/app/views/agents/show.html.erb @@ -132,12 +132,12 @@

Options: -

<%= JSON.pretty_generate @agent.options %>
+
<%= JSON.pretty_generate @agent.options || {} %>

Memory: -

<%= JSON.pretty_generate @agent.memory %>
+
<%= JSON.pretty_generate @agent.memory || {} %>

diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index a93d62b5..69784af1 100644 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb @@ -16,6 +16,7 @@ <% @events.each do |event| %> + <% next unless event.agent %> <%= link_to event.agent.name, agent_path(event.agent) %> <%= time_ago_in_words event.created_at %> ago diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb index 4b428524..6c15b037 100644 --- a/app/views/events/show.html.erb +++ b/app/views/events/show.html.erb @@ -7,7 +7,7 @@

Payload: -

<%= JSON.pretty_generate @event.payload %>
+
<%= JSON.pretty_generate @event.payload || {} %>

<% if @event.lat && @event.lng %> diff --git a/config/routes.rb b/config/routes.rb index a564c844..7ca90003 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ Huginn::Application.routes.draw do resources :agents do member do post :run + post :handle_details_post delete :remove_events end diff --git a/spec/controllers/agents_controller_spec.rb b/spec/controllers/agents_controller_spec.rb index c41e8054..e36efa6b 100644 --- a/spec/controllers/agents_controller_spec.rb +++ b/spec/controllers/agents_controller_spec.rb @@ -18,6 +18,22 @@ describe AgentsController do end end + describe "POST handle_details_post" do + it "passes control to handle_details_post on the agent" do + sign_in users(:bob) + post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => "bar" } + JSON.parse(response.body).should == { "success" => true } + agents(:bob_manual_event_agent).events.last.payload.should == { :foo => "bar" } + end + + it "can only be accessed by the Agent's owner" do + sign_in users(:jane) + lambda { + post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => :bar } + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + describe "GET show" do it "only shows Agents for the current user" do sign_in users(:bob) diff --git a/spec/fixtures/agents.yml b/spec/fixtures/agents.yml index 2edb44bd..df47d77f 100644 --- a/spec/fixtures/agents.yml +++ b/spec/fixtures/agents.yml @@ -92,3 +92,8 @@ bob_twitter_user_agent: :oauth_token => "---", :oauth_token_secret => "---" }.to_yaml.inspect %> + +bob_manual_event_agent: + type: Agents::ManualEventAgent + user: bob + name: "Bob's event testing agent" diff --git a/spec/models/agents/peak_detector_agent_spec.rb b/spec/models/agents/peak_detector_agent_spec.rb index 3bf69f84..6a68f234 100644 --- a/spec/models/agents/peak_detector_agent_spec.rb +++ b/spec/models/agents/peak_detector_agent_spec.rb @@ -69,10 +69,6 @@ describe Agents::PeakDetectorAgent do :pattern => { :filter => "something" }) @agent.memory[:peaks][:something].length.should == 2 end - - it "works on real world data" do - pending "need examples" - end end describe "validation" do @@ -95,4 +91,4 @@ describe Agents::PeakDetectorAgent do @agent.should_not be_valid end end -end \ No newline at end of file +end