Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / ngModelController.js
Created April 23, 2015 12:55
source code for demonstrating using ngModelController with SVG and Angular - full post https://truffles.me.uk/turn-anything-into-an-ng-model-with-ngmodelcontroller
"use strict";
/*
the `clock()` directive the only bit of the code that's handling ngModel - the
rest is basic angular or DOM code
*/
function clockDirective() {
@timruffles
timruffles / gist:e1f14572412f43d09da4
Created April 7, 2015 11:18
custom d3 multi format time scale - get your own formats/intervals into axis
// array of [format string, useFormat]. formats are accessed in order, return true to use the format
// - this is the default - it pretty much just finds the smallest time period that's non-zero
var multiScale = d3.time.format.utc.multi([
[".%L", function(d) { return d.getUTCMilliseconds(); }],
[":%S", function(d) { return d.getUTCSeconds(); }],
["%I:%M", function(d) { return d.getUTCMinutes(); }],
["%I %p", function(d) { return d.getUTCHours(); }],
["%a %d", function(d) { return d.getUTCDay() && d.getUTCDate() != 1; }],
["%b %d", function(d) { return d.getUTCDate() != 1; }],
["%B", function(d) { return d.getUTCMonth(); }],
@timruffles
timruffles / promise-queue.js
Last active August 29, 2015 14:15
quick promise based queue sketch
function queue(concurrency) {
var outstanding = [];
return function(fn) {
outstanding.push(fn);
if(outstanding.length < concurrency) {
pop();
}
}
function pop() {
@timruffles
timruffles / proxy.sh
Last active August 29, 2015 14:14
oauth local testing via ssh proxy
# ssh's into target box, forwards all incoming connections to :3000 to localhost :3030
# -R [bind_address:]port:host:hostport
# -N no remote command - just forward
# -g allows remote to connect to local
#
# you'll need to add `GatewayPorts yes` to your ssh config - e.g at `/etc/ssh/sshd_config` -
# and then restart sshd on the host machine (`service ssh restart` on ubuntu)
# some-host.com:443 -> localhost:4443
ssh -NgR 0.0.0.0:443:localhost:4443 some-host.com
@timruffles
timruffles / record_calls.rb
Last active August 29, 2015 14:13
record calls
module RecordCalls
# usage:
# resque_calls = RecordCalls.on(Resque, :enqueue)
#  resque_calls.called_with? PostTask, :hello, :*, project.id
#
# :* is a placeholder for calls, change RecordCalls.placeholder if you want a different one
@placeholder = :*
class << self
@timruffles
timruffles / test.rb
Created January 9, 2015 18:04
rails - POST JSON in controller tests in rails 3.2.x - can't easily do it, necessary for things as simple as post body containing { action: "something" }
# from http://stackoverflow.com/a/13206873/427710
env = Rack::MockRequest.env_for('/posts/1', :method => 'PUT', <some other params>)
status, headers, body = PostsController.action(:update).call(env)
@timruffles
timruffles / 0_README.md
Last active August 29, 2015 14:10
transforms vs ifs

I was changing the behaviour of this controller, and wondered if there was some way to make it less branchy and more self-documenenting.

I remembered an idea of Dave Thomas's - thinking about programs as series of transforms - and tried to apply it.

Which do you prefer?

@timruffles
timruffles / 01_before.js
Created November 10, 2014 19:36
'inside out' refactoring with promises
function outer(db) {
var codeqs = loadCodeqsForComparison(db, project.id, paths, analyses);
}
function loadCodeqsForComparison(db, projectId, paths, analyses) {
var files = loadFiles(projectId, ref, paths)
var codeqs = loadCodeqs(db, analyses)
return Promise.all([codeqs, files]).spread(function(codeqList, files) {
return codeqList.map(function(codeq) {
@timruffles
timruffles / index.html
Last active August 29, 2015 14:08
modal-demo
<!DOCTYPE html>
<html ng-app=exercise>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Angular Exercise</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../vendor/foundation/css/normalize.css" rel="stylesheet">
@timruffles
timruffles / tracking.js
Created October 28, 2014 11:01
tracking factory
var app = angular.module("exercise",[]);
function trackingFactory($http) {
var tracking = {};
var count = {};
tracking.event = function(event) {
if(!(event in count)) {
count[event] = 0;