Create a gist now

Instantly share code, notes, and snippets.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment