Skip to content

Instantly share code, notes, and snippets.


James Stewart jystewart

View GitHub Profile
View get_json.rb
require 'fileutils'
url_file = "urlfile.txt"
def sanitize_filename(filename)
name = filename.strip
# NOTE: File.basename doesn't work right with Windows paths on Unix
# get only the filename, not the whole path
name.gsub!(/^.*(\\|\/)/, '')
# Strip out the non-ascii character
jystewart / team-repo-list.rb
Last active Sep 4, 2015
Generate YAML file of teams' github repositories suitable for generating pages with github pages
View team-repo-list.rb
# Generate YAML file of teams' github repositories suitable for generating pages
# with jekyll/github pages and upload it to a github repository.
# Requires the 'octokit' gem
# Usage
# export TOKEN=my-github-personal-token
# export ORG=my-org
# ruby team-repo-list.rb
# export TOKEN=
jystewart / places_of_interest.go
Created Nov 5, 2013
Very early proof of concept golang implementation of the GOV.UK Imminence API
View places_of_interest.go
package main
import (
jystewart / places_of_interest.rb
Last active Dec 27, 2015
Quick example of how to do a geo query with the moped library and convert the output to JSON
View places_of_interest.rb
require 'moped'
require 'yajl'
session =[ "" ])
session.use "imminence_development"
query = session.command({
geoNear: "places",
near: [ -2.16933984212885, 52.27229461855149 ],
spherical: false,
jystewart / migrate-devise-passwords.rb
Created Oct 5, 2012
Methods for migrating devise passwords from naive MD5 to a new strategy
View migrate-devise-passwords.rb
class User < ActiveRecord::Base
alias_method :valid_password_without_legacy?, :valid_password?
def valid_password?(incoming_password)
if valid_password_without_legacy?(incoming_password)
return true
elsif Digest::MD5.hexdigest(incoming_password) == self.encrypted_password
jystewart / works_with_gds-sso_0.6
Created Jun 14, 2012
Updated test for use in govuk_content_models
View works_with_gds-sso_0.6
test "should create new user with oauth params" do
auth_hash = {
"uid" => "1234abcd",
"info" => {
"uid" => "1234abcd",
"email" => "",
"name" => "Luther Blisset",
user = User.find_for_gds_oauth(auth_hash).reload
jystewart / tag_collation_service.rb
Created Jun 10, 2012
Tag code cleanup sketches
View tag_collation_service.rb
# A class to filter input parameters coming from a form in rails
# and collate them into a form suitable for use with a taggable
# object.
# This will mutate the params hash it is given.
# This could be used in a controller to take input that represents
# tags under its various names and collapse them to hand to a domain
# model that only knows about tags/tag_ids. Used with a decorator
# such as it could handle everything
jystewart / tag_type_decorator.rb
Created Jun 10, 2012
Example decorator that could be used with GOV.UK API responses to extract tags by type
View tag_type_decorator.rb
require 'active_support/inflector'
class TagTypeDecorator
def self.has_tag_types(*types)
types.each do |sym|
define_method sym do { |t| t.tag_type == sym.to_s.singularize.capitalize }
jystewart / elevate.xml
Created Feb 29, 2012
Solr Schema for need-o-tron
View elevate.xml
<?xml version="1.0" encoding="UTF-8" ?>
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
jystewart / api.rake
Created Jan 2, 2012
Class to help with generating OAuth2 tokens for app-to-app interactions
View api.rake
namespace :api do
task :create_client => :environment do
raise "Requires name. id and secret are optional" unless ENV['name']
@service ||=
@service.create_client(ENV['name'], ENV['id'], ENV['secret'])
puts @service.description