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 }
end
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)
new(env).call
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)
end
@mdub
mdub / rvm-default-ruby
Created Jan 18, 2010
rvm-default-ruby
View rvm-default-ruby
#!/bin/sh
# 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 = "http://dogbiscuit.org"
session = Capybara::Session.new(:culerity, "dummy app")
session.visit("/")
puts "URL: #{session.current_url}"
puts "\nArticles:"
session.all('//h3').each do |e|
@mdub
mdub / chef-bootstrap.rb
Created Oct 29, 2010
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
mdub / sass_stylesheets.rb
Created Dec 15, 2010
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")
end
end
end
@mdub
mdub / pgbackup.rake
Created May 12, 2011
Rake tasks to capture backups and archive in S3
View pgbackup.rake
# Tasks designed to run ON Heroku
namespace :pgbackup do
def pgbackup_client
require "pgbackups/client"
@pgbackup_client ||= PGBackups::Client.new(ENV.fetch("PGBACKUPS_URL"))
end
task :capture do
db = "DATABASE_URL"