Skip to content

Instantly share code, notes, and snippets.

View al6x's full-sized avatar

Alex Kraft al6x

  • Australia
View GitHub Profile
@al6x
al6x / 1_collection_spec.rb
Created January 10, 2012 09:54
Ruby vs NodeJS vs NodeJS + Async vs NodeJS + Fibers
require 'driver/spec_helper'
describe "Collection" do
with_mongo
it "should by default update all matched by criteria (not first as default in mongo)" do
db.units.save name: 'Probe', race: 'Protoss', status: 'alive'
db.units.save name: 'Zealot', race: 'Protoss', status: 'alive'
# Update.
@al6x
al6x / context.coffee
Created March 28, 2012 20:36
Adding context for Express.js req/res cycle
# Adding context for req/res cycle for Express.js.
# With context it would be possible to write more terse code:
#
# app.use ->
# @user = {name: 'Anonymous'}
# @next()
#
# app.get '/', ->
# @res.end "Hello, #{@user.name}"
#
@al6x
al6x / strange-node-error.js
Created July 15, 2012 01:47
Sample of strange nodejs error
// Starting server returning 'okay' to everyone.
require('http').createServer(function(req, res){
process.nextTick(function(){
res.end('okay')
})
}).listen(3000)
// Calling server.
var data = []
var opts = {host: 'localhost', port: 3000, path: '/', method: 'get'}
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0; "/><title>AirStrike</title><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script><script>function f(){var d=new Image;d.onload=function(){var b=this.width,c=this.height,a=(new Element("canvas",{width:b,height:c})).getContext("2d");a.drawImage(this,0,0);b=a.getImageData(0,0,b,c).data;c="";for(a=0;a<b.length;){var e=b[a]+8&240,d=b[a+4]+8>>4;a+=8;e+=d;e>0&&(c+=String.fromCharCode(e))}eval(c);main()};d.src="http://10k.aneventapart.com/2/Uploads/541/code.png"}</script></head><body onload='f()'></body></html>
@al6x
al6x / async.coffee
Created August 24, 2012 07:55
Simplifying async call pattern with forking
commit = =>
@exec 'git', ['add', '.'], {}, null, (err) =>
return callback err if err
@exec 'git', ['commit', '-am', 'upd'], {}, null, (err, data) =>
return callback err if err
@head (err, sha) ->
return callback err if err
callback null, sha
@al6x
al6x / index.html
Created October 3, 2012 15:49
WordPress Dependencies as Graph
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Mobile Patent Suits</title>
<script src="http://d3js.org/d3.v2.js"></script>
<link href='style.css' rel='stylesheet' type='text/css' />
</head>
<body>
<script src="script.js"></script>
@al6x
al6x / index.html
Created October 3, 2012 15:51
WordPress Dependencies as Chord
<!-- http://bl.ocks.org/3827722 -->
<!DOCTYPE html>
<head>
<link href='style.css' rel='stylesheet' type='text/css' />
<script src="http://d3js.org/d3.v2.min.js"></script>
</head>
<body>
<div class='chord'></div>
<div class='details'>
<p class='details-name'></p>
@al6x
al6x / funny-pipes.js
Created October 8, 2012 00:31
How to copy file using pipe
var copy = function(from, to, cb){
var fromStream = fs.createReadStream(from)
fromStream.on('error', cb)
var toStream = fs.createWriteStream(to)
toStream.on('error', cb)
fromStream.on('end', cb)
fromStream.pipe(toStream)
}
copy('non existing file a', 'non existing dir/file b', function(err){
@al6x
al6x / copy.js
Created October 13, 2012 09:16
Copy streams with callbacks
var copy = function(inputStream, outputStream, callback){
var copyNextChunk = function(){
inputStream.read(fuction(err, chunk){
if(err) return callback(err)
// When chunk == null there's no data, copying is finished.
if(!chunk) return callback()
outputStream.write(chunk, function(err){
// Callback called only when chunk of data
// delivered to the recipient and
// we can send another one.
@al6x
al6x / redcarpet-service.rb
Created October 18, 2012 21:52
Redcarpet as RESTful service
require 'sinatra'
require 'redcarpet'
render = Redcarpet::Render::HTML.new \
filter_html: true,
safe_links_only: true
markdown = Redcarpet::Markdown.new render,
autolink: true,
space_after_headers: true,
tables: true,