summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-02-09 20:25:22 -0700
committermo khan <mo@mokhan.ca>2015-02-09 20:25:22 -0700
commit34f53efff788b7558f63d973aad1f3475c27c125 (patch)
treef867a5243b5b1ef15ed16952abc3a43f91226ded /app
parent64dd29d70ddda6f4927e9352e3e90c4f87b5041a (diff)
extract message objects to publish.
Diffstat (limited to 'app')
-rw-r--r--app/controllers/agents/events_controller.rb8
-rw-r--r--app/controllers/api/agents/events_controller.rb9
-rw-r--r--app/controllers/api/agents/files_controller.rb7
-rw-r--r--app/controllers/application_controller.rb6
-rw-r--r--app/controllers/dispositions_controller.rb11
-rw-r--r--app/models/event_message.rb25
-rw-r--r--app/models/poke_message.rb23
-rw-r--r--app/services/publisher.rb4
8 files changed, 78 insertions, 15 deletions
diff --git a/app/controllers/agents/events_controller.rb b/app/controllers/agents/events_controller.rb
index 6827938..30ca295 100644
--- a/app/controllers/agents/events_controller.rb
+++ b/app/controllers/agents/events_controller.rb
@@ -11,9 +11,11 @@ module Agents
end
def create
- message = event_params.merge({agent_id: @agent.id})
- routing_key = "events.#{event_params[:type]}.#{@agent.id}"
- Publisher.publish(routing_key, message)
+ publish(EventMessage.new(
+ agent_id: @agent.id,
+ event_type: event_params[:event_type],
+ data: event_params[:data]
+ ))
redirect_to agent_events_url, notice: 'Event was successfully created.'
end
diff --git a/app/controllers/api/agents/events_controller.rb b/app/controllers/api/agents/events_controller.rb
index 255a5bc..ca9b829 100644
--- a/app/controllers/api/agents/events_controller.rb
+++ b/app/controllers/api/agents/events_controller.rb
@@ -3,9 +3,12 @@ module Api
class EventsController < ApplicationController
def create
@agent = Agent.find(params[:agent_id])
- message = event_params.merge({agent_id: @agent.id})
- routing_key = "events.#{event_params[:type]}.#{@agent.id}"
- Publisher.publish(routing_key, message)
+ publish(EventMessage.new(
+ agent_id: @agent.id,
+ event_type: event_params[:event_type],
+ data: event_params[:data]
+ ))
+
render nothing: true
end
diff --git a/app/controllers/api/agents/files_controller.rb b/app/controllers/api/agents/files_controller.rb
index 1329d04..b111048 100644
--- a/app/controllers/api/agents/files_controller.rb
+++ b/app/controllers/api/agents/files_controller.rb
@@ -9,12 +9,11 @@ module Api
@agent = Agent.find(params[:agent_id])
@fingerprint = params[:id]
@file = Disposition.find_by(fingerprint: params[:id])
- message = {
+ publish(EventMessage.new(
agent_id: @agent.id,
- type: :lookup,
+ event_type: :scanned,
data: params[:data]
- }
- Publisher.publish("events.scanned.#{@agent.id}", message)
+ ))
end
end
end
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 84e9c93..8ce68a5 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -3,4 +3,10 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
protect_from_forgery with: :null_session
+
+ protected
+
+ def publish(message)
+ Publisher.publish(message)
+ end
end
diff --git a/app/controllers/dispositions_controller.rb b/app/controllers/dispositions_controller.rb
index 17f8657..e63f696 100644
--- a/app/controllers/dispositions_controller.rb
+++ b/app/controllers/dispositions_controller.rb
@@ -18,14 +18,19 @@ class DispositionsController < ApplicationController
end
def create
- fingerprint = disposition_params[:fingerprint]
- Publisher.publish("commands.poke.#{fingerprint}", disposition_params)
+ publish(PokeMessage.new(
+ fingerprint: disposition_params[:fingerprint],
+ state: disposition_params[:state],
+ ))
redirect_to dispositions_path, notice: 'Disposition was successfully created.'
end
def update
- Publisher.publish("poke", disposition_params)
+ publish(PokeMessage.new(
+ fingerprint: disposition_params[:fingerprint],
+ state: disposition_params[:state],
+ ))
redirect_to dispositions_path, notice: 'Disposition was successfully updated.'
end
diff --git a/app/models/event_message.rb b/app/models/event_message.rb
new file mode 100644
index 0000000..0a0c9ae
--- /dev/null
+++ b/app/models/event_message.rb
@@ -0,0 +1,25 @@
+class EventMessage
+ attr_reader :agent_id, :event_type, :data
+
+ def initialize(agent_id:, event_type:, data: {})
+ @agent_id = agent_id
+ @event_type = event_type
+ @data = data
+ end
+
+ def routing_key
+ "events.#{event_type}.#{agent_id}"
+ end
+
+ def to_hash
+ {
+ agent_id: agent_id,
+ event_type: event_type,
+ data: data
+ }
+ end
+
+ def to_json
+ to_hash.to_json
+ end
+end
diff --git a/app/models/poke_message.rb b/app/models/poke_message.rb
new file mode 100644
index 0000000..b134ba9
--- /dev/null
+++ b/app/models/poke_message.rb
@@ -0,0 +1,23 @@
+class PokeMessage
+ attr_reader :fingerprint, :state
+
+ def initialize(fingerprint:, state: )
+ @fingerprint = fingerprint
+ @state = state
+ end
+
+ def routing_key
+ "commands.poke.#{fingerprint}"
+ end
+
+ def to_hash
+ {
+ fingerprint: fingerprint,
+ state: state
+ }
+ end
+
+ def to_json
+ to_hash.to_json
+ end
+end
diff --git a/app/services/publisher.rb b/app/services/publisher.rb
index 704f1e3..1c384dd 100644
--- a/app/services/publisher.rb
+++ b/app/services/publisher.rb
@@ -1,7 +1,7 @@
class Publisher
- def self.publish(routing_key, message = {})
+ def self.publish(message)
exchange = channel.topic("malwer")
- exchange.publish(message.to_json, routing_key: routing_key)
+ exchange.publish(message.to_json, routing_key: message.routing_key)
end
def self.channel