Skip to content

Instantly share code, notes, and snippets.

View pdswan's full-sized avatar

Peter Swan pdswan

View GitHub Profile
@pdswan
pdswan / sequentially.js
Created June 6, 2013 16:24
semi-functional js experimentation forcing sequentially execution
var assert = require('assert')
function _if(predicate) {
return {
then: function(thenFn) {
return {
else: function(elseFn) {
predicate.map(function(val) {
if (val) thenFn()
else elseFn()
// result of `curl https://lydian.indabamusic.com/opportunities`
{
"status": "success",
"message": null,
"data": [
{
"id": "1237fa00-c24c-11e2-94e4-12313b0b1241",
"slug": "device-you-think-you-know-remix-contest",
"name": "Device - \"You Think You Know\" Remix Contest",
"tempo": "179",
@pdswan
pdswan / purgeInbox.js
Created May 28, 2013 19:41
Purge unread Gmail messages older than x days
/**
* Removes all unread threads matching the query
* older_than:<interval> label:purge-after-<interval>
*/
var intervals = ["1d", "7d", "1m"];
function purgeAll() {
intervals.forEach(function(interval) {
var query = constructQuery(interval);
var threads = GmailApp.search(query);
Logger.log("Found " + threads.length + " matching query: " + query);
@pdswan
pdswan / jruby_utc.rb
Created April 10, 2013 15:19
Forcing jruby to use UTC for everything
ENV['TZ'] = 'UTC'
if RUBY_PLATFORM =~ /java/
org.joda.time.DateTimeZone.setDefault(org.joda.time.DateTimeZone::UTC)
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone('UTC'))
end
@pdswan
pdswan / example.js
Last active December 15, 2015 00:09
What's the proper way to wrap a possibly asynchronous function in an angular deferred? I have an external, callback based SDK which interacts with an http API. The first time I ask for resources, an http call is made. The second time the data is cached so the callback is called immediately. This is a simplified example.
var data
function possiblyAsync(callback) {
if (alreadyLoaded) {
callback(data)
} else {
asyncLoad(function(loadedData) {
data = loadedData
callback(data)
})
}
@pdswan
pdswan / not-working.js
Last active December 14, 2015 19:19
express cors
var express = require('express')
, cors = require('connect-xcors')({
headers: ['X-Requested-With', 'X-HTTP-Method-Override', 'Content-Type', 'Accept', 'Authorization']
})
var app = express()
// OPTIONS requests don't get handled because
// cors is hidden behind the routing middleware
// and '/' is only accessible via GET
@pdswan
pdswan / MRI_1.9.2-p247_backtrace
Created November 18, 2012 00:16
Rubinius vs MRI Socket Errors
IOError:
closed stream
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/openssl/buffering.rb:174:in `sysread_nonblock'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/openssl/buffering.rb:174:in `read_nonblock'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/net/http.rb:2770:in `read_chunked'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/net/http.rb:2750:in `read_body_0'
/Users/pdswan/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/net/http.rb:2710:in `read_body'
@pdswan
pdswan / Gemfile
Created October 20, 2012 18:56
Ruby event source without event machine
source "http://rubygems.org"
gem 'thin'
gem 'cramp'
gem 'rack'
@pdswan
pdswan / vagrant.rb
Created August 27, 2012 17:18
Vagrant capistrano config
require 'ostruct'
class VagrantSSHConfig < OpenStruct
def initialize
super parse_options(`vagrant ssh-config`)
end
private
def parse_options(option_string)
@pdswan
pdswan / mailing_list_updater_unit_test.rb
Created June 1, 2012 18:08
Testing Classes for Active Record Callbacks
require 'test/unit'
require 'mocha'
# assumes MailingListUpdater is defined / exposed at the top level. this is not the case
# for the code in this gist.
class MailingListUpdaterTest < Test::Unit::TestCase
test "#update_mailing_list calls enqueue_for_mailing_list_update when a user is present" do
user = mock('User', :enqueue_for_mailing_list_update => true)
record = stub('Record')