Skip to content

Instantly share code, notes, and snippets.

@cleitonfco
Created September 12, 2009 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleitonfco/185892 to your computer and use it in GitHub Desktop.
Save cleitonfco/185892 to your computer and use it in GitHub Desktop.
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