Skip to content

Instantly share code, notes, and snippets.

View hoverlover's full-sized avatar

Chad Boyd hoverlover

View GitHub Profile
# open the file containing the gems to load and install each
File.open(Dir.pwd << "/gems.txt", "r") do |f|
while (line = f.gets)
puts `sudo gem install --no-ri --no-rdoc #{line}` unless line.strip.empty?
end
end
# Pick up to 10 names, making sure to draw from each "hat" at least once
class Name < Struct.new(:first, :last); end
max_names = 10
hat1 = [Name.new('Abraham', 'Lincoln'), Name.new('George', 'Washington')]
hat2 = [Name.new('Thomas', 'Jefferson'), Name.new('James', 'Madison'), Name.new('Theodore', 'Roosevelt'), Name.new('John', 'Kennedy')]
hat3 = [Name.new('Calvin', 'Coolidge'), Name.new('Ronald', 'Regan'), Name.new('Woodrow', 'Wilson')]
[hat1, hat2, hat3].sort{|a,b| b.size <=> a.size}.inject{|memo, names| memo.zip(names)}.flatten.compact[0, max_names]
@hoverlover
hoverlover / enumerable_constants.rb
Created May 25, 2010 16:02
Module to facility the enumeration of constants
# This module should be used for situations where enumerating over constant values is desired.
# This facilitates keeping the code DRY by keeping the constant values defined in one place, and
# still having the ability to enumerate over them wherever they are needed.
#
# Example use cases:
#
# * defining constants for possible field values in an AR object and including this module to
# provide access to the values in a 'validates_inclusion_of' validation
# * defining constants for select box values in a view and including this module to allow them to be
# enumerated over in the select tag
module Travis
class Builder
@@ -29,9 +30,18 @@ module Travis
host = rails_config['url'] || 'http://127.0.0.1'
url = "#{host}/builds/#{build['id']}#{'/log' if data.delete(:append)}"
uri = URI.parse(host)
- data = { :body => { :_method => :put, :build => data }, :head => { :authorization => [uri.user, uri.password] } }
- # stdout.puts "-- post to #{url} : #{data.inspect}"
- register_connection EventMachine::HttpRequest.new(url).post(data)
+ data = {
@hoverlover
hoverlover / spec_helper.rb
Created March 4, 2011 21:19
Place this somewhere that gets loaded when RSpec starts and your browser will automatically show the contents of the webpage when an acceptance spec fails.
# Somewhere in the file
Capybara.auto_save_and_open_page = true if 'true' == ENV['CAPYBARA_AUTO_SAVE_AND_OPEN_PAGE']
@hoverlover
hoverlover / active_admin_heroku.rb
Created June 7, 2011 16:35
Rails initializer for using ActiveAdmin with Sass on Heroku
if Rails.env.production?
require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets", "admin"))
template_path_one = "#{Gem.loaded_specs['activeadmin'].full_gem_path}/app/assets/stylesheets"
template_path_two = "#{Gem.loaded_specs['activeadmin'].full_gem_path}/lib/active_admin/sass"
old_compile_path = "#{Rails.root}/public/stylesheets/admin"
new_compile_path = "#{Rails.root}/tmp/stylesheets/admin"
Sass::Plugin::remove_template_location template_path_one
@hoverlover
hoverlover / routes.rb
Created October 25, 2011 04:47
Map a scoped resource to a controller with an unrelated namespace
Sample::Application.routes.draw do
# Route /a/b -> Foo::BarsController. This is useful
# when you need to conform to a certain url structure
# while using a controller that doesn't match that structure,
# eg. a controller supplied by a Rails engine.
scope '/a' do
resources :b, controller: 'bars', module: 'foo'
end
end
@hoverlover
hoverlover / install-ruby-debug-ruby-1.9.3.sh
Created November 23, 2011 18:24 — forked from boriscy/install-ruby-debug-ubuntu-ruby-1.9.3
ruby-debug in ruby-1.9.3 and rbenv
# These instructions are for Ruby 1.9.3 under rbenv.
# To install ruby-debug19 for ruby-1.9.3 you need to download the following gems from http://rubyforge.org/frs/?group_id=8883
linecache19-0.5.13.gem
ruby_core_source-0.1.5.gem
ruby-debug19-0.11.6.gem
ruby-debug-base19-0.11.26.gem
#Then in your console
@hoverlover
hoverlover / auto_save_and_open_page.rb
Created February 16, 2012 21:18
Auto save_and_open_page on failure for Capybara
module Capybara
def Capybara.auto_save_and_open_page=(val)
@auto_save_and_open_page = !!val
end
def Capybara.auto_save_and_open_page?
@auto_save_and_open_page ||= false
end
end
@hoverlover
hoverlover / savon_client.rb
Created March 19, 2012 14:45
Using my forks of Akami and Savon to sign a request with a X.509 certificate. See https://github.com/genuitytech/akami and https://github.com/genuitytech/savon.
client = Savon::Client.new do
# This can be a URL also
wsdl.document = "/Path/to/your.wsdl"
# These are optional, only if your WSDL sucks :)
wsdl.endpoint = "https://your_endpoint"
wsdl.namespace = "http://your_namespace"
certs = Akami::WSSE::Certs.new :cert_file => "/path/to/cert.crt", :private_key_file => "/path/to/private/key.pem", :private_key_password => "password"
wsse.sign_with = Akami::WSSE::Signature.new certs