cleitonfco (owner)

Revisions

gist: 185892 Download_button fork
public
Public Clone URL: git://gist.github.com/185892.git
Embed All Files: show embed
my_middleware.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'rubygems'
require 'twitter'
 
class MyMiddleware
  def initialize(app, config = {})
    @app = app
    @config = { :app => 'myapp' }.merge(config)
  end
 
  def call(env)
    begin
      status, headers, response = @app.call(env)
    rescue => exception
      status, headers, response = 500, { 'Content-Type' => 'text/plain' }, 'Ops! Temos um problema aqui.'
      send_twitter(exception, env, headers)
    end
    [status, headers.merge({'X-Middleware-App' => @config[:app]}), response]
  end
 
  def send_twitter(exception, env, headers)
    message = "#{env['PATH_INFO']}: '#{exception.to_s}' #erro ##{@config[:app]}"
 
    if (@config[:user] && @config[:pass])
      message += "..." if message.slice!(137..-1)
 
      # Conecta no Twitter através de HTTPAuth
      @twitter ||= Twitter::Base.new(Twitter::HTTPAuth.new(@config[:user], @config[:pass]))
 
      # Envia a mensagem
      @twitter.update message
 
      # Acrescenta a mensagem ao cabeçalho da resposta
      headers.merge!({ 'X-Message-Sent' => message })
    end
  end
end