Skip to content

Instantly share code, notes, and snippets.

View aliang's full-sized avatar

Alvin Liang aliang

View GitHub Profile
@aliang
aliang / Mac SSH Autocomplete
Created June 14, 2011 07:14
Add auto complete to your ssh, put into your .bash_profile
_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
@aliang
aliang / stream_itunes_over_ssh.sh
Created June 12, 2011 08:39
stream iTunes over SSH (requires dns-sd)
# Shamelessly taken from http://blog.iharder.net/2009/09/28/itunes-stream-itunes-over-ssh/
# Your remote library will appear in your local iTunes as a shared library
# You need an ssh server running on the remote library's computer
#!/bin/sh
dns-sd -P "Home iTunes" _daap._tcp local 3689 localhost.local. \
127.0.0.1 "Arbitrary text record" &
PID=$!
ssh -C -N -L 3689:localhost:3689 yourhost.dyndns.org
kill $PID
@aliang
aliang / application.rb
Created June 12, 2011 08:12
monkeypatch Rails::Application.load_console to load app-specific .irbrc
# taken from http://railsbros.de/2010/11/29/a-custom-irbrc-for-rails-3.html
# config/application.rb that is
module MyApp
class Application < Rails::Application
# config stuff comes here
def load_console(sandbox=false)
super
if File.exists?(project_specific_irbrc = File.join(Rails.root, ".irbrc"))
puts "Loading project specific .irbrc ..."
@aliang
aliang / expr.py
Created January 10, 2011 23:01
expr template tag for evaluating arbitrary python in django templates
# app/common/templatetags/expr.py
from django import template
from django.utils.translation import gettext_lazy as _
from django.conf import settings
import re
register = template.Library()
class ExprNode(template.Node):
@aliang
aliang / mandrill_signature_verifier.rb
Last active December 17, 2019 22:38
Mandrill webhook verifier, in Ruby
class MandrillSignatureVerifier
def initialize(key, url, params, signature)
@key = key
@url = url
@params = params
@signature = signature
end
# Return true if the signature matches
def verified?
@aliang
aliang / mymodel.rb
Created June 13, 2011 06:25
render from model in Rails 3
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
# 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:
#
# https://github.com/rails/rails/blob/11fd052aa815ae0255ea5b2463e88138fb3fec61/activerecord/lib/active_record/attribute_assignment.rb#L113-L218
#
# and DataMapper:
@aliang
aliang / custom_headers.rb
Created March 9, 2010 17:25
setting custom headers with ruby net/http
# this was surprisingly difficult to find, the documentation and API is poor
require "net/http"
require "uri"
url = URI.parse("http://www.whatismyip.com/automation/n09230945.asp")
req = Net::HTTP::Get.new(url.path)
req.add_field("X-Forwarded-For", "0.0.0.0")
# For content type, you could also use content_type=(type, params={})
@aliang
aliang / shared_examples_for_active_model.rb
Created July 7, 2016 18:47
ActiveModel::Lint tests for MiniTest 5
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
aliang / parse_query.rb
Created June 20, 2011 22:00
search query string parser, google-ish
# via http://jablan.radioni.ca/post/4982485974/parsing-search-query-in-ruby
def parse_query s
s.scan(/((\S+)\:\s?)?([+-])?(("(.+?)")|(\S+))/).map{|match|
Hash[
[nil, :prefix, :plusminus, nil, nil, :phrase, :word].zip(match).select(&:all?)
]
}
end