Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
jnunemaker / gist:311068
Created February 22, 2010 13:32 — forked from kamal/gist:269412
require "rack/openid"
require "warden"
use Rack::OpenID
use Warden::Manager do |manager|
Warden::Strategies.add(:openid) do
def authenticate!
if resp = env["rack.openid.response"]
case resp.status
when :success
@jnunemaker
jnunemaker / application.js
Created March 3, 2010 18:43 — forked from bkeepers/application.js
browser cookie timezone stuff
jQuery(function() {
$.cookie('tz', (new Date()).getTimezoneOffset());
});
@jnunemaker
jnunemaker / mg
Created March 30, 2010 19:25 — forked from defunkt/gemspec
simple gemspec generator
#!/usr/bin/env ruby
# Usage: generate a blank gemspec, for defunkt
abort "pass a name" unless name = ARGV[0]
date = Time.now.strftime('%Y-%m-%d')
puts <<spec
Gem::Specification.new do |s|
s.name = "#{name}"
s.version = "0.1.0"
alias reload='. ~/.bashrc'
alias ea="$EDITOR ~/bin/dotfiles/bash/aliases && reload"
alias ee="$EDITOR ~/bin/dotfiles/bash/env && reload"
alias eh="$EDITOR ~/bin/dotfiles/bash/heroku && reload"
# Processes
alias tu='top -o cpu'
alias tm='top -o vsize'
# Projects
@jnunemaker
jnunemaker / oauth2_example.rb
Created April 22, 2010 22:18 — forked from technoweenie/oauth2_example.rb
oauth2, sinatra, faraday
# see http://github.com/intridea/oauth2
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'
class ConnectionLogger < Faraday::Middleware
def call(env)
env[:response].on_complete do |env|
puts "RESULT: #{env[:status]}\n#{env[:body]}"
@jnunemaker
jnunemaker / mongodb_compound_unique_index.rb
Created May 6, 2010 17:52
MongoDB compound unique index
require 'rubygems'
require 'mongo'
uid, aid = BSON::ObjectID.new, BSON::ObjectID.new
db = Mongo::Connection.new.db('testing')
db.drop_collection('ratings')
db.create_collection('ratings')
ratings = db['ratings']
ratings.create_index([[:user_id, Mongo::ASCENDING], [:article_id, Mongo::ASCENDING]], :unique => true)
@jnunemaker
jnunemaker / group_sum.rb
Created May 6, 2010 19:13
Summing ratings with group in MongoDB
require 'rubygems'
require 'mongo'
require 'pp'
db = Mongo::Connection.new.db('testing')
db.collections.each { |c| c.remove }
articles = db['articles']
ratings = db['ratings']
# Patch until MM 0.7.6 which fixes this:
# http://github.com/jnunemaker/mongomapper/commit/a721b66d9bc9ef78a5ce32dd2eb36d24abd4b953
module SetWithTypecasting
def self.included(model)
model.plugin SetWithTypecasting
end
module ClassMethods
def set(*args)
criteria, updates = criteria_and_keys_from_args(args)
@jnunemaker
jnunemaker / validates_existence_of.rb
Created May 7, 2010 13:23
Validates existence of in MongoMapper
module ValidatesExistenceOfPlugin
def self.included(model)
model.plugin ValidatesExistenceOfPlugin
end
module ClassMethods
def validates_existence_of(*args)
add_validations(args, ValidatesExistenceOfPlugin::ValidatesExistenceOf)
end
end
@jnunemaker
jnunemaker / stylesheet.rb
Created May 17, 2010 22:16
stripped down version of processors in the stylesheet model in harmonyapp.com
class Stylesheet
include MongoMapper::Document
class Processor
def self.errors
[]
end
attr_reader :contents