Skip to content

Instantly share code, notes, and snippets.

View bschwartz's full-sized avatar

Brendan Schwartz bschwartz

View GitHub Profile
#
# Put this in a directory and create a `Gemfile` with this in it:
#
# source 'https://rubygems.org'
# gem 'nsq-cluster'
# gem 'nsq-ruby'
#
# Then run `bundle install` to install the require gems.
# Then run this like so: `ruby fin-failer.rb`
#
@bschwartz
bschwartz / multitopic.rb
Created November 11, 2014 13:52
Consume from multiple topics into a single queue
require 'nsq'
require 'thread'
queue = Queue.new
consumer_for_topic1 = Nsq::Consumer.new(
topic: 'topic1',
channel: 'multi-topic-consumer',
queue: queue
)
# If you've ever worked with file uploads in Rails, you've probably realized that Rails
# handles uploads with a filesize under 10K differently than files over 10K.
# I prefer things to be as similar as possible so I wrote this quick monkey patch.
# Drop it in your config/initializers directory and you're off to the races.
#
# You should also check out Paperclip, acts_as_attachment, file_column, etc. because
# they take care of all this business for you.
@bschwartz
bschwartz / gist:63920
Created February 13, 2009 14:17
Easily loop through AR database records in chunks. Great for migrations.
# If you're doing migrations or work on large datasets,
# you don't want to do a Record.find(:all).each.
#
# Why you ask? Well, unless you have gobs of system memory, your system
# will be paging like gangbusters when it starts loading the entire db table
# into memory as AR objects.
#
# To use this just drop this file into the /lib dir of your Rails project and
# you'll be able to use it in any migration
#
# Adjust sessions so they work across subdomains
# This also will work if your app runs on different TLDs
# from: http://szeryf.wordpress.com/2008/01/21/cookie-handling-in-multi-domain-applications-in-ruby-on-rails/
# modified to work with Rails 2.3.0
module ActionControllerExtensions
def self.included(base)
base::Dispatcher.send :include, DispatcherExtensions
end
@bschwartz
bschwartz / full-scrub.js
Created November 6, 2014 18:15
Scrub all query params with FreshURL
FreshUrl.waitsFor(function(){freshUrl.allReady()}).then(function(){
// This will fire once all analytics platforms are ready
// Scrub everything in the query string, not just utm_*
window.history.replaceState({}, '', window.location.pathname + window.location.hash);
});
<!-- Begin MailChimp Signup Form -->
<!--[if IE]>
<style type="text/css" media="screen">
#mc_embed_signup fieldset {
position: relative;
}
#mc_embed_signup legend {
position: absolute;
top: -1em;
left: .2em;
// This is a jQuery monkey-patch that changes all PUT and DELETE requests
// into POST requests and sets a "_method" param to the original request method.
//
// This is a workaround for the fact that some corporate networks and
// environments don't allow PUT and DELETE requests.
//
// This whole thing works because Rails implements the _method hack.
(function(){
var ajaxWithoutMethodHack = jQuery.ajax;
jQuery.ajax = function( s ) {
# if this is for a Rails app, drop it in /config/initializers
class Module
if defined?(:rake_original_const_missing)
def const_missing(const)
rake_original_const_missing(const)
end
end
@bschwartz
bschwartz / vswap.wistia-tracker.js
Created May 6, 2010 16:06
Track Wistia videos using Vertster
/*
* Tracks a video's play progress via Vertster
*
* Usage:
*
* Vswap.trackWistiaVideo(560, 'wistia_123', 63.0);
*
* Note: This currently only works with a Wistia video embed but could be easily adapted to
* work with YouTube or other video players.
*/