Skip to content

Instantly share code, notes, and snippets.


Alvin Liang aliang

View GitHub Profile
aliang / gist:263863
Created Dec 26, 2009 — forked from retr0h/gist:98308
content type handling for sinatra
View gist:263863
CONTENT_TYPES = {:html => 'text/html', :css => 'text/css', :js => 'application/javascript'}
before do
# instead of using case here, metaprogram it
request_uri = case request.env['REQUEST_URI']
when /\.css$/ : :css
when /\.js$/ : :js
else :html
content_type CONTENT_TYPES[request_uri], :charset => 'utf-8'
View gist:308372
function delegateHandler(e){
var element = e.element(), elements = element.ancestors ? element.ancestors().concat([element]) : [element];
((Element.retrieve(this, 'prototype_delegates') || $H()).get(e.eventName || e.type) || []).each(function(pair){
if (element = Selector.matchElements(elements, pair.key)[0])
pair.value.invoke('call', element, e);
function delegate(element, selector, event, handler){
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 / gist:340915
Created Mar 23, 2010 — forked from RStankov/gist:162593
bubble and delegate focus, blur, change with prototype
View gist:340915
function focusInHandler(e){
function focusOutHandler(e){
if (document.addEventListener){
document.addEventListener("focus", focusInHandler, true);
View heroku_deploy.rake
# List of environments and their heroku git remotes
:staging => 'myapp-staging',
:production => 'myapp-production'
namespace :deploy do
ENVIRONMENTS.keys.each do |env|
desc "Deploy to #{env}"
task env do
aliang / prototype-within-viewport.js
Created Aug 1, 2010
methods for seeing if something is in the viewport. see also
View prototype-within-viewport.js
Element.Methods.centerIsWithinViewport = function(element) {
var dim = document.viewport.getDimensions();
var so = document.viewport.getScrollOffsets();
var co = element.cumulativeOffset();
var edim = element.getDimensions();
// x and y measured from upper left
var center = {
x: co.left + (edim.width / 2),
y: + (edim.height / 2)
View partials.rb
# stolen from
# and made a lot more robust by me
# this implementation uses erb by default. if you want to use any other template mechanism
# then replace `erb` on line 13 and line 17 with `haml` or whatever
module Sinatra::Partials
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
View share random song from your itunes library in dropbox
require 'fileutils'
dirs = Dir['path_to_your_root_music_directory/**/*.*']
file = dirs[rand(dirs.size)]
end until File.file?(file)
# could check to make sure your dropbox folder is mounted/exists here
# remove all files from this dropbox folder
View .gitignore
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