Airbrake search

We need to search for specific errors quiet often, this script helps us find them without having to go through the web interface.

Usage
auth_token can be found on your settings page, it is NOT the api-key.

ruby airbrake_search.rb your-account your-auth-token | grep foo

Code

#! /usr/bin/env ruby
# https://grosser.it/2012/09/08/airbrake-search
# search for errors given a name
#
# gem install airbrake-api
# USAGE: ruby airbrake_search.rb your-account your-auth-token | grep SOMETHING
# https://your-account.airbrake.io/errors/ID

require "airbrake-api"

AirbrakeAPI.account = ARGV[0] || raise("need airbrake account as ARGV[0]")
AirbrakeAPI.auth_token = ARGV[1] || raise("need airbrake token as ARGV[1], go to airbrake -> settings, copy your auth token")
AirbrakeAPI.secure = true

page = 1
while errors = AirbrakeAPI.errors(:page => page)
  errors.each do |error|
    puts "#{error.id} -- #{error.error_class} -- #{error.error_message} -- #{error.created_at}"
  end
  $stderr.puts "Page #{page} ----------\n"
  page += 1
end

Rack::Utils.escape / unescape + CGI.escape/unescape/escapeHTML vs undefined method bytesize for nil

The official solution for this problem is to use e.g. CGI.escape thing.to_str,
my unofficial solution is to automate that 🙂

Code

# https://grosser.it/2012/08/16/rackutils-escape-unescape-cgi-escapeunescapeescapehtml-vs-undefined-method-bytesize-for-nil/
AUTOMATED_TO_STR_FOR_SAFE_BUFFER = <<-RUBY
  def METHOD_with_html_safe(object)
    if object.is_a?(ActiveSupport::SafeBuffer)
      METHOD(object.to_str)
    else
      METHOD_without_html_safe(object)
    end
  end
  alias_method_chain :METHOD, :html_safe
RUBY

# can be removed if
# Rack::Utils.escape "a & & %24".html_safe
# Rack::Utils.unescape "a & & %24".html_safe
# does not raise an error, must work with strings and symbols
Rack::Utils.class_eval do
  [:escape, :unescape].each do |method|
    eval AUTOMATED_TO_STR_FOR_SAFE_BUFFER.gsub("METHOD", method.to_s)
    module_function :"#{method}_without_html_safe"
    module_function method
  end
end

# can be removed if
# CGI.escape "a & & %24".html_safe
# CGI.unescape "a & & %24".html_safe
# CGI.unescapeHTML "a & & %24".html_safe
# does not raise an error, must work with strings and symbols
# (escapeHTML always works)
CGI.class_eval do
  class << self
    [:escape, :unescape, :unescapeHTML].each do |method|
      eval AUTOMATED_TO_STR_FOR_SAFE_BUFFER.gsub("METHOD", method.to_s)
    end
  end
end

Object.send :remove_const, :AUTOMATED_TO_STR_FOR_SAFE_BUFFER

Ruby/ActiveRecord fastest way to truncate test database

Tried a few strategies, but this seems to be the fastest:
DatabaseCleaner truncate_all: 0.8s
This: 0.2s

# fast truncation of all tables that need truncations (select is 10x faster then truncate)
# https://grosser.it/2012/07/03/rubyactiverecord-fastest-way-to-truncate-test-database/
def truncate_all_tables
  config = ActiveRecord::Base.configurations[::Rails.env]
  connection = ActiveRecord::Base.connection
  connection.disable_referential_integrity do
    connection.tables.each do |table_name|
      next if connection.select_value("SELECT count(*) FROM #{table_name}") == 0
      case config["adapter"]
      when "mysql", "mysql2", "postgresql"
        connection.execute("TRUNCATE #{table_name}")
      when "sqlite", "sqlite3"
        connection.execute("DELETE FROM #{table_name}")
        connection.execute("DELETE FROM sqlite_sequence where name='#{table_name}'")
      end
    end
    connection.execute("VACUUM") if config["adapter"] == "sqlite3"
  end
end