Skip to content

Instantly share code, notes, and snippets.

View subelsky's full-sized avatar
🧘‍♂️

Mike Subelsky subelsky

🧘‍♂️
View GitHub Profile
@subelsky
subelsky / null_zero_obj.rb
Created July 1, 2011 20:51
using a null zero object
# I have a function that looks up a monthly report which may or may not exist yet (e.g. if
# you look at the start of a month, before I've gotten around to generating that month's
# report)
#
# where the report isn't generated I just want to return "0" for each column in this report;
# so what I do is return an instance of the below object whenever there's no report
# available (thus avoiding any "try" unpleasantness) - something about this slightly
# bothers me though. Is there a more elegant/simpler way I should consider?
class NullZeroObject
@subelsky
subelsky / Gemfile
Created July 28, 2011 14:17
Getting SSL, Capybara, Rails 3, and Devise to work together
gem "rack-ssl", "1.3.2"
# when Capybara issue 409 or 422 get resolved, you can switch back to the official
# capybara gem
# https://github.com/jnicklas/capybara/pull/409
# https://github.com/jnicklas/capybara/pull/422
gem "capybara", git: "https://github.com/mcolyer/capybara.git", branch: "fix-ssl-redirects"
@subelsky
subelsky / audio.rake
Created November 4, 2011 14:15
Rake task to prepare MP3 files for Apple's HTTP Streaming protocol and upload them to S3
namespace :audio do
desc "downsample, segment, and upload audio files"
task :load => :environment do
access_key_id = ENV['AWS_ACCESS_KEY_ID']
secret_access_key = ENV['AWS_ACCESS_KEY']
source_dir = ENV['SOURCE_DIR']
fail "Must specify AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY, and SOURCE_DIR" if access_key_id.blank? || secret_access_key.blank? || source_dir.blank?
filenames = Dir["#{source_dir}/**/**.*"]
fail "#{source_dir} is empty" if filenames.empty?
@subelsky
subelsky / gist:2227323
Created March 28, 2012 15:29
Parsing surveymonkey result data
html = IO.read("survey.html")
rows = Nokogiri::HTML(html).css("tr")[????].collect { |r| r.children.to_a.values_at(0,3).collect(&:content) }.sort_by(&:last).reverse.collect { |r| "#{r.first} - #{r.last}" }
@subelsky
subelsky / migration_generator.rb
Created April 12, 2012 17:49
Open Rails migrations in an editor automatically after creation
# As soon as I create a Rails migration I always want to open that file in an editor.
# I'd like to submit the following patch to Rails to create an --editor argument
# for the migration generator but I can't quite figure out the Thor way of doing this.
require 'rails/generators/active_record'
module ActiveRecord
module Generators
class MigrationGenerator < Base
argument :editor, :type => :string, :lazy_default => ENV['EDITOR'], :required => false, :banner => "/path/to/your/editor", :desc => "Open migration using specified editor (defaults to EDITOR)"
@subelsky
subelsky / fetcher.rb
Created May 3, 2012 13:29
Apprentice Homework Assignment
# Two of the projects we'll be discussing are heavily dependent on code like the example below, which
# requests stock price information from Google's finance API and transforms the results into a hash
# for use in our database. This pattern shows up over and over in my projects. I always setup a separate
# codebase containing a bunch of worker" modules that just do stuff like this.
#
# I need to write a bunch more of these for a variety of data sources. My plan is to start you off
# on these, then graduate to more complex features.
#
# Here are two things for you to try:
#
@subelsky
subelsky / massive_update.sh
Created May 25, 2012 12:03
A thing I couldn't do w/o good test coverage
# I run this command once a week on all of my active projects
# It helps ensure that I'm always able to use the latest/greatest
# versions of various dependencies, or at least that I find out
# quickly when I've become locked into a particular version
# (which just happened with the latest versions of the Ruby gems
# Webmock and VCR
alias mrupd='git pull && bundle update && rdbm && rspec spec; sd'
# the alias stands for 'massive rails update' because I have a bunch of other aliases that
@subelsky
subelsky / rsh_topics_6-9-12.rb
Created June 9, 2012 16:52
Ruby Study Hall outline 6/9/12
# BEGINNER: how to get started with ruby
# http://www.rubystudyhall.com/getting_started_with_ruby
# http://tryruby.org
# irb
# run "Hello, World" from command line
# do simple calculations
# read a file and count the lines in it
# read a file from command line argument
@subelsky
subelsky / rspec_shared_controller_spec_example.rb
Created July 3, 2012 17:33
RSpec shared example tests for controllers
# how to use https://github.com/subelsky/subelsky_power_tools/blob/master/lib/subelsky_power_tools/controller_shared_behavior.rb
describe PlatformsController do
it_should_behave_like "a protected plural controller" do
let(:user) { create(:user) }
let(:object_not_owned_by_user) { create(:platform) }
let(:valid_change_params) { { platform: { username: "HEYWOOD" } } }
let(:user_sign_in_proc) { lambda { sign_in user } }
let(:signin_redirect) { new_user_session_url(protocol: "http") }
let(:not_found_redirect) { root_url }
@subelsky
subelsky / imagemagick_snippets.sh
Created August 20, 2012 14:45
Useful ImageMagick snippets
# Flatten a transparent image with a white background:
convert -flatten img1.png img1-white.png
# Make an image transparent
convert -transparent '#FFFFFF' nontransparent.gif transparent.png
# convert an image into tiles
convert -size 3200x3200 tile:single_tile.png final.png
# making a montage from a collection of images