Skip to content

Instantly share code, notes, and snippets.

View thomasyip's full-sized avatar

Thomas Y thomasyip

View GitHub Profile
/**
* Here is the simplified version. Courtesy of Mike Koss.
*
* See, http://labnote.beedesk.com/the-pitfalls-of-html5-applicationcache
*/
function handleAppCache() {
if (applicationCache == undefined) {
return;
}
@thomasyip
thomasyip / apple_theme_boxflex.css
Created November 29, 2010 06:50
Boxflex for iPhone app pane
/**
* Extracted from: https://github.com/beedesk/jQTouch/commit/b5d2fc63fe15acba15c4e8eaafc0e128997f8484
*/
/* ============= from themes/apple/theme.css ============= */
#jqt > * {
-webkit-transform: translate3d(0,0,0);
-webkit-backface-visibility: hidden;
background: rgb(197,204,211) url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAABCAIAAACdaSOZAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABdJREFUeNpiPHrmCgMC/GNjYwNSAAEGADdNA3dnzPlQAAAAAElFTkSuQmCC);
-webkit-user-select: ignore;
@thomasyip
thomasyip / slim.rb
Created October 26, 2011 05:41
Minimal Sinatra Example
# /slim/slim.rb
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require 'rubygems'
require 'sinatra'
class AppMain < Sinatra::Application
set :root, APP_ROOT
get '/' do
@thomasyip
thomasyip / config.ru
Created October 26, 2011 05:46
Config.ru for Rails meets Sinatra
# /config.ru
# This file is used by Rack-based servers to start the application.
# For Rails
require ::File.expand_path('../config/environment', __FILE__)
# For Sinatra
require './slim/slim.rb'
# - Make sinatra play nice
use Rack::MethodOverride
@thomasyip
thomasyip / config.ru
Created October 31, 2011 03:18
Sinatra + Warden & Rails + Devise Example
# /config.ru
# This file is used by Rack-based servers to start the application.
# File generated by "rails create MyApp"
# For Rails
require ::File.expand_path('../config/environment', __FILE__)
# For Sinatra
require './slim/slim.rb'
# - Make sinatra play nice
@thomasyip
thomasyip / storages.py
Created July 20, 2012 23:41
class S3BotoStaticStorage (to deploy of django `media` and `static` into two amazon bucket)
from __future__ import absolute_import
import settings
from storages.backends.s3boto import S3BotoStorage
from boto.s3.connection import SubdomainCallingFormat
def getattr_backup(obj, key, backupkey, default):
result = getattr(obj, key, None)
if result is None:
@thomasyip
thomasyip / common [stash] fields.py
Created July 22, 2012 04:19
Updated version of Django BigInt Patch for 64bit Primary Keys
"""
module mydjangolib.bigint_patch
A fix for the rather well-known ticket #399 in the django project.
Create and link to auto-incrementing primary keys of type bigint without
having to reload the model instance after saving it to get the ID set in
the instance.
Logs:
@thomasyip
thomasyip / YouModelResource.py
Created August 25, 2012 21:35
Custom `hydrate_m2m` method to workaround tastypie bug on creating model with dependent model
# Say, you have models `Invoice` and `InvoicePart`. The `InvoicePart`
# has a non-nullable field of `Invoice`.
#
# Currently (August 2012), `tastypie` will throw an error when you try
# to create (`HTTP Post`) a new `Invoice` that contains one or more new
# `InvoicePart`.
#
# The problem is that when `tastypie` processes the `InvoicePart`, the
# backlink to `Invoice` is not set.
#
@thomasyip
thomasyip / gist:5010926
Created February 22, 2013 05:21
Heroku Cycling
Running 1 dyno. Does running more dynos get some better treament?
```
2013-02-22T04:49:35+00:00 heroku[web.1]: Cycling
2013-02-22T04:49:38+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-02-22T04:49:41+00:00 heroku[web.1]: Process exited with status 143
2013-02-22T04:49:41+00:00 heroku[web.1]: State changed from up to down
2013-02-22T04:49:41+00:00 heroku[web.1]: State changed from down to starting
2013-02-22T04:49:46+00:00 heroku[web.1]: Starting process with command `newrelic-admin run-program python apps/manage.py runserver 0.0.0.0:40284 --noreload`
2013-02-22T04:49:47+00:00 app[web.1]: Validating models...
@thomasyip
thomasyip / CORS_ExpressJS
Created March 20, 2013 08:22
Cheatsheet for adding CORS supports to an ExpressJS app. I found that the most reliable way of sending CORS headers is to echo back what the browser sent to the server. Reminder to test it with browser cache disable and cleared at least one.
// CORS
app.use(function(req, res, next) {
if ('OPTIONS' !== req.method) {
if ('origin' in req.headers) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
}
next();
} else {
var body = '{}\n';
if ('origin' in req.headers) {