Skip to content

Instantly share code, notes, and snippets.

View lmars's full-sized avatar

Lewis Marshall lmars

View GitHub Profile
@lmars
lmars / redirect_to_matcher.rb
Created September 5, 2012 21:00
Sinatra based domain redirect app
class RedirectTo
def initialize(expected, session)
@expected = expected
@session = session
end
def matches?(visit_proc)
instance_eval(&visit_proc)
@status = @session.status_code
@lmars
lmars / cap.sh
Created September 21, 2012 10:33
Capistrano growlnotify
cap() {
bundle exec cap $@
if [ $? -eq 0 ]; then
growlnotify --image=$HOME/Pictures/success.png -m 'Success!' "cap $@"
else
growlnotify --image=$HOME/Pictures/failure.png -m 'Failure!' "cap $@"
fi
}
@lmars
lmars / spec_helper.rb
Created January 14, 2013 10:52
Capybara feature caching
before(:each, :capybara_feature => true)
@cache_store = ActionController::Base.cache_store
ActionController::Base.perform_caching = true
ActionController::Base.cache_store = :memory_store
end
after(:each, :capybara_feature => true)
ActionController::Base.perform_caching = false
ActionController::Base.cache_store = @cache_store
end
#
# start of solution
#
MyString = Class.new(String)
MyString.define_singleton_method(:alphabet) { 'abc' }
MyString.class_eval {
exclaim_method = proc { |*args|
count = args.first || 1
@lmars
lmars / encrypted_zip.rb
Created January 23, 2013 10:22
Create a CSV in memory then add it to an encrypted zip file
require 'zipruby'
require 'csv'
csv = CSV.generate do |csv|
csv << ['i', '2i']
1.upto(10) do |i|
csv << [i, i*2]
end
end
@lmars
lmars / memoize.rb
Created April 17, 2013 10:34
Memoize example
def foo(*args)
@memoized_foo ||= {}
@memoized_foo[args] ||= begin
some_expensive_method
end
end
@lmars
lmars / 00_sync.rb
Last active December 18, 2015 10:29
require 'net/http'
require 'uri'
random_url = URI('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new')
ints = []
resp = Ne
@lmars
lmars / resque.rake
Created November 5, 2013 13:39
Resque setup
namespace :resque do
task :setup do
ENV["MY_CUSTOM_KEY"] = "MY CUSTOM VALUE"
end
end
@lmars
lmars / dump.rb
Last active December 29, 2015 10:19
Dump top ten Ruby objects by count every minute
Thread.start do
loop do
objects = Hash.new(0)
ObjectSpace.each_object { |obj| objects[obj.class] += 1 }
File.open("/tmp/top_ten_objects.txt", "a") do |file|
file.puts "-" * 50
file.puts "PID: #{Process.pid} - #{Time.now.strftime("%c")}"
file.puts "-" * 50
@lmars
lmars / Gemfile
Last active December 30, 2015 14:39
Async rack app to fetch giphy gifs
source "https://rubygems.org"
gem "em-http-request"
gem "eventmachine"
gem "json"
gem "rack"
gem "thin"