Skip to content

Instantly share code, notes, and snippets.

@myronmarston
myronmarston / heroku.rake
Created December 18, 2009 23:48
Rake task to download a sql dump from a heroku app
namespace :heroku do
def app_name
@app_name ||= Heroku::Command::BaseWithApp.new([]).app
end
def latest_bundle(timeout = 30)
puts "Attempting to get latest bundle..."
get_bundle = lambda do
bundles = Heroku::Command.run('bundles', {})
bundles.sort { |b1, b2| b1[:created_at] <=> b2[:created_at] }
def find_element(xpath_expression, text, case_insensitive = false)
exp = if case_insensitive
xpath_expression % ["translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvqxyz')", text.downcase]
else
xpath_expression % ["text()", text]
end
unless elem = find(exp)
if case_insensitive
raise "Failed to find element using locator #{text}"
~/projects/dgnetwork[bundler]% ruby --version
ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0]
source :gemcutter
gem 'rails', '2.3.5'
gem 'httparty'
gem 'mad_mimi_mailer', '0.0.8'
gem 'fastercsv'
gem 'crummy'
gem 'prawn'
gem 'barby'
gem 'friendly_id'
# The idea for this technique taken from: http://blog.wolfman.com/articles/2007/07/14/using-rspec-to-test-haml-helpers
module HelperHamlSupport
def new(*args, &block)
super.tap do |obj|
obj.extend Haml
obj.extend Haml::Helpers
obj.init_haml_helpers
end
end
end
# Rack::Test doesn't support hitting external webservers. However, when a user enters their credit card info,
# it is submitted directly to braintree, so we need to intregration test with hitting an external server.
# This hack enabled this, by swapping out the Rack::Test's rack app with a Rack::Client instance.
# Rack::Client acts just like a rack app, but under the covers it delegates to Net::HTTP.
require 'rack/client'
module RackTestHack
def new(*a, &b)
---
- !ruby/struct:VCR::HTTPInteraction
request: !ruby/struct:VCR::Request
method: :get
uri: http://maps.google.com:80/maps/geo?q=Seattle%2C+WA&output=xml&key=ABQIAAAAM1mKqHK9XdhE_YEkQ7oqnhR8EBP7TfhOOIrsLnV5Hk7LABXvFhRRL3Y8fJfBk0KFeIkQgKLdCVcFCQ&oe=utf-8
body:
headers:
accept:
- "*/*"
host:
>> puts User.first.methods.select { |m| m =~ /_with(out)?_/ }.sort
destroy_with_callbacks
destroy_with_transactions
destroy_without_callbacks
destroy_without_transactions
extend_with_included_modules_from
load_with_new_constant_marking
reload_with_autosave_associations
reload_with_dirty
reload_without_autosave_associations
>> p = Proc.new { |a, b| a }
=> #<Proc:0x00000001036962f8@(irb):2>
>> p.call(5)
=> 5
>> l = lambda { |a, b| a }
=> #<Proc:0x000000010368e8c8@(irb):4>
>> l.call(5)
ArgumentError: wrong number of arguments (1 for 2)
from (irb):4
from (irb):5:in `call'
class DocumentAnalyzer
SPLIT_STRATEGY = lambda { |doc| doc.split(' ').size }
def initialize(document)
@document = document
end
def word_count(counting_strategy = SPLIT_STRATEGY)
counting_strategy.call(@document)
end