Commit 41c8858

mo khan <mo@mokhan.ca>
2015-02-10 03:36:18
use api controller and log errors in fake agent.
1 parent 34f53ef
app/controllers/api/agents/files_controller.rb
@@ -1,6 +1,6 @@
 module Api
   module Agents
-    class FilesController < ApplicationController
+    class FilesController < ApiController
       before_action do
         request.format = :json
       end
app/controllers/api/agents_controller.rb
@@ -1,9 +1,5 @@
 module Api
-  class AgentsController < ApplicationController
-    #before_action do
-      #request.format = :json
-    #end
-
+  class AgentsController < ApiController
     def create
       @agent = Agent.create!(agent_params)
     end
app/controllers/api/api_controller.rb
@@ -0,0 +1,5 @@
+module Api
+  class ApiController < ApplicationController
+    protect_from_forgery with: :null_session
+  end
+end
app/controllers/application_controller.rb
@@ -2,7 +2,6 @@ class ApplicationController < ActionController::Base
   # Prevent CSRF attacks by raising an exception.
   # For APIs, you may want to use :null_session instead.
   #protect_from_forgery with: :exception
-  protect_from_forgery with: :null_session
 
   protected
 
app/models/event_message.rb
@@ -14,7 +14,7 @@ class EventMessage
   def to_hash
     {
       agent_id: agent_id,
-      event_type: event_type,
+      type: event_type,
       data: data
     }
   end
app/models/scanned.rb
@@ -0,0 +1,2 @@
+class Scanned < Event
+end
lib/fake_agent.rb
@@ -44,6 +44,8 @@ class FakeAgent
     when "unknown"
       puts "file is unknown"
     end
+  rescue StandardError => error
+    log_error(error)
   end
 
   def sniff(interface)
@@ -85,8 +87,8 @@ class FakeAgent
       }
       Typhoeus.post(event_url, body: body)
     end
-  rescue => e
-    puts "#{e.message} #{e.backtrace.join(' ')}"
+  rescue StandardError => error
+    log_error(error)
   end
 
   def fingerprint_for(file)
@@ -122,4 +124,8 @@ class FakeAgent
   def registration_url
     "#{endpoint}/api/agents"
   end
+
+  def log_error(error)
+    puts "#{error.message} #{error.backtrace.join(' ')}"
+  end
 end