Skip to content

Instantly share code, notes, and snippets.

View jamesmartin's full-sized avatar
🤫
Shhh

James Martin jamesmartin

🤫
Shhh
View GitHub Profile
@jamesmartin
jamesmartin / invisibility_spec.rb
Created February 17, 2011 23:04
Testing the Invisibility superpower by creating a sort of dumb target class 'Person' to give the power to in the test.
module Invisibility
def activate
@visible = false
end
def deactivate
@visible = true
end
end
@jamesmartin
jamesmartin / sleepy_spec.rb
Created February 22, 2011 11:07
Stubbing sleep
# example borrowed from Capybara.timeout
class Sleepy
def self.do_this(seconds = 1, &block)
start_time = Time.now
result = nil
until result
return result if result = yield
@jamesmartin
jamesmartin / cm_subscribers_multi_value_custom_field_example.rb
Created March 2, 2011 01:04
Adding custom fields with multiple values to a subscriber
require 'rubygems'
require 'json'
custom_fields = [
{ :Key => 'memstatus', :Value => "{account_status}"},
{ :Key => 'memtype', :Value => "{user.member_type}"},
{ :Key => 'memsincedate', :Value => "{member_since.to_date}"},
{ :Key => 'memrenewaldate', :Value => "{user.account.renewal_date.to_date}"},
{ :Key => 'usertype', :Value => "{user_type}"},
#{ :Key => 'neighborhood group', :Value => "#{neighborhood_groups.join('||')}", :DataType => 'Multi-Options'},
$:.unshift(File.dirname(__FILE__), ".")
require 'spec_helper'
module Butler
def self.included base
base.instance_eval { @place = [] }
end
def put_fork_on_the_left
@jamesmartin
jamesmartin / bar.rb
Created March 28, 2011 12:07
Modular sinatra that serves static content from within the '/bar' app
require 'rubygems'
require 'sinatra/base'
class Bar < Sinatra::Base
set :static, true
set :public, File.join(File.dirname(__FILE__), 'bar/_site/')
get '' do
@jamesmartin
jamesmartin / app.rb
Created April 18, 2011 23:01
Middleware that rewrites every image to be 'The Best Image on the Internet'
require 'rubygems'
require 'sinatra'
Sinatra::Base.inline_templates = true
class App < Sinatra::Base
get '/' do
erb :index
end
@jamesmartin
jamesmartin / getCurrentMinute.js
Created August 23, 2011 06:49
File this one in the dumb bucket.
parseInt(Date().split(" ")[4].split(":")[1]);
@jamesmartin
jamesmartin / account_holder.rb
Created March 8, 2012 07:13
An ATM cash withdrawal scenario, using CukeSalad to focus on Roles, Tasks and Actions
class Account
def initialize
@balance = 0
end
def deposit amount
@balance += amount.as_money
end
def withdraw amount
@jamesmartin
jamesmartin / zip.rb
Created March 9, 2012 06:22
The Ruby Enumerable#zip method (matrix transform), in Ruby
require 'minitest/autorun'
class Array
def zzzip(*args)
results = []
i = 0
each do |ary_element|
zipped_entry = args.inject([ary_element]) do |memo, element|
memo << element[i]
end
@jamesmartin
jamesmartin / scrape.rb
Created April 5, 2012 09:35
Little website scraping example
require 'rubygems'
require 'nokogiri'
require 'httparty'
require 'uri'
require 'pp'
class HtmlParserIncluded < HTTParty::Parser
SupportedFormats.merge!('text/html' => :html)
def html