Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / import.sh
Last active August 24, 2018 08:50
Capture a Heroku PostgreSQL backup, download it, and restore it to your local development database.
#!/bin/sh
set -e
app=$1
local_db=${app}_development
# Create the backup on heroku
heroku pgbackups:capture --expire
@stevenharman
stevenharman / devise_mail_helpers.rb
Created August 14, 2013 02:23
Extract Devises "raw" tokens from outgoing email, since the tokens stored in the DB have been digested. (As for Devise 3.1)
module Features
module MailHelpers
def last_email
ActionMailer::Base.deliveries[0]
end
# Can be used like:
# extract_token_from_email(:reset_password)
def extract_token_from_email(token_name)
@stevenharman
stevenharman / email_validator.rb
Last active December 20, 2015 11:59
A really robust (but still likely not full-spec compliant) email validator for ActiveModel.
# encoding: binary
require 'active_model/validator'
class EmailValidator < ActiveModel::EachValidator
EmailAddress = begin
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
@stevenharman
stevenharman / cmd-enter.js
Last active September 18, 2017 22:13
⌘-ENTER: submit that form!
$(document).on('keydown', 'form :input:not(:disabled)', function(e) {
if(e.keyCode == 13 && (e.metaKey || e.ctrlKey)) {
$form = $(this).parents('form')
if ($form[0].checkValidity()) {
$form.submit();
}
}
});
@stevenharman
stevenharman / NSObject+Tap.h
Created June 12, 2013 18:16
A `tap` method for Objective-C, as inspired by Ruby. Thanks, @coreyhaines.
#import <Foundation/Foundation.h>
@interface NSObject (Tap)
- (id)tap:(void (^)(id))block;
@end
@stevenharman
stevenharman / defaults.rb
Last active July 7, 2021 14:36
A subtle difference between Ruby's Hash.fetch(:key, :default) vs. (Hash[:key] || :default)
h = {
'a' => :a_value,
'b' => nil,
'c' => false
}
h.fetch('a', :default_value) #=> :a_value
h.fetch('b', :default_value) #=> nil
h.fetch('c', :default_value) #=> false
h.fetch('d', :default_value) #=> :default_value
@stevenharman
stevenharman / supply_chain_log.rb
Last active December 16, 2015 01:38
A simple, purpose-built, logging abstraction used while importing BreweryDB.com data into Brewdega.
require 'delegate'
module SupplyChain
class Log < SimpleDelegator
def initialize(log = Rails.logger)
super(log)
end
def record(item)
@stevenharman
stevenharman / ruby-wall-times.md
Created April 9, 2013 23:08
I thought Ruby 2.0 was supposed to be faster than 1.9?

First with Ruby 1.9.3-p392

brewhouse: ~/code/brewdega/cellar [1.9.3-p392|master]
☺ $ time be rspec
................................................................................................................................................................................................................................................................

Finished in 8.7 seconds
256 examples, 0 failures
@stevenharman
stevenharman / git-noton
Last active December 15, 2015 11:59 — forked from searls/git-unmaster
A git command for logging all the commits on the current branch that are not on another branch as of your nearest common ancestor. Additional git-log flags come along for the ride (e.g. `git unmaster --oneline`)
#!/bin/sh
if [ $# -eq 0 ]; then
echo "Please specify the base branch"
exit 1
fi
BASEBRANCH=$1
shift
@stevenharman
stevenharman / brewery.json
Created March 9, 2013 18:07
Fetching a brewery from BreweryDB.com with location and social accounts. I'm seeing the location data interleaved with the social accounts. Just me?
// GET /brewery/NcvyGD?withAlternateNames=Y&withLocations=Y&withSocialAccounts=Y
{
"data":{
"id":"NcvyGD",
"name":"Hawai'i Nui Brewing",
"description":"At Hawai\u2018i Nui Brewing, we\u2019re passionate about craft beer. We brew small batches of our all-natural ales and lagers from the finest barley malt, wheat, hops and yeast. We are committed to local production in Hawai\u2018i. \r\n\r\nOur uniquely well balanced Island Style beer is perfect for your time in Paradise!\r\n\r\nWhat is Island Style? To those of us who call Hawai\u2019i home, it\u2019s all about balance. Working hard, sure. But when pau hana time comes, it\u2019s about friends and family. Heeding the call of the sun and sand, surf and sea. Tasting freshness. Celebrating music. Stopping at sunset. Resting easy at night. Appreciating life\u2019s detours\u2013what we call holo holo time. Living life as it should be.",
"website":"http:\/\/www.hawaiinuibrewing.com\/",
"established":"2007",
"isOrganic":"N",