Skip to content

Instantly share code, notes, and snippets.

View steventux's full-sized avatar

Steve Laing steventux

View GitHub Profile
@steventux
steventux / gist:10279962
Created April 9, 2014 14:57
Quickly query the Github API with Ruby for ...stuff
require 'open-uri'
require 'json'
buff = ''
open('https://api.github.com/repos/alphagov/smart-answers/pulls') { |f| f.each_line { |line| buff << line } }
pulls = JSON.parse(buff)
puts pulls.first['user']
@steventux
steventux / gist:2990759
Created June 25, 2012 19:36
MiniTest + Rails + Devise test_helper
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require File.dirname(__FILE__) + '/blueprints'
require 'faker'
require 'rails/test_help'
require 'minitest/autorun'
require 'minitest/pride'
class MiniTest::Unit::TestCase
include MiniTest::ActiveRecordAssertions
@steventux
steventux / gist:2414348
Created April 18, 2012 15:29
Getting Padrino to play nicely with Content-Type => 'application/json' (the default for Backbone JS).
use Rack::Parser, :content_types => {
'application/json' => Proc.new { |body| ::MultiJson.decode body }
}
@steventux
steventux / gist:2368493
Created April 12, 2012 15:49
MiniTest + ActiveRecord associations helper methods
module MiniTest
module Assertions
module ActiveRecord
# assert_association User, :has_many, :editables, :polymorphic => true
#
def assert_association(clazz, association, associate, options={})
reflected_assoc = clazz.reflect_on_association(associate)
@steventux
steventux / gist:2172792
Created March 23, 2012 17:03
Benchmarks of POST to Padrino with postdata of 180c chars using various datastores and insertion techniques. (Ruby 1.9.3 + Passenger 3.0.11)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Passenger Ruby 1.9.3 Mysql AR create
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Server Software: Apache/2.2.14
Server Hostname: perf-test.local
Server Port: 80
Document Path: /message
Document Length: 0 bytes
@steventux
steventux / gist:2024426
Created March 12, 2012 20:18
Javascript bitwise encoding
var Encryption = (function() {
function generateKey(seed) {
var key = ""
while (key.length < seed) key += Math.srand(seed)
return key
}
/*
* @param 'val' - a String to encode or a byte[] to decode.
@steventux
steventux / gist:1559909
Created January 4, 2012 12:49
Turns a Javascript string into a function, can handle namespaced/contextual functions.
/**
* Given the String "Foo.who.bar" this function returns the function bar() from the context of Foo.who
* handy when you need to call or apply a namespaced function from a String representation.
*/
var stringToFunction = function(str) {
var nsArr = str.split("."),
parentNs = window,
len = nsArr.length;
for (var idx = 0; idx < len; idx++) {
parentNs = parentNs[nsArr[idx]];
@steventux
steventux / gist:1151384
Created August 17, 2011 11:46
ActionView date_select calendar logic in javascript
document.observe("dom:loaded", function() {
var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var dateEl = $('project_expiry_date_3i');
var monthEl = $('project_expiry_date_2i');
var yearEl = $('project_expiry_date_1i');
if (dateEl && monthEl && yearEl) {
var currentDateIndex = dateEl.selectedIndex;
function leapYearAdjustedDays() {
var daysInMonth = DAYS_IN_MONTH[monthEl.selectedIndex];
// Leap year calc.