Skip to content

Instantly share code, notes, and snippets.

Avatar

Myron Marston myronmarston

View GitHub Profile
@myronmarston
myronmarston / heroku.rake
Created Dec 18, 2009
Rake task to download a sql dump from a heroku app
View heroku.rake
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] }
View capybara_case_insensitivity.rb
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}"
View Environment
~/projects/dgnetwork[bundler]% ruby --version
ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0]
View Gemfile
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'
View haml_support.rb
# 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
View rack_test_hack.rb
# 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)
View Seattle_WA.yml
---
- !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:
View alias_method_chain_pollution.rb
>> 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
View proc_vs_lambda.rb
>> 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'
View document_analyzer.rb
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
You can’t perform that action at this time.