Skip to content

Instantly share code, notes, and snippets.

# Add to config/routes.rb
# post 'incoming_emails' => 'incoming_emails#create'
require 'mail'
class IncomingEmailsController < ApplicationController
skip_before_filter :verify_authenticity_token, only: :create
def create
mail = Mail.new(params[:message])
echo Entering vagrant... Ctrl-C to abort
sleep 1
cd ~/
vagrant up && vagrant ssh && exit
@choonkeat
choonkeat / activerecord.stack.rb
Last active August 29, 2015 14:01
Drop this file into config/initializers/ and enjoy SQL logging with stacktrace
# set env FULL_STACKTRACE=1 for full stacktrace
# set env DEBUG_LOG=1 to turn this on (default off)
if defined?(ActiveRecord::LogSubscriber)
module ActiveRecord
class LogSubscriber
def sql(event)
self.class.runtime += event.duration
return unless logger.debug?
@choonkeat
choonkeat / hash.csspath.rb
Created June 2, 2014 02:17
Inefficient but expressive
Hash.class_eval do
# returns an array of values
def csspath(path, options = {})
Nokogiri::HTML(self.to_xml).css(path.dasherize).collect {|element|
result = Hash.from_xml(element.to_s)
(options[:include_keys] ? result : result.values.first)
}
end
end
@choonkeat
choonkeat / mitm.bash
Created June 3, 2014 14:46
peep at traffic e.g. elasticsearch request & response
# usage: [proxy port] [target port]
socat -v -d -d -d TCP4-LISTEN:$1,reuseaddr,retry,fork TCP4:localhost:$2,retry
@choonkeat
choonkeat / routes.patch.rb
Last active August 29, 2015 14:02
make "GET /assets/yourfile.css" redirect to the correct "<asset host>/assets/yourfile<-digest suffix>.css" url
match "/assets/*path" => lambda {|env|
path = env['REQUEST_PATH']
Rails.cache.fetch("v9.assets.#{path}", expires_in: 1.hour) do
if found = Rails.application.assets.find_asset(File.basename(path))
[302, { 'Location' => "#{Rails.application.config.action_controller.asset_host}#{File.dirname(path)}/#{found.digest_path}" }, []]
else
[404, {}, []]
end
end
}, via: [:get] # /lambda

OS X Screencast to animated GIF

This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.

Screencapture GIF

Instructions

To capture the video (filesize: 19MB), using the free "QuickTime Player" application:

// http://stackoverflow.com/a/8809472/136558
function generateUUID(){
var d = (window.performance && window.performance.now && window.performance.now()) ||
(Date.now && Date.now()) ||
(new Date().getTime());
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
});

Keybase proof

I hereby claim:

  • I am choonkeat on github.
  • I am choonkeat (https://keybase.io/choonkeat) on keybase.
  • I have a public key whose fingerprint is 5615 96F6 F675 FBD1 EE75 A2AF 6B5C 347F 317F DF7C

To claim this, I am signing this object:

(function() {
function direct_upload() {
console.log('direct_upload', this);
var ele = this;
new DirectUpload({
file_element: ele,
onProgress: function(percent, message) {
console.log('onProgress', arguments);
},
onError: function(status) {