Skip to content

Instantly share code, notes, and snippets.

View jystewart's full-sized avatar

James Stewart jystewart

View GitHub Profile
@jystewart
jystewart / optimize_tables.sql
Created May 21, 2011 15:01
Query to generate OPTIMIZE TABLE statements for all fragmented tables
SELECT CONCAT('OPTIMIZE TABLE ', TABLE_SCHEMA,'.',TABLE_NAME, ';'), Data_free from information_schema.TABLES where TABLE_SCHEMA NOT IN ('information_schema','mysql') and Data_free > 0 AND ENGINE NOT IN ('INNODB', 'MEMORY');
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
jystewart / places_of_interest.go
Created November 5, 2013 22:44
Very early proof of concept golang implementation of the GOV.UK Imminence API
package main
import (
"encoding/json"
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/http"
)
@jystewart
jystewart / places_of_interest.rb
Last active December 27, 2015 12:39
Quick example of how to do a geo query with the moped library and convert the output to JSON
require 'moped'
require 'yajl'
session = Moped::Session.new([ "127.0.0.1:27017" ])
session.use "imminence_development"
query = session.command({
geoNear: "places",
near: [ -2.16933984212885, 52.27229461855149 ],
spherical: false,
@jystewart
jystewart / migrate-devise-passwords.rb
Created October 5, 2012 15:47
Methods for migrating devise passwords from naive MD5 to a new strategy
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
update_legacy_password(incoming_password)
end
end
@jystewart
jystewart / works_with_gds-sso_0.6
Created June 14, 2012 13:32
Updated test for use in govuk_content_models
test "should create new user with oauth params" do
auth_hash = {
"uid" => "1234abcd",
"info" => {
"uid" => "1234abcd",
"email" => "user@example.com",
"name" => "Luther Blisset",
}
}
user = User.find_for_gds_oauth(auth_hash).reload
@jystewart
jystewart / tag_collation_service.rb
Created June 10, 2012 16:16
Tag code cleanup sketches
# 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 https://gist.github.com/2906392 it could handle everything
@jystewart
jystewart / tag_type_decorator.rb
Created June 10, 2012 16:08
Example decorator that could be used with GOV.UK API responses to extract tags by type
require 'active_support/inflector'
class TagTypeDecorator
def self.has_tag_types(*types)
types.each do |sym|
define_method sym do
taggable.tags.select { |t| t.tag_type == sym.to_s.singularize.capitalize }
end
end
end
@jystewart
jystewart / elevate.xml
Created February 29, 2012 16:51
Solr Schema for need-o-tron
<?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
http://www.apache.org/licenses/LICENSE-2.0
@jystewart
jystewart / api.rake
Created January 2, 2012 11:08
Class to help with generating OAuth2 tokens for app-to-app interactions
namespace :api do
task :create_client => :environment do
raise "Requires name. id and secret are optional" unless ENV['name']
@service ||= AuthServiceTools.new
@service.create_client(ENV['name'], ENV['id'], ENV['secret'])
puts @service.description
end