Class: EventNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/event_notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventNotifier

Returns a new instance of EventNotifier.



64
65
66
67
# File 'lib/event_notifier.rb', line 64

def initialize()
  read_config
  @config[:ignore_exceptions] ||= self.class.default_ignore_exceptions
end

Class Method Details

.default_ignore_exceptionsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/event_notifier.rb', line 35

def self.default_ignore_exceptions
   [].tap do |exceptions|
     
     exceptions << 'ActionController::RoutingError'
	exceptions << 'ActionController::InvalidAuthenticityToken'
	exceptions << 'CGI::Session::CookieStore::TamperedWithCookie'
	exceptions << 'ActionController::UnknownAction'
	exceptions << 'ActionController::UnknownHttpMethod'
exceptions << 'Mongoid::Errors::DocumentNotFound'
   end
end

Instance Method Details

#configure(opts = {}) ⇒ Object

Configure through hash



24
25
26
27
28
29
30
31
32
33
# File 'lib/event_notifier.rb', line 24

def configure(opts = {})
	@config = {
            :api_key=> "",
            :uri =>""
          }

@valid_config_keys = @config.keys

  opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
end

#ignored_exception(ignore_array, exception) ⇒ Object



60
61
62
# File 'lib/event_notifier.rb', line 60

def ignored_exception(ignore_array, exception)
  Array.wrap(ignore_array).map(&:to_s).include?(exception.class.name)
end

#notify(exception, request, params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/event_notifier.rb', line 69

def notify(exception, request,params)
	unless ignored_exception(@config[:ignore_exceptions],exception )

		event = EventNotification.build_hash(exception,request,params)
		@event = event.to_json
		uri = URI.parse(@config[:uri])
		token = @config[:api_key]

		https = Net::HTTP.new(uri.host, uri.port)
		https.use_ssl = false
		request = Net::HTTP::Post.new(uri.path,initheader = {"Content-Type" =>"application/json", "Authorization" => "Token token=#{token}"})

		request.body = "#@event"

		response = https.request(request)
	end
end

#read_configObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/event_notifier.rb', line 47

def read_config
	path_to_yaml_file = "#{Rails.root}/config/appmonitor.yml"
	begin
     config = YAML::load(IO.read(path_to_yaml_file))
     Rails.logger.info(config)
   rescue Errno::ENOENT
return
   rescue Psych::SyntaxError
 return
   end
   configure(config)
end