Class: ExceptionsBegone::Sender

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.mailerObject

ugly



5
6
7
# File 'lib/exceptions_begone/sender.rb', line 5

def mailer
  @mailer
end

Class Method Details

.log(message) ⇒ Object



32
33
34
# File 'lib/exceptions_begone/sender.rb', line 32

def log(message)
  Rails.logger.error("[EXCEPTIONS_BEGONE]: #{message}")
end

.method_missing(method_name, *args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/exceptions_begone/sender.rb', line 36

def method_missing(method_name, *args)
  if method_name.to_s =~ /^send_(.*)/
    Sender.__send__(:send_generic, "#{$1}", *args)
  else
    super(method_name, *args)
  end
end

.post(data, connection_options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/exceptions_begone/sender.rb', line 7

def post(data, connection_options = {})
  conn_conf = ConnectionConfigurator.build(connection_options)

  http = Net::HTTP.new(conn_conf.host, conn_conf.port)
  http.read_timeout = conn_conf.read_timeout
  http.open_timeout = conn_conf.open_timeout

  log(data)
  http.post(conn_conf.path, data, "Content-type" => "application/json", "Accept" => "application/json")
rescue TimeoutError, Errno::ECONNREFUSED => e
  log(e.inspect)
  log(data)
  mailer.deliver_error(e.message, "#{e.inspect} #{data}") if mailer
end

.send_exception(exception, controller, request, connection_options = {}) ⇒ Object



22
23
24
25
# File 'lib/exceptions_begone/sender.rb', line 22

def send_exception(exception, controller, request, connection_options = {})
  notification = Formatter.format_exception_data(exception, controller, request)
  send_generic("exception", notification, connection_options = {})    
end

.send_generic(category, notification, connection_options = {}) ⇒ Object



27
28
29
30
# File 'lib/exceptions_begone/sender.rb', line 27

def send_generic(category, notification, connection_options = {})
  data = Formatter.format_data(category, notification)
  post(data, connection_options)
end