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: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.
@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: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: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: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: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 }
}
{
"primary_parent":
{
"web_url": "https://www.gov.uk/browse/abroad/travel-abroad",
"title": "Travel abroad",
"parent": {
"web_url": "https://www.gov.uk/browse/abroad",
"title": "Passports, travel and living abroad",
"parent": null
}
DraftContentItem.where(publishing_app: "email-campaign-frontend").each do |content_item|
content_item.update_attributes!(publishing_app: "share-sale-publisher")
payload = Presenters::ContentStorePresenter.present(content_item)
ContentStoreWorker.perform_async(
content_store: Adapters::DraftContentStore,
base_path: content_item.base_path,
payload: payload,
)
end
Started GET "/foreign-travel-advice/spain" for ::1 at 2016-02-24 16:35:21 +0000
Processing by TravelAdviceController#show as HTML
Parameters: {"country_slug"=>"spain"}
Rendered govuk_component/breadcrumbs (0.7ms)
Rendered govuk_component/title (1.0ms)
Rendered travel_advice/_travel_advice_navigation.html.erb (7.7ms)
Rendered govuk_component/metadata (8.9ms)
Rendered govuk_component/govspeak (0.8ms)
Rendered travel_advice/_country_summary.html.erb (54.9ms)
Rendered govuk_component/related_items (2.0ms)