Skip to content

Instantly share code, notes, and snippets.

View pboling's full-sized avatar
🏓
Ping me if you need me!

Peter Boling pboling

🏓
Ping me if you need me!
View GitHub Profile
@pboling
pboling / phone_finder.rb
Last active August 29, 2015 14:11
Find Phone Numbers in Dirty Database
# SQL_PHONE_NORMALIZER Example:
# Lead.find_by_sql(PhoneFinder::SQL_PHONE_NORMALIZER.call(table: 'leads', column_name: 'phone', phone: '(555) 760-2012'))
# User.find_by_sql(PhoneFinder::SQL_PHONE_NORMALIZER.call(table: 'users', column_name: 'phone', phone: '555-223-4027'))
#
# AREL_PHONE_NORMALIZER Example:
# PhoneFinder::AREL_PHONE_NORMALIZER.call(rel: Lead.where(email: 'peter.boling@example.org'), table: 'leads', column_name: 'phone', phone: '(555) 760-2012')
# PhoneFinder::AREL_PHONE_NORMALIZER.call(rel: User.where(email: 'peter.boling@example.org'), table: 'users', column_name: 'phone', phone: '555-223-4027')
#
# See: https://coderbits.com/posts/pCl7og
module PhoneFinder
@pboling
pboling / api_logger.rb
Last active August 29, 2015 14:13 — forked from dblock/api_logger.rb
require 'ext/grape_middleware_logger'
module TrumakerAPI
module Middleware
class ApiLogger < Grape::Middleware::Logger
def after
logger.info "[api] Requested#{request_log}" if !request_log.blank?
if Rails.env.development?
response_body = JSON.parse(response.body.first)
@pboling
pboling / nested_filters.rb
Last active August 29, 2015 14:16
Concern::NestedFilters
module Concern
module NestedFilters
extend ActiveSupport::Concern
CACHE_EXPIRATION = 1.days
NULL_SORT = "LAST"
NestedFilter = Struct.new(:filter_klass, :filter_param_key, :filter_name, :filter_options)
NestedFilterOption = Struct.new(:id, :name)
included do
# Download all repos for an organization
ORG_NAME=trumaker
curl -s https://api.github.com/orgs/$ORG_NAME/repos?per_page=200 | ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
# Download all repos for a user
USER_NAME=pboling
curl -s https://api.github.com/users/$USER_NAME/repos?per_page=200 | ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
@pboling
pboling / boolean.rb
Created April 3, 2015 00:01
TrueClass & FalseClass => Boolean Comparable hack
module Boolean
include Comparable
# true > false
def <=>(other)
raise ArgumentError, "Do not know how to compare #{other.class} with TrueClass and FalseClass" unless [TrueClass, FalseClass].include?(other.class)
other ? (self ? 0 : -1) : (self ? 1 : 0)
end
end
@pboling
pboling / prepare-commit-msg
Last active September 19, 2015 08:28
commit message git hook: Adds story type and story ID to the end of each commit
#!/usr/bin/env ruby
# vim: set syntax=ruby
# branches should be named like:
# <story_type>/<story_id>-explosion-in-the-fudge-factory-spec-suite-fix
# where story type is one of "hotfix", "bug", "feature", "candy"
#
branch = `git branch 2> /dev/null | grep -e ^* | awk '{print $2}'`
regex = /^(?<story_type>(hotfix)|(bug)|(feature)|(candy))\/(?<story_id>\d{8,})-.+\Z/
match_data = branch.match(regex)
@pboling
pboling / gist:3714897
Last active October 10, 2015 15:57
compare specs run by rake & autotest
rake_files = "#{list of files copied from console when rake command is run}"
autotest_files = "#{list of files copied from console when autotest command is run}"
r = rake_files.split(' ').map {|x| ar = x.split('/'); ar[(ar.length-2)..(ar.length-1)].join('/')}
a = autotest_files.split(' ').map {|x| ar = x.gsub(/'/,'').split('/'); ar[(ar.length-2)..(ar.length-1)].join('/')}
# autotest will generally be lacking some, as its config becomes out of date
diff = r - a
@pboling
pboling / plpgsql.rake
Last active October 12, 2015 18:56 — forked from rietta/plpgsql.rake
Are you using PostgreSQL and don't want to make your app run as PostgreSQL super user, then add this custom rake task to your `lib/tasks` folder and be happy.
#
# PostgreSQL writes two optional commands to the database schema
# file, called db/structure.sql, that can only be run as a root
# database user. These are not needed actually, so comment them
# out automatically
#
# CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
# COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
#
namespace :db do
@pboling
pboling / std_out.rb
Created October 18, 2015 00:37
Rspec support: Silnce the `puts`
# Some specs causes a lot of stuff to output to the console during the spec run which interrupts the flow of lovely green dots.
# We don't want to ignore stuff that we want to see, like rspec deprecation warnings, but for an otherwise clean spec,
# that is puts ing things, we can turn those off by tagging the spec as :noisy.
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:each, :noisy) do
# Redirect stderr and stdout
$stderr = File.open(File::NULL, "w") # to /dev/null, since Ruby 1.9.3
$stdout = File.open(File::NULL, "w") # to /dev/null, since Ruby 1.9.3
@pboling
pboling / Airbrake GirlFriday Error
Created February 4, 2013 23:18
Airbrake generator fails on "uninitialized constant GirlFriday".
script/rails generate airbrake --api-key key---derp
Configuration:
api_key: "key---derp"
js_api_key: "key---derp"
backtrace_filters: [#<Proc:0x007fa29d75c708@/Users/pboling/.rvm/gems/ruby-
development_environments: []
development_lookup: true
environment_name: "development"
host: "api.airbrake.io"