This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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 |
OlderNewer