Skip to content

Instantly share code, notes, and snippets.

@karlseguin
karlseguin / gist:1129351
Created August 6, 2011 13:47
Dealing with inconsistent implementations of request.body (in Rack)
def self.stream_to_buffer(stream)
return stream if stream.is_a?(StringIO)
#put some type of size restrictions (though this is better done at the webserver level)
return nil if stream.size > 250 * 1024
StringIO.new(stream.read.to_s)
end
@karlseguin
karlseguin / gist:1142046
Created August 12, 2011 13:38
My goal is to make this test pass
def test_insert_with_auto_reconnect
conn = ReplSetConnection.new([TEST_HOST, RS.ports[0]], [TEST_HOST, RS.ports[1]], [TEST_HOST, RS.ports[2]], {:auto_retry => 5})
coll = conn.db(MONGO_TEST_DB).collection("test-sets")
RS.kill_primary
@coll.save({:a => 30}, :safe => true)
assert_equal 30, coll.find_one()["a"]
end
// account.js
var Store = new (require('./store').Store)('accounts', {'_id': 'id', 's': 'secret'}, function(){return new Account();});
var Account = function Account() {
this.id = this.secret = null;
};
Account.findById = function(id, callback) {
Store.findOne({'_id': Store.idFromString(id)}, function(err, result) {
if (err) { callback(err); }
@karlseguin
karlseguin / gist:1241676
Created September 26, 2011 05:44
some ruby and java code I'm writing
#ruby
require 'digest'
def salusa_signature(key, secret, user = nil)
Digest::SHA1.hexdigest(key + '|' + secret + '|' + (user || '').to_s)
end
//java
import java.security.*;
private static final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@karlseguin
karlseguin / gist:1327266
Created October 31, 2011 10:47
Nodejs and MongoDB, A Beginner’s Approach - Mirror

this is a mirror of http://blog.ksetyadi.com/2011/10/nodejs-and-mongodb-a-beginners-approach/

This is not a book and I didn’t try to sell a book to you.

The term “A Beginner’s Approach” reflects my self when finding a hard way out to connect Nodejs to MongoDB. There are lots of libraries available to use when connecting Nodejs to MongoDB. If you were trying to make your feet wet, and that’s what I’m doing until today, you probably want to try this approach. I can’t promise anything but at least, you will not get a headache.

First, read about Nodejs. After that, MongoDB. If you’re already familiar with it, skip it and install both on your system. There maybe vary depending on your system. If you use Mac and Homebrew (or MacPorts), you’re lucky. Just do this:

$ brew install node
@karlseguin
karlseguin / gist:1336377
Created November 3, 2011 12:27
C# pool creator
public class Pool<T>
{
private readonly int _count;
private readonly Queue<T> _pool;
private readonly Func<Pool<T>, T> _create;
private readonly object _lock = new object();
public Pool(int count, Func<Pool<T>, T> create)
{
_count = count;
@karlseguin
karlseguin / gist:1342519
Created November 6, 2011 05:27
pngout and cache-control all files in an s3 bucket
require 'aws/s3'
require 'net/http'
require 'fileutils'
$config = {
:aws_bucket => 'XYZ',
:aws_key => 'BLABLAH',
:aws_secret => 'secret',
:png_compressor => 'PATH_TO_PNG_OUT_OR_SOMETHING'
}
You build an endpoint that takes in data and saves it.
This is a service that takes in data from outside sources, the user can be any type.
(some sites use integers, some use strings, some guids...)
3 samples:
POST /notes
{
user: 9000
@karlseguin
karlseguin / gist:1598236
Created January 12, 2012 02:44
reading length-prefixed streams
class StreamReader
constructor: (@callback) ->
read: (buffer) ->
unless @buffer?
if this.initialize(buffer)
@partial = null
@partialLength = 0
else
return
@karlseguin
karlseguin / gist:1694156
Created January 28, 2012 12:29
style override I use for duckduckgo.com
body{background:#fff;font-family:'Verdana';font-size:16px;}
#header{background-image:nome;background:#f0f0f0;border-bottom:1px solid #ddd;height:45px}
#header_logo, #keyboard_shortcuts{display:none !important;}
#search_form{border:none;margin-top:4px}
#search_form_input{border:solid #ccc;border-width:1px 0 1px 1px}
#search_form_input_clear{border:solid #ccc;border-width:1px 0}
#header_button_menu_wrapper{position:absolute;top:3px;right:20px;}
#header_button{background:none;border:none}
#header_button_menu_wrapper a.header_button_menu_item{color:#555;font-size:90%;text-shadow:none;background:none;text-transform: lowercase}
#zero_click_abstract, #zero_click_abstract_top, .results_zero_click, .results_zero_click_more{border:none;}