Skip to content

Instantly share code, notes, and snippets.

@heisters
heisters / script_spec.rb
Created April 23, 2009 21:19
rspec I/O for a ruby script
require 'open3'
describe 'my_script' do
def run *args
options = args.extract_options!
args.unshift './my_script'
Open3.popen3(args.join(' ')) do |i, o, e|
i.write options[:stdin]
i.close
@heisters
heisters / object_try.rb
Created April 24, 2009 17:17
a more succinct implementation of Object#try
# Makes it easy to try more than one method on Object and just return nil if
# the Object doesn't respond to any of the methods. So, rather than:
#
# obj.try(:foo).try(:bar)
#
# you can just
#
# obj.try(:foo, :bar)
#
# Based on Object#try from ActiveSupport.
@heisters
heisters / enumerable_try_keys.rb
Created April 24, 2009 20:37
access elements deep in a nested enumerable safely
# Makes it easy to access elements deep within a nested Enumerable. Rather than
# having to do
#
# enum[:foo] && enum[:foo][:bar]
#
# you can just do
#
# enum.try_keys :foo, :bar
module Enumerable
def try_keys *keys
@heisters
heisters / render_status_codes.rb
Created May 5, 2009 17:02
automatically define Rails render_* methods for all possible HTTP responses
class ApplicationController < ActionController::Base
protected
# Define render_xxx methods for all known status codes.
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |symbol, code|
define_method "render_#{symbol}" do |*args|
respond_to do |format|
format.html{render_error_html_file(code, (args.first || {}))}
format.all{head symbol}
end
@heisters
heisters / Rakefile.rb
Created June 2, 2009 00:41
a rake task to pull gists from github.com as html
# A rake task to pull all your gists from github.com as html.
#
# This has advantages over the script embed:
# * the script uses document.write, which is not compatible with XHTML Strict 1.0
# * document.write also can't be used after the document body has loaded (ie. no AJAX)
# * the gists get indexed by search engines this way
desc "Pull all gists from github and cache them locally"
task :gists do
uri = URI.parse 'http://gist.github.com/api/v1/yaml/gists/heisters'
html = YAML.load(Net::HTTP.get(uri))['gists'].inject('') do |h, gist|
@heisters
heisters / basic_ufw.sh
Created June 11, 2009 17:38
A basic UFW firewall configuration
ufw default deny
ufw allow ssh/tcp
ufw limit ssh/tcp
ufw allow http/tcp
ufw allow https/tcp
ufw logging on
ufw enable
ufw status
@heisters
heisters / tshark_http.sh
Created June 15, 2009 00:34
Make TShark dump and dissect an HTTP connection
# Make TShark dump and dissect an HTTP connection
sudo tshark -i3 -V -T text -i en1 -f "tcp port 80 and host <host ip>" -d "tcp.port==80,http" -R http.request
@heisters
heisters / temporary_dsl.rb
Created July 24, 2009 20:58
A Ruby 1.9 DSL strategy
# An unintrusive, temporary DSL strategy.
#
# Why discusses Ruby DSLs and instance_eval vs. block arguments:
# http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html
#
# This takes advantage of some Ruby 1.9 features to implement a DSL mixin that
# is temporary and doesn't override locally defined methods.
class DslInstance
attr_accessor :script
@heisters
heisters / jsonp.js
Created October 22, 2009 00:20 — forked from malsup/jsonp
an alternative to jQuery's JSONP method with basic error handling
// fn to handle jsonp with timeouts and errors
// hat tip to Ricardo Tomasi for the timeout logic
$.getJSONP = function(s) {
s.dataType = 'jsonp';
$.ajax(s);
var t = 0, cb = s.url.match(/callback=(\w+)/)[1], cbFn = window[cb];
var $script = $('head script[src*='+cb+']');
if (!$script.length)
return; // same domain request
@heisters
heisters / bdd.vim
Created December 30, 2009 20:08
Run RSpec and Cucumber from vim, on the current spec/feature
" Vim functions to run RSpec and Cucumber on the current file and optionally on
" the spec/scenario under the cursor. Within the context of a Rails app, will
" prefer script/spec and script/cucumber over their system counterparts.
function! RailsScriptIfExists(name)
" Bundle exec
if isdirectory(".bundle") || (exists("b:rails_root") && isdirectory(b:rails_root . "/.bundle"))
return "bundle exec " . a:name
" Script directory
elseif exists("b:rails_root") && filereadable(b:rails_root . "/script/" . a:name)
return b:rails_root . "/script/" . a:name