Skip to content

Instantly share code, notes, and snippets.

View periodically_cleared_hash.rb
# A Hash derivative that is automatically cleared every so often
class PeriodicallyClearedHash
def initialize(period, &default_value_calculator)
@period = period.to_i
@default_value_calculator = default_value_calculator || lambda { nil }
attr_reader :period
View prevent_denial_of_service.rb
# This Rack middleware prevents API consumers from exceeding their allocated requests-per-minute limit.
# Per-minute usage allocations divided by 6 to give a 10-second allocation. If a consumer goes over-limit
# for a particular period, they are temporarily denied access.
class PreventDenialOfService
class << self
def call(env)
View stub_rest_client.rb
def stub_rest_client(method, url = nil, options = {}, &block)
options = options.dup
options[:method] = method
options[:url] = url.to_s if url
stub(RestClient::Request).execute(hash_including(options), &block)
mdub / rvm-default-ruby
Created January 18, 2010 11:47
View rvm-default-ruby
# For easy rvm/TextMate integration, point your "TM_RUBY" Shell Variable at this script
# e.g. TM_RUBY=/Users/whaevuh/bin/rvm-default-ruby
source $HOME/.rvm/config/default
exec ruby "$@"
View gist:289701
sudo cp /usr/local/Cellar/mysql/5.1.41/com.mysql.mysqld.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist
sudo cp /usr/local/Cellar/postgresql/8.4.2/org.postgresql.postgres.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/org.postgresql.postgres.plist
sudo cp /usr/local/Cellar/mongodb/1.2.0-x86_64/org.mongodb.mongod.plist /Library/LaunchDaemons/
sudo launchctl load -w /Library/LaunchDaemons/org.mongodb.mongod.plist
View capybara-example.rb
require "capybara"
Capybara.app_host = ""
session =, "dummy app")
puts "URL: #{session.current_url}"
puts "\nArticles:"
session.all('//h3').each do |e|
mdub / example_pact_matcher_spec.rb
Created September 6, 2015 11:42
Using Pact data-diffs for fun and profit
View example_pact_matcher_spec.rb
require "spec_helper"
require "pact/matchers"
require "pact/matchers/unix_diff_formatter"
require "term/ansicolor"
RSpec::Matchers.define :match_data do |expected|
match do |actual|
mdub / chef-bootstrap.rb
Created October 29, 2010 01:08
bootstrap a remote machine as a Chef server/client
View chef-bootstrap.rb
#!/usr/bin/env ruby
require "rubygems"
require "erb"
require "fileutils"
require "json"
require "net/ssh"
require "net/scp"
require "optparse"
mdub / sass_stylesheets.rb
Created December 15, 2010 11:57
serve *.sass using Rack::File
View sass_stylesheets.rb
# app/metal/sass_stylesheets.rb
# This Rails metal keeps Sass stylesheets up-to-date, and serves them using Rack.
# Unlike the default Sass/Rails integration, this happily supports a
# Sass::Plugin.options[:css_location] outside the Rails.public_path.
class SassStylesheets
class << self
View heroku_static_cache_control.rb
if defined?(Heroku::StaticAssetsMiddleware)
puts "Carefully adjusting Heroku::StaticAssetsMiddleware"
class Heroku::StaticAssetsMiddleware
def can_cache?(reply)
status, headers, body = reply
# if there's already a "Cache-Control" header, don't override it
body.kind_of?(Rack::File) and status.to_i == 200 and !headers.has_key?("Cache-Control")