Skip to content

Instantly share code, notes, and snippets.

@punund
punund / gist:1034276
Created June 19, 2011 13:31
Proper exception handling in Rails 3
# coding: utf-8
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from Exception, :with => :handle_exceptions
private
def handle_exceptions(e)
case e
when CanCan::AccessDenied
@punund
punund / fen_controller.rb
Created June 19, 2011 13:59
Dynamic creation of chess diagrams
#
# responds to requests with Forsyth-Edwards notation in URL with an image:
# http://dia-x.info/fen/2S5/1bQKpR2/4s3/2bkpP2/Sr1p2r1/1P1R3B/1B2s3/8
#
class FenController < ApplicationController
include Magick
@punund
punund / author.rb
Created June 19, 2011 15:45
Generation of alphanumeric person handle
#
# This automatically creates a "handle" upon creation of person's record.
#
# "John Doe"'s handle shall look like "DO<x>J", where <x> is a minimal necessary number of digits,
# so we have to look up the existing handles first. We exclude zeros (to avoid confusion with Os)
# and ones (to exclude jealousy "to be the first"), so we conveniently employ octal digits and add
# two subsequently. We also have to do the best effort to normalize last names (drop diacritics,
# honorifics, Celtic and Dutch name prefixes, sr/jr suffixes, etc. to maximize variance.
#
# See this in action at http://dia-x.info/authors
@punund
punund / .rvmrc
Created November 16, 2011 22:52
.rvmrc common template
_RUBY=ruby-1.9.3
_PROJECT=dia-x
export rvm_gem_options="--no-rdoc --no-ri"
if [[ -s "${rvm_path:-$HOME/.rvm}/environments/$_RUBY@$_PROJECT" ]]
then
. "${rvm_path:-$HOME/.rvm}/environments/$_RUBY@$_PROJECT"
else
rvm --create use "$_RUBY@$_PROJECT"
@punund
punund / splitter.sh
Created November 24, 2011 00:16
splitter in bash
#!/bin/bash
[[ $1 =~ ([[:alnum:]]+)-([[:alnum:]]+) ]] || exit 1
[[ "$2" = suffix ]] &&
echo ${BASH_REMATCH[2]} && exit 0
echo ${BASH_REMATCH[1]} && exit 0
@punund
punund / getImageSize.coffee
Created November 11, 2012 19:57
Retrieves first chunk of data of remote image via HTTP GET to find out its size in pixels. PNG, JPEG
getImageSize = (image, cb) ->
fromHex = (hex) -> parseInt "0x#{hex}", 16
req = http.request image, (res) ->
res.setEncoding 'hex'
res.on 'error', (e) -> cb "getImageSize error (#{image}): " + e, null, image
res.on 'data', (chunk) ->
m = switch res.headers['content-type']
when 'image/jpeg'
chunk.match 'ffc0001108(....)(....)'
when 'image/png'
indexB: ->
x = fs.readFileSync xmlInput
parser.parseString x, (error_xml, res_xml) ->
for product in res_xml?.products?.product[0..0]
r = request
uri: S.Found.uri + '/stock/one'
method: 'POST'
json: product
r.pipe process.stdout
var bases = require('bases');
var crypto = require('crypto');
// Returns a base-62 (alphanumeric only) string of the given length:
function randomStr(length) {
// We generate a random number in a space at least as big as 62^length,
// and if it's too big, we just retry. This is still statistically O(1)
// since repeated probabilities less than one converge to zero. Hat-tip to
// a Google interview for teaching me this technique! ;)
# Delete branches not tracked by remote
git remote prune origin
# Prettier log
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
@punund
punund / gist:7965186
Created December 14, 2013 21:35
два файла конфигурации nginx
ike@osaka:~$ cat /etc/nginx/sites-available/node-configly
server {
listen 80;
server_name www.config.ly;
location / {
proxy_pass http://unix:/var/run/proxy.sock/node-configly.80:$uri$is_args$args;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;