Skip to content

Instantly share code, notes, and snippets.


Alvin Liang aliang

View GitHub Profile
aliang / Mac SSH Autocomplete
Created Jun 14, 2011
Add auto complete to your ssh, put into your .bash_profile
View Mac SSH Autocomplete
_complete_ssh_hosts ()
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
aliang / application.rb
Created Jun 12, 2011
monkeypatch Rails::Application.load_console to load app-specific .irbrc
View application.rb
# taken from
# config/application.rb that is
module MyApp
class Application < Rails::Application
# config stuff comes here
def load_console(sandbox=false)
if File.exists?(project_specific_irbrc = File.join(Rails.root, ".irbrc"))
puts "Loading project specific .irbrc ..."
aliang / mandrill_signature_verifier.rb
Last active Dec 17, 2019
Mandrill webhook verifier, in Ruby
View mandrill_signature_verifier.rb
class MandrillSignatureVerifier
def initialize(key, url, params, signature)
@key = key
@url = url
@params = params
@signature = signature
# Return true if the signature matches
def verified?
aliang / mymodel.rb
Created Jun 13, 2011
render from model in Rails 3
View mymodel.rb
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
View virtus-multiparameter_attributes.rb
# Rails datetime_select and similar use multiparameter attributes which are
# these dawful things from the bowels of activerecord and actionpack. This
# module extends virtus models to coerce multiparameter attributes back together
# before assigning attributes.
# Here's the implementation for ActiveRecord:
# and DataMapper:
aliang / custom_headers.rb
Created Mar 9, 2010
setting custom headers with ruby net/http
View custom_headers.rb
# this was surprisingly difficult to find, the documentation and API is poor
require "net/http"
require "uri"
url = URI.parse("")
req =
req.add_field("X-Forwarded-For", "")
# For content type, you could also use content_type=(type, params={})
aliang / shared_examples_for_active_model.rb
Created Jul 7, 2016
ActiveModel::Lint tests for MiniTest 5
View shared_examples_for_active_model.rb
shared_examples_for "ActiveModel" do
require 'minitest/assertions'
require 'active_model/lint'
include MiniTest::Assertions
include ActiveModel::Lint::Tests
# This is the main difference between MiniTest 4 and 5.
# We must keep a counter of assertions.
attr_accessor :assertions
aliang / parse_query.rb
Created Jun 20, 2011
search query string parser, google-ish
View parse_query.rb
# via
def parse_query s
[nil, :prefix, :plusminus, nil, nil, :phrase, :word].zip(match).select(&:all?)
aliang / reserved_name_validator.rb
Created Dec 30, 2010
validate reserved names in Rails 3
View reserved_name_validator.rb
# app/validators/reserved_name_validator.rb
class ReservedNameValidator < ActiveModel::EachValidator
about account add admin api
app apps archive archives auth
config connect contact create
delete direct_messages downloads
edit email
faq favorites feed feeds follow followers following
aliang / publicize_methods.rb
Created Dec 12, 2013
Expose private methods for testing. Can accept a block, or just call in RSpec before/after blocks
View publicize_methods.rb
class Class
def publicize_methods
@_saved_private_instance_methods = self.private_instance_methods
self.class_eval { public *@_saved_private_instance_methods }
if block_given?