Skip to content

Instantly share code, notes, and snippets.

Justin Perkins justinperkins

View GitHub Profile
View test service
require 'test_helper'
class ServiceTest < ActiveSupport::TestCase
def setup
super
@klass = Class.new do
include Service
View test.rb
class Event
def initialize
@callbacks = {}
end
def on(event, &block)
@callbacks[event] ||= []
@callbacks[event] << block
end
@justinperkins
justinperkins / jquery-auth-token.js
Created Oct 26, 2012
Put auth-token on all jQuery Ajax Requests
View jquery-auth-token.js
$(function(){
var tokenValue = $("meta[name='csrf-token']").attr('content');
$.ajaxSetup({
headers: {'X-CSRF-Token': tokenValue}
});
})
@justinperkins
justinperkins / gist:3909887
Created Oct 18, 2012
Backbone.js + Rails: Add Auth Token to Sync
View gist:3909887
// Adapted from: https://gist.github.com/1251730
// I like this technique better: https://gist.github.com/3960219
$(function(){
var paramName = $("meta[name='csrf-param']").attr('content');
var paramValue = $("meta[name='csrf-token']").attr('content');
Backbone.sync = _.wrap(Backbone.sync, function(originalSync, method, model, success, error){
if (method == 'create' || method == 'update' || method == 'delete') {
// grab the token from the meta tag rails embeds
@justinperkins
justinperkins / cookie.js
Created Sep 7, 2012 — forked from ChillyBwoy/cookie.js
Cookie functions for underscore.js
View cookie.js
View quix
tt http://trac/cgi-bin/trac.cgi/ticket/%s Jump To Trac Ticket
tc http://trac/cgi-bin/trac.cgi/changeset/%s Jump To Trac Commit
tp http://trac/cgi-bin/trac.cgi/browser/%s Jump To Trac Path
am http://www.allmusic.com/cg/amg.dll?SQL=%s&OPT1=1&Submit=Go&P=amg Search AllMusic
movies http://www.google.com/movies?sc=1&near=%s&rl=1 Google Movies
View taco_mailer.rb
#!/usr/bin/env ruby
#######################
# description:
# sends an email to one or more people on a certain day of the week, which rotates every week
# an email is sent on monday to advise your recipients which day of the week the taco day falls on
# then on the day of the taco day, a brief email will be sent to remind people once again
# use cron to schedule this script to be invoked on weekdays at whatever time you want the email to be sent
# here is an example: 0 11 * * 1-5 ~/bin/taco-mailer.rb
#######################
View time_tracker.rb
#!/usr/bin/env ruby
require 'rubygems'
=begin
TimeTracker is a simple way to track time on tasks
Start tracking time: ./time_tracker.rb -s some-task-name
Stop tracking time: ./time_tracker.rb -f
View recorded time for a task: ./time_tracker.rb -h some-task-name
View image_resizer.rb
# Use builtin Image Events app in Mac OS X to resize images with Ruby
# Usage:
# require 'image_resizer'
# class Foo
# include ImageResizer
# def some_method
# resize('/absolute/path/to/an/image.jpg')
# resize('/absolute/path/to/an/image.jpg', :target_width => 500)
# end
# end
View amazon_mp3_music.rb
require 'hpricot'
require 'open-uri'
class AmazonMp3Music
ASSOCIATE_ID = 'enjoybeing-20'
AMAZON_INJECTED_STRINGS = [' (Amazon Exclusive)', ' [Explicit]']
attr_accessor :asin, :artist, :album, :album_image, :referral_url, :processed
def initialize(url)
@url = url
You can’t perform that action at this time.