Skip to content

Instantly share code, notes, and snippets.

View aliang's full-sized avatar

Alvin Liang aliang

View GitHub Profile
@aliang
aliang / installation.sh
Created February 18, 2012 22:39 — forked from mikhailov/installation.sh
Nginx+passenger application config: ssl redirection, http headers, passenger optimal settings. see details: http://mikhailov.posterous.com/nginx
$ cd /usr/src
$ wget http://nginx.org/download/nginx-0.8.52.tar.gz
$ tar xzvf ./nginx-0.8.52.tar.gz
$ rm ./nginx-0.8.52.tar.gz
$ gem install s3sync capistrano capistrano-ext passenger --no-ri --no-rdoc
$ passenger-install-nginx-module
# Automatically download and install Nginx? 2. No: I want to customize my Nginx installation
# Where is your Nginx source code located?: /usr/src/nginx-0.8.52
# Where do you want to install Nginx to?: /opt/nginx
@aliang
aliang / knife.rb
Created March 22, 2012 21:37 — forked from jtimberman/knife.rb
Commented knife.rb for all the things
# Knife Configuration File.
#
# This is a Ruby DSL to set configuration parameters for Knife's
# general options. The default location for this file is
# ~/.chef/knife.rb. If multiple Chef repositories are used,
# per-repository configuration files can be created. A per repository
# configuration file must be .chef/knife.rb in the base directory of
# the Chef repository. For example,
#
# ~/Development/chef-repo/.chef/knife.rb
@aliang
aliang / knife.rb
Created July 23, 2012 09:44 — forked from wilmoore/knife.rb
Base "knife" configuration for a standard chef-solo setup
# .chef/knife.rb
# SEE: http://wiki.opscode.com/display/chef/Troubleshooting+and+Technical+FAQ
# set some sensible defaults
current_dir = File.dirname(__FILE__)
user = ENV['OPSCODE_USER'] || ENV['USER']
log_level :debug
log_location STDOUT
node_name `hostname`
client_key ''
@aliang
aliang / each_by_page.rb
Last active December 16, 2015 20:19
helper to iterate all by pagination using kaminari
# Probably should investigate how to chain this with scopes
# Also assumes mongoid with a default _id field, and the order_by method
class MyModel
def self.each_by_page(options = {})
per = options[:per] || 25
order_by = options[:order_by] || [:_id, 1]
i = 1
until (current_page = self.order_by(order_by).page(i).per(per)).empty?
current_page.each do |obj|
<form>
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
<input type="submit" value="Submit"></input>
</form>
@aliang
aliang / publicize_methods.rb
Created December 12, 2013 20:35
Expose private methods for testing. Can accept a block, or just call in RSpec before/after blocks
class Class
def publicize_methods
@_saved_private_instance_methods = self.private_instance_methods
self.class_eval { public *@_saved_private_instance_methods }
if block_given?
yield
reprivatize_methods
end
end
@aliang
aliang / country_codes.js
Created April 17, 2014 06:45
Really stupid iso-3166 country code converter. As stupid as possible!
var CountryCodes = (function() {
// Codes taken from Wikipedia as of Wed Apr 16 2014
var codes = {"AFG": "AF", "ALA": "AX", "ALB": "AL", "DZA": "DZ", "ASM": "AS", "AND": "AD", "AGO": "AO", "AIA": "AI", "ATA": "AQ", "ATG": "AG", "ARG": "AR", "ARM": "AM", "ABW": "AW", "AUS": "AU", "AUT": "AT", "AZE": "AZ", "BHS": "BS", "BHR": "BH", "BGD": "BD", "BRB": "BB", "BLR": "BY", "BEL": "BE", "BLZ": "BZ", "BEN": "BJ", "BMU": "BM", "BTN": "BT", "BOL": "BO", "BES": "BQ", "BIH": "BA", "BWA": "BW", "BVT": "BV", "BRA": "BR", "IOT": "IO", "BRN": "BN", "BGR": "BG", "BFA": "BF", "BDI": "BI", "KHM": "KH", "CMR": "CM", "CAN": "CA", "CPV": "CV", "CYM": "KY", "CAF": "CF", "TCD": "TD", "CHL": "CL", "CHN": "CN", "CXR": "CX", "CCK": "CC", "COL": "CO", "COM": "KM", "COG": "CG", "COD": "CD", "COK": "CK", "CRI": "CR", "CIV": "CI", "HRV": "HR", "CUB": "CU", "CUW": "CW", "CYP": "CY", "CZE": "CZ", "DNK": "DK", "DJI": "DJ", "DMA": "DM", "DOM": "DO", "ECU": "EC", "EGY": "EG", "SLV": "SV", "GNQ": "GQ", "ERI": "ER", "EST": "EE", "ETH": "ET",
@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 / io_ext.rb
Created May 7, 2014 22:47
Select random line from IO object
# See http://stackoverflow.com/questions/11007111/ruby-whats-an-elegant-way-to-pick-a-random-line-from-a-text-file
class IO
# Selects a random "line" from a file (might not be a line if different separator is passed).
# @param *args Same arguments as IO.foreach
# @return A random line from the IO object
def self.random_line(*args)
chosen_line = nil
self.foreach(*args).each_with_index do |line, number|
chosen_line = line if rand < 1.0/(number+1)
end
@aliang
aliang / disable-leaflet-map-events.js
Created June 4, 2014 00:00
Disable all (or almost all!) leaflet map events
// Assumes your L.Map is called "map". Untested!
map.dragging.disable();
map.touchZoom.disable();
map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
map.boxZoom.disable();
map.keyboard.disable();
if (map.tap) {
map.tap.disable();
}