Skip to content

Instantly share code, notes, and snippets.

@rustyio
rustyio / subgit
Created January 24, 2010 18:48
subgit
#!/usr/bin/env sh
# subgit
#
# A tiny wrapper around git to help you manage
# Git sub-projects easily, safely, and simply.
#
# Created by Rusty Klophaus (@rklophaus)
#
# See http://rklophaus.com/subgit for usage.
def self.cached_method(meth)
eval <<-CODE
def #{meth}_with_cache
return @#{meth} if @#{meth}
result = #{meth}_without_cache
@#{meth} ||= result
end
CODE
alias_method_chain meth, :cache
end
@ahoward
ahoward / net-http-debug.rb
Created December 10, 2010 20:06
a simple way to debug tons of libs that use ruby's net/http
BEGIN {
require 'net/http'
Net::HTTP.module_eval do
alias_method '__initialize__', 'initialize'
def initialize(*args,&block)
__initialize__(*args, &block)
@leshill
leshill / gist:870866
Created March 15, 2011 15:17
Cucumber/Capybara JS alert handling
# Add this to more_web_steps.rb
# Selenium docs: http://code.google.com/p/selenium/wiki/RubyBindings#Javascript_Alert/Confirm today!
When /^I (accept|dismiss) the "([^"]*)" alert$/ do |action, text|
alert = page.driver.browser.switch_to.alert
alert.text.should eq(text)
alert.send(action)
end
@igrigorik
igrigorik / rack_routes.rb
Created March 29, 2011 04:17
Goliath + http_router.rb
require 'goliath'
require 'http_router'
# https://github.com/joshbuddy/http_router
HttpRouter::Rack.override_rack_builder!
class RackRoutes < Goliath::API
map('/get/:id') do |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]}\n"]]
end
@mrflip
mrflip / async_aroundware.rb
Created May 3, 2011 06:47
a way to make an asynchronous request in the middleware, and only proceed with the response when both the endpoint and our middleware's responses have completed.
class Barrier < EM::Synchrony::Multi
attr_accessor :env, :status, :headers, :body
def initialize env
super()
@env = env
@acb = env['async.callback']
env['async.callback'] = self
callback do
@mrflip
mrflip / auth_and_rate_limit.rb
Created May 11, 2011 06:59
API key authentication + rate limiting in Goliath using MongoDB (incomplete sketch)
#!/usr/bin/env ruby
$: << File.join(File.dirname(__FILE__), '../lib')
require 'goliath'
require 'em-mongo'
require 'em-http'
require 'em-synchrony/em-http'
require 'yajl/json_gem'
require 'goliath/synchrony/mongo_receiver' # has the aroundware logic for talking to mongodb
require File.join(File.dirname(__FILE__), 'auth_receiver')
// Allows you to specify events on the data tied to the view in the same way
// that you can specify dom events:
//
// dataEvents: {
// instanceName: {
// 'change:name': 'mothed',
// delete: 'otherMethod'
// }, ...
// }
registerDataEvents: function(unbind) {
@davetapley
davetapley / update_sdists.sh
Created January 3, 2012 22:17
If a package's sdist tarball is newer than its cabal-dev installed version, reinstall it
#!/bin/bash
SANDBOX_DIR="cabal-dev"
SDIST_DIR="./sdist/"
SDIST_SUFFIX=".tar.gz"
PACKAGES_CONF_DIR=$SANDBOX_DIR"/packages-"`ghc --numeric-version`".conf/"
find $SDIST_DIR -name *$SDIST_SUFFIX -print0 | while read -d $'\0' sdist_path
do
@hesselink
hesselink / ExtensibleRecords.hs
Created April 25, 2012 20:05
Extensible records with data kinds
{-# LANGUAGE
GADTs
, KindSignatures
, DataKinds
, PolyKinds
, TypeOperators
, TypeFamilies
, MultiParamTypeClasses
, FlexibleInstances
, UndecidableInstances