Skip to content

Instantly share code, notes, and snippets.

@davetapley
davetapley / events.js
Created April 10, 2013 17:45
Trouble using for in loop with mocha
/* Mocha test
to use:
npm install mocha
mocha <filename>
or
npm test
*/
var assert = require('assert');
var tzwhere = require('../lib/index');
@davetapley
davetapley / mongojs_count.js
Last active December 16, 2015 02:59
Count for mongojs cursor is returned incorrectly.
var testCollection = require('mongojs')('test').collection('testCollection'),
shouldBeOneDoc = testCollection.find({}).sort({ myField : 1 }).limit(1);
shouldBeOneDoc.count(function (err, count) { console.log(count) });
@davetapley
davetapley / lat_hist.js
Created April 16, 2013 17:29
Pull entire Latitude history for a user
// https://developers.google.com/latitude/v1/using#ListingHistory
var minTimeMs = 0, // Earliest location to pull, 1st Jan 1070 seems far back enough.
maxTimeMs = Date.new().getTime(), // Latest location to pull, now.
lastResponse = [];
while {
// it is assumed this returns a max of 1000 locations in reverse chronological order,
// most recent location before maxTime first,
// then 999 more locations going back in time.
@davetapley
davetapley / fail.sh
Created April 24, 2013 01:05
PostgreSQL roles issue on Ubuntu 10.04
vagrant@heroku:~$ sudo su postgres
$ psql
psql (9.1.9)
Type "help" for help.
postgres=# SELECT rolname FROM pg_roles;
rolname
----------
postgres
vagrant
@davetapley
davetapley / validate-and-upsert.js
Created April 29, 2013 16:29
Validate a Mongoose model and then upsert if valid
mySchema.statics.validateAndUpsert = function (conditions, update, callback) {
var updateModel = new this(update);
updateModel.validate(function (err) {
if (err) {
callback(err);
} else {
this.findOneAndUpdate(conditions, update, { upsert: true}, callback);
}
}
};
@davetapley
davetapley / validate-and-upsert-2.js
Created April 29, 2013 16:49
Validate a Mongoose model and then upsert if valid (the other way)
mySchema.statics.upsert = function (conditions, update, callback) {
this.findOne(conditions, function (err, existingDoc) {
if (err) {
callback(err);
} else if (existingDoc) {
// I don't think this is valid
existingDoc.update(update);
existingDoc.save(callback);
} else {
return this.create(update, callback);
@davetapley
davetapley / runner.js
Last active December 16, 2015 22:18
Keep doing work until told to stop.
var timer,
work = function () {
asyncWork(function(err) {
timer = setTimeout(work, 10000);
}
},
exports.start = function () {
work();
@davetapley
davetapley / removing_bob.rb
Created May 21, 2013 15:59
Execute ActiveRecord query immediately
original_bobs = User.where first_name: 'Bob'
# Assume this deletes a Bob:
User.destroy 123
# Now I'd like to see user 123 in this list:
puts original_bobs
@davetapley
davetapley / dists.csv
Created June 3, 2013 02:02
Transit distance ratios
We can't make this file beautiful and searchable because it's too large.
1.0986533107808496
1.583960737452346
0.13734048649924682
3.5384582189028078
0.8109541275321867
1.4710983394905912
1.2568627243577724
0.6561450580865987
1.2664615130882972
1.10855911768647
Mon Jun 10 19:17:51 MST 2013: performing suspend
Tue Jun 11 07:52:44 MST 2013: Awake.
Tue Jun 11 07:52:44 MST 2013: Running hooks for resume
Running hook /etc/pm/sleep.d/novatel_3g_suspend resume suspend:
/etc/pm/sleep.d/novatel_3g_suspend resume suspend: success.
Running hook /usr/lib/pm-utils/sleep.d/99video resume suspend:
/usr/lib/pm-utils/sleep.d/99video resume suspend: success.
Running hook /etc/pm/sleep.d/99bcmbluetooth resume suspend: