Skip to content

Instantly share code, notes, and snippets.

class DatabaseLogSubscriber < ActiveSupport::LogSubscriber
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
def sql(event)
return unless logger.debug?
payload = event.payload
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
conn_id = payload[:connection_id]
conn = ObjectSpace._id2ref(conn_id)
from_connection = conn.active? ? conn.current_database : "No active connection"
@codeodor
codeodor / explanation.md
Last active January 6, 2016 12:47
I need help with an error between Net::FTP and OpenSSL in Ruby.

I'm trying to get FTPFXP to work on Ruby 2.0 (so I can FTP over TLS)

I'm having problems with this line, which sets up the SSL Socket. OpenSSL checks whether @sock is a T_FILE

In Ruby 1.9.3, it worked well, because it was just a TCPSocket but in Ruby 2.0, @sock becomes a Net::FTP::BufferedSocket.

It's easy enough to change the code in https://github.com/codeodor/ftpfxp/blob/06f2d56e65e73d3818b6c7aef4dfe461bad98849/lib/ftpfxp/ftpfxptls.rb#L87 to send @sock.io to make it a TCPSocket again, but when Net::FTP tries to close the socket, it expects the socket to have a read timeout, wh

@codeodor
codeodor / extract_archive.rb
Last active January 3, 2016 17:19
Proof of concept for adding extract_archive.rb to Fog for Rackspace
require 'fog'
module Fog
module Storage
class Rackspace
class Real
# Extract Archive
#
# See http://docs.rackspace.com/files/api/v1/cf-devguide/content/Extract_Archive-d1e2338.html
#
@codeodor
codeodor / houston_rb_december_2013.md
Last active December 30, 2015 23:39
Code for discussion @houstonrb December 2013 meetup.

I won't make it tonight because while I thought I'd be recovered from illness by now, I'm still a little sick and don't want to infect the entire Houston Ruby community with a nasty virus!

But I had some ideas for code, so if you need some, here they are:

These commits to Rails might seem like bad code because they take something elegant and turn it into longer uglier code, but the performance gains are nice!

rails/rails#12185

and

@codeodor
codeodor / trace-tools.txt
Last active December 29, 2015 18:49
rvm --trace upgrade && rvm --trace tools
~$ rvm --trace tools strings 1.9.3-p484
+ 1385768598.505099713 /scripts/cli : __rvm_parse_args() 671 > [[ -n '' ]]
+ 1385768598.514017915 /scripts/cli : __rvm_parse_args() 673 > set -o errtrace
+ 1385768598.523055186 /scripts/cli : __rvm_parse_args() 674 > export 'PS4=+ $(__rvm_date "+%s.%N" 2>/dev/null) ${BASH_SOURCE##${rvm_path:-}} : ${FUNCNAME[0]:+${FUNCNAME[0]}()} ${LINENO} > '
+ 1385768598.531905984 /scripts/cli : __rvm_parse_args() 674 > PS4='+ $(__rvm_date "+%s.%N" 2>/dev/null) ${BASH_SOURCE##${rvm_path:-}} : ${FUNCNAME[0]:+${FUNCNAME[0]}()} ${LINENO} > '
+ 1385768598.540891056 /scripts/cli : __rvm_parse_args() 708 > [[ -z '' ]]
+ 1385768598.549814287 /scripts/cli : __rvm_parse_args() 708 > [[ -n '' ]]
+ 1385768598.558420167 /scripts/cli : __rvm_parse_args() 711 > [[ error == '' ]]
+ 1385768598.567271382 /scripts/cli : __rvm_parse_args() 711 > [[ 0 -eq 1 ]]
+ 1385768598.576020986 /scripts/cli : __rvm_parse_args() 711 > [[ -n '' ]]
@codeodor
codeodor / gist:7101231
Created October 22, 2013 13:55
What I really want is $(‘option [value :in(1,2,3)]’) to select all options whose value is 1, 2, or 3.
Example:
select 2 has, say 10 options: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
select1_option0 should hide nothing in select2
select1_option1 should hide all but select2: options 1, 2, and 3
select1_option2 should hide all but select2: options 1, 2, 3, and 9
select1_option3 should hide all but select2: options 5, 6, and 7
select1_option4 should hide all but select2: options 4, 6, 8, and 1
@codeodor
codeodor / bob.rb
Created August 14, 2013 13:46 — forked from rlb3/bob.rb
require 'ripper'
def Object.const_missing(name)
klass = const_set name, Class.new
klass.class_eval do
def method_missing name, *args
file, line_num, _ = caller[0].split(':')
file_contents = File.open(file).read
@codeodor
codeodor / index_stats.rb
Created April 14, 2013 12:50
Inspect migrations to get an idea of how we define and use indexes in a Rails project.
apps = [
"/path/to/rails/project/root",
"/path/to/another/rails/project/root"
]
def number_of_migrations_since_first_reference(attribute, directory, indexed_in)
grep_command = "grep -H \"#{attribute.gsub(/[\[\]\s]/,'')}\" #{directory}/*.rb"
all_refs = `#{grep_command}`
return -1 unless all_refs.length > 0 # probably defined with t.references :other_table
@codeodor
codeodor / epic_yak_shave.sh
Created February 11, 2013 23:42
Setting up statsd to run with the graphite backend.
git clone git://github.com/etsy/statsd.git
cd statsd
# yak shaving:
# install nodejs if you don't have it
brew install node # assuming you have/use homebrew
# install python if you don't have it
# I already had python due to it being packaged with MacOS, but if you don't have it, you'll need it
@codeodor
codeodor / friend.rb
Created November 14, 2012 02:33
Friend functions in Ruby
# See http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-set_trace_func for docs on this function
require 'ostruct'
module Friendly
def self.included(base)
base.extend(ClassMethods)
end
def method_missing(name, *args, &block)