Skip to content

Instantly share code, notes, and snippets.

@lengarvey
lengarvey / gemstats.rb
Created January 12, 2012 04:42
Get a list of how many gems there are for each letter.
require 'rest_client'
url = "http://rubygems.org/gems?letter="
('A' .. 'Z').each do |letter|
response = RestClient.get url + letter
total = response.to_s.match(/(\d+)<\/b> in total/)[1]
puts letter + " total " + total
end
@lengarvey
lengarvey / mongodb.rb
Created March 9, 2012 12:51
Mongo 2.1 homebrew recipe
require 'formula'
require 'hardware'
class Mongodb < Formula
homepage 'http://www.mongodb.org/'
packages = {
:x86_64 => {
:url => 'http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.1.0.tgz',
:md5 => '8645b107c0e9342bfaab4e68281e0d9b',
@lengarvey
lengarvey / mongo_test.rb
Created March 10, 2012 08:20
mongo stuff
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("so_test")
end
class Client
include Mongoid::Document
belongs_to :contact
field :name, type: String
@lengarvey
lengarvey / mr.js
Created March 15, 2012 03:52
mapreduce to aggregate in mongodb
// Assume a document structure as follows:
db.test.insert( { epochs : [1331781663, 1331781670, 1331781795], otherData : "Hello" } );
db.test.insert( { epochs : [1331721663, 1331721670, 1331721795], otherData : "Goodbye" } );
map = function() {
var d = this.otherData;
this.epochs.forEach(function(epoch) {
// Pin the epochs to a certain date
var date = new Date(epoch * 1000); // js does timestamps in milliseconds
@lengarvey
lengarvey / mrartists.js
Created March 18, 2012 02:40
Mongo Map Reduce example
// Insert some data into mongo:
[{email: "bob@example.com", user_id: "bob", artist_number: 24}, {email: "bob@example.com", user_id: "bob", artist_number: 22}, {email: "joe@example.com", user_id: "joe", artist_number: 24}, {email: "susan@example.com", user_id: "susan", artist_number: 21}, {email: "susan@example.com", user_id: "susan", artist_number: 24}, {email: "joe@example.com", user_id: "joe", artist_number: 22}].forEach(function(d) { db.artists.insert(d); });
// Declare map and reduce
map = function () {
emit({artist:this.artist_number, user:this.user_id}, 1);
}
@lengarvey
lengarvey / mongoid.rb
Created March 18, 2012 13:29
mongodb mongoid initialiser to create a pk factory
class PKFactory
def create_pk(row)
puts "creating #{row.inspect}"
row['_id'] = row['name']
row
end
end
Mongoid.configure do |config|
puts config.inspect
@lengarvey
lengarvey / sensis_results.json
Created March 31, 2012 02:37
sensis result duplicates
[
{
"name": "Epping Club",
"id": "13092989",
"hasExposureProducts": false,
"listingType": "Business",
"reportingId": "eyJzb3VyY2UiOiJZRUxMT1ciLCJwcm9kdWN0SWQiOiIxODQ3Nzg5MzIiLCJwcm9kdWN0VmVyc2lvbiI6IjEifQ",
"primaryAddress": {
"state": "NSW",
"type": "PHYSICAL",
@lengarvey
lengarvey / yay.coffee
Created June 19, 2012 04:50
Overriding rails ajax with custom success handler
if $('#your_form_id').length > 0
$.rails.ajax = (options) ->
options.success = ->
# do some stuff
# hide the first div, show the second
$.ajax(options)
@lengarvey
lengarvey / async_typekit.js
Created July 13, 2012 04:30
Use this to load the typekit javascript async style.
TypekitConfig = {
kitId: '<YOUR KIT ID HERE>'
};
(function() {
var tk = document.createElement('script');
tk.src = '//use.typekit.com/' + TypekitConfig.kitId + '.js';
tk.type = 'text/javascript';
tk.async = 'true';
tk.onload = tk.onreadystatechange = function() {
var rs = this.readyState;
@lengarvey
lengarvey / gist:3161319
Created July 22, 2012 23:03
Chrome without bundled flash. Fixes Macbook Pro Retina Flash issues
echo "/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --disable-bundled-ppapi-flash --user-datadir=/Users/$USER/Library/Application\ Support/Google/Chrome\ Canary/ 1> /dev/null 2> /dev/null &" > ~/Chrome.command
chmod 775 ~/Chrome.command