Skip to content

Instantly share code, notes, and snippets.

View joshed-io's full-sized avatar

Josh Dzielak joshed-io

View GitHub Profile
@joshed-io
joshed-io / gist:5852208
Created June 24, 2013 18:16
Ruby JSON performance benchmark
require 'json'
require 'benchmark'
json = {
:one => {
:two => {
:three => {
:four => {
:five => {
:six => {
@joshed-io
joshed-io / keen_io_zrank.rb
Last active December 18, 2015 17:09
A simple Ruby script illustrating how to implement Redis-style ZRANK on the result of a Keen IO group_by query
# Example Keen IO API response for a count, grouped by an 'entry' field
keen_result = [
{
"entry" => "Apple",
"result" => 15
},
{
"entry" => "Orange",
"result" => 10
},
@joshed-io
joshed-io / keen-node.js
Created March 1, 2013 11:14
Feed Ralph Pizza - Publish an event to the Keen IO API with node.js and the npm request package
// install the 'request' package first with 'npm install request'
var request = require('request');
// replace with your Keen IO project token
var projectToken = "501999234-FAKE-PROJECT-ID";
// create a sample JSON event for an event collection
var eventCollection = "meals";
var sampleEvent = {
username: "ralph",
@joshed-io
joshed-io / install_ruby_2.0.sh
Last active December 14, 2015 04:08
Try out Ruby 2.0 with an existing app that uses Bundler
cd $HOME/.rbenv/plugins/ruby-build
git pull
rbenv install 2.0.0-p0
rbenv local 2.0.0-p0
gem install bundler -v 1.3.0.pre.8
cd /path/to/ruby/app
# Necessary if not already 600
chmod 600 $HOME/.gem/credentials
@joshed-io
joshed-io / keen_beacon.rb
Created January 22, 2013 23:01
Send an event to keen.io via a GET request, useful for placing image beacons.
# Send an event to keen.io via a GET request
# Useful for placing image beacons in email
# (Traditional POST approach is still recommended where possible)
require 'json'
require 'base64'
# Get a project ID and API Key from keen.io
project_id = "your-project-id"
api_key = "your-api-key"
@joshed-io
joshed-io / com.darkice.plist
Created November 21, 2012 18:11
launchd plists for Icecast, Darkice, Jack Audio, and Github's Play
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.darkice</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
@joshed-io
joshed-io / paperclip.rb
Created October 11, 2012 05:38
Safer Paperclip id_partition interpolation for mongoid
# Googling for how to implement a good Paperclip id_partition for mongoid, you might find -
# attachment.instance.id.to_s.scan(/.{4}/).join("/")
# There are problems with this. Because mongoid ID's are hex strings, a 4-character string can contain
# 4^16 or 65536 values. Usually with id partition we're trying to make sure no directory ever holds more
# than 32k files, so 64k is potentially dangerous. To be safe we need to drop to 3 characters or 4096 values.
Paperclip.interpolates :id_partition do |attachment, style|
attachment.instance.id.to_s.scan(/.{3}/).join("/")
end
@joshed-io
joshed-io / console_include.rb
Created July 17, 2012 08:10
Load users in Rails console by username via method_missing
#adapt to your ORM/ODM as needed
#put in outermost scope of a file require'd into an IRB session
def self.method_missing(method_name, *args, &block)
User.where(username: method_name).first || super
end
# usage
# $ rails c
# >> dzello.username
@joshed-io
joshed-io / .vimrc
Created June 5, 2012 09:33
Vim regexp to convert Ruby 1.8 hash syntax into Ruby 1.9 syntax
" This assumes one space between the symbol and the hashrocket, adjust to your needs!
:%s/:\([a-z]*\) =>/\1:/gi<CR>
@joshed-io
joshed-io / example.coffee
Created May 31, 2012 22:28
Coffeescript mixins pattern
horse = new Horse()
horse.read({ title: "The 7 Habits of Highly Successful Horses"})
horse.ebooksRead() # 1
horse.smarterThan(new Horse()) # true