Skip to content

Instantly share code, notes, and snippets.

View jqr's full-sized avatar

Elijah Miller jqr

View GitHub Profile
$ strobe deploy --production
This computer is not yet registered with a Strobe account.
1. login
2. signup
? 1
Please enter your Strobe username and password.
Email: elijah.miller@gmail.com
Password:
Unfortunately, a fatal error has occurred. Please report this error to Strobe Support at http://support.strobeapp.com/home so that we can fix it. Thanks!
/usr/local/strobe/bundle/ruby/1.9.1/gems/activesupport-3.0.10/lib/active_support/json/backends/yajl.rb:12:in `parse': input must be a string or IO (Yajl::ParseError)
@jqr
jqr / setup-statsd.sh
Created July 6, 2011 02:04 — forked from howardr/setup-statsd.sh
Minor changes to statsd installation
# install git
sudo apt-get install -y g++ curl libssl-dev apache2-utils
sudo apt-get install -y git-core
mkdir ~/src
cd ~/src
# download the Node source, compile and install it
git clone https://github.com/joyent/node.git
cd node
# Adds rel="nofollow" and auto_link class to all links by default.
def auto_link(text, options = {}, &block)
super(text, options.reverse_merge(:link => :all, :html => { :rel => :nofollow, :class => 'auto_link' }), &block)
end
# Returns a link to url with the specified content, automatically adds
# rel="nofollow" and the external class to the link.
def link_to_external(content, url, options = {})
url = httpify_url(url)
link_to content, url, options.merge(:rel => :nofollow, :class => "#{options[:class].to_s} external")
end
# Just like link_to_external, but uses the dehttpified url as the content.
def link_to_external_url(url, options = {})
link_to_external(h(dehttpify_url(url)), url, options)
# Including this module causes indexing and index removal to occur in
# DelayedJobs. Requires all automatic index/removal code to be enabled (the
# default) to work properly. Include this module after calling searchable.
#
# searchable do # TODO: automatically enable after calling searchable?
# text :name
# end
#
# include DelayedIndex
module DelayedIndex
module FindDuplicates
# Returns a hash of duplicate values and their counts, select can be
# anything that evaluates to a single SQL column.
# Warning: select and minimum_count are not SQL escaped!
#
# User.find_duplicates(:username) # { 'jqr' => 2 }
# User.find_duplicates(LOWER(city), 5) # { 'indianaplis' => 25, 'new york' => 201 }
def find_duplicates(select, minimum_count = 2)
rows = connection.select_all("SELECT #{select} AS value, COUNT(*) AS count FROM #{table_name} GROUP BY #{select} HAVING COUNT(*) >= #{minimum_count}")
rows.inject({}) do |acc, row|
# Warning: Loses any scope it may have been called on!
def send_all_in_batches(batch_size, method, *args)
transaction do
start_id = first(:order => 'id ASC').id
end_id = first(:order => 'id DESC').id
current_id = start_id
while current_id <= end_id
send_later :send_to_batch, current_id, current_id + batch_size, method, *args)
current_id += batch_size
end
# See install instructions @ http://github.com/mynyml/harmony
require 'rubygems'
require 'harmony'
$: << '/Library/Ruby/Gems/1.8/gems/harmony-0.5.1/vendor/envjs/lib'
page = Harmony::Page.fetch('http://example.com')
puts page.execute_js('document.title') #=> "Example Web Page"
# http://rubylearning.com/blog/2010/01/26/rpcfn-fair-distribution-6/
module Enumerable
def sum
inject(0) { |acc, v| acc + v }
end
end
class FairDistribution
attr_accessor :jobs, :worker_count, :distribution
# Useful in JRuby environment where Timeout and SystemTimer are not working as expected.
class ThreadedTimeout
def self.realtime(timeout, options = {})
resolution = options[:resolution] || 1
begin
start = Time.now
resolution ||= 1
mutex = Mutex.new
thread = Thread.new do
mutex.synchronize do