Skip to content

Instantly share code, notes, and snippets.

View kmayer's full-sized avatar

Ken Mayer kmayer

View GitHub Profile
@kmayer
kmayer / application_record.rb
Last active June 15, 2017 20:26
How to get a fast, but approximate, row count in ActiveRecord + Postgresql
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
class << self
def approximate_row_count(table_name = arel_table.name)
results = ActiveRecord::Base.connection_pool.with_connection { |c|
c.execute("SELECT reltuples::BIGINT AS approximate_row_count FROM pg_class WHERE relname = '#{table_name}'")
}
results.present? ? results.first['approximate_row_count'].to_i : nil
end
@kmayer
kmayer / fizzbuzz.rb
Last active March 13, 2017 03:55
FizzBuzz to 100 in one line of (very compact) ruby. And other variants
# How to use and experiment
# $ irb -I. -rfizzbuzz
# => fb = FizzBuzz.new
# => fb.enumerator_with_array_indices.take(30) == FizzBuzz.EXPECTATIONS
# => true
class FizzBuzz
EXPECTATIONS=[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26, "Fizz", 28, 29, "FizzBuzz"]
def shortest # 100 chars of ruby
@kmayer
kmayer / ruby_web_proxy.sh
Created March 8, 2017 19:08
Instant web proxy server
ruby -r webrick/httpproxy -e 's = WEBrick::HTTPProxyServer.new(:Port => 9999, :RequestCallback => Proc.new{|req,res| puts req.request_line, req.raw_header}); trap("INT"){s.shutdown}; s.start'
@kmayer
kmayer / logentries_alerts_controller.rb
Created October 27, 2016 21:22
LogentriesAlertsController
class LogentriesAlertsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
return head(:unprocessable_entity) unless
LogentriesAuth.new(request).authorized?
if what =~ /R1[45]/ &&
who =~ /web\.\d+/
Redis.current.set(
@kmayer
kmayer / logentries_auth.rb
Last active October 26, 2016 14:04
Logentries.com HMAC Authenticator in Ruby, for Rails
# Check authenticity of a logentries.com webhook
# cf: https://logentries.com/doc/webhookalert/
# cf: https://blog.logentries.com/2013/02/webhooks-are-hmac-authenticated/
require 'openssl'
require 'base64'
require 'digest/md5'
class LogentriesAuth
attr_reader :request
@kmayer
kmayer / sticky_heading_list.jsx
Created April 28, 2016 16:57
<StickyHeadingList> react component
var _css = require("./sticky_heading_list.scss");
// import from https://github.com/polarblau/stickySectionHeaders
// requires jQuery as a global
(function($){
/* A little helper to calculate the sum of different
* CSS properties
*
* EXAMPLE:
* $('#my-div').cssSum('paddingLeft', 'paddingRight');
@kmayer
kmayer / puma.rb
Last active April 5, 2016 23:13
Puma plugin to stop via Redis on Heroku
# === Plugins ===
require './lib/puma/plugin/redis_stop_puma'
plugin 'redis_stop_puma'
@kmayer
kmayer / trustctime.rb
Created November 27, 2013 18:02
chef recipe to turn off ctime trust
# http://www.git-tower.com/blog/make-git-rebase-safe-on-osx/
execute "mistrust ctime" do
command "git config --global core.trustctime #{node['git']['trustctime'].to_s }"
user node['current_user']
end
@kmayer
kmayer / sidekiq.rb
Created September 8, 2013 17:41
Sidekiq Exception tracer for NewRelic Add this to your sidekiq initializer
module NewRelic
class SidekiqException
def call(worker, msg, queue)
begin
yield
rescue => exception
NewRelic::Agent.notice_error(exception, :custom_params => msg)
raise exception
end
end
@kmayer
kmayer / player.rb
Created August 27, 2013 20:37
rubywarrior (Level 6) with Ryan Spore
Command = Struct.new(:method, :direction)
class Command
def initialize(m, *args)
@method = m
@direction = args
end
def perform(warrior)
warrior.send(@method, *@direction)