Skip to content

Instantly share code, notes, and snippets.

View omarkj's full-sized avatar

Omar Yasin omarkj

  • Vancouver, BC, Canada
View GitHub Profile
// Based on http://www.diveintojavascript.com/projects/sprintf-for-javascript
String.prototype.format = function() {
var i = 0, a, f = this, o = [], m, p, c, x, s = '';
while (f) {
if (m = /^[^\x25]+/.exec(f)) {
o.push(m[0]);
}
else if (m = /^\x25{2}/.exec(f)) {
o.push('%');
@omarkj
omarkj / gist:517141
Created August 10, 2010 11:55
Using EventEmitters
var amqp = require('./amqp'),
sys = require('sys'),
net = require('net'),
events = require('events');
var RabbitClient = function() {
events.EventEmitter.call(this);
var self = this;
var connection = amqp.createConnection({ host: cfg.AMQP.Host });
connection.addListener('ready', function () {
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
var fs = require('fs'),
assert = require('assert'),
crypto = require('crypto');
var msg = "Gaur!";
fs.readFile('PrivateKey.pem', 'utf-8', function(e,pri) {
var signer = crypto.createSign('sha256').update(msg);
var signature = signer.sign(pri, output_format='base64');
fs.readFile('PublicKey.pem', 'utf-8', function(e,pub) {
var sys = require('sys'),
spawn = require('child_process').spawn,
openssl = spawn('python', ['test.py']);
openssl.stdout.on('data', function (data) {+
sys.print('signature is valid: ' + data);
});
openssl.stderr.on('data', function (data) {
sys.print('Error: ' + data);
@omarkj
omarkj / EventBus.js
Created October 31, 2010 17:44
Simple EventBus in JS
"use strict";
var EventBus = function() {
var events = [];
var p_on = function(eventName, callback) {
if (!events.some(function(e) {
return e.name == eventName; })) {
var new_event = {
name: eventName,
cb: callback
};
@omarkj
omarkj / _formatting.js
Created November 9, 2010 14:55
Underscore.js mixin that provides a 'printf'-like functionality
_.mixin({
format: function(string) {
var i = 1, a, f = string, o = [], m, p, c, x, s = '';
while (f) {
if (m = /^[^\x25]+/.exec(f)) {
o.push(m[0]);
}
else if (m = /^\x25{2}/.exec(f)) {
o.push('%');
}
@omarkj
omarkj / deploy_couchapps
Created November 19, 2010 20:07
Deploy multiple couchapps
#!/usr/bin/env python
# encoding: utf-8
import sys, getopt, subprocess, os
help_message = '''
Usage: deploy_all
\t-s server_name
\t-c couchapp exec'''
class Usage(Exception):
@omarkj
omarkj / xhr_polling
Created March 1, 2011 18:08
Not getting EXIT
-module(socketio_transport_xhr_polling).
-include_lib("../include/socketio.hrl").
-behaviour(gen_server).
%% API
-export([start_link/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
handle_cast(listen, #state{socket=Socket, rest=Rest} = State) ->
case gen_tcp:recv(Socket, 0, 35000) of
{ok, Data} ->
RestOfData = parse(<<Rest/binary, Data/binary>>),
gen_server:cast(?SERVER, listen),
State#state{rest=RestOfData};
{error, closed} ->
io:format("error, socket closed, do failover? ~n"),
gen_server:cast(?SERVER, connect),
failover(State);