Skip to content

Instantly share code, notes, and snippets.

@xaviershay
xaviershay / LdapAuthenticator.java
Created July 24, 2012 03:27
LDAP Authentication for dropwizard
package com.squareup.alcatraz.auth;
import com.google.common.base.Optional;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
#!/usr/bin/env ruby
# Given an absolute path to a ruby file and an optional RVM directory, locates
# both an `.rvmrc` and a `Gemfile` and runs the script with both.
script = ARGV.shift
$rvm_path = ARGV.shift.to_s
rvm_script = "#{$rvm_path}/scripts/rvm"
$rvm_path = nil unless File.exists?(rvm_script)
bundler = nil
@xaviershay
xaviershay / database_cache_store.rb
Created June 12, 2012 00:14
DatabaseCacheStore
# Implementation of Rails.cache backed by our database. We don't have high
# performance or space requirements, so using existing infrastructure for our
# caching needs is desirable.
class DatabaseCacheStore < ActiveSupport::Cache::Store
class CacheEntry < ActiveRecord::Base
end
def self.migrate_up(m)
m.create_table :cache_entries do |t|
t.string :key, :null => false

Lofthack

Let's create. I'm making dahl to eat.

When Thursday 19th July, 6:30pm to 10

Where 17th & Harrison, SF (I'll send you exact instructions.)

What Code, learning, words, reality, whatever

@xaviershay
xaviershay / half_rspec_rails.rb
Created April 14, 2012 21:32
For GOOS + Rails
# By default, rspec/rails tags all specs in spec/integration as request specs,
# which is not what we want. There does not appear to be a way to disable this
# behaviour, so this file is a copy of rspec/rails.rb with this default
# behaviour commented out.
require 'rspec/core'
RSpec::configure do |c|
c.backtrace_clean_patterns << /vendor\//
c.backtrace_clean_patterns << /lib\/rspec\/rails/
end
@xaviershay
xaviershay / gist:1733416
Created February 3, 2012 22:37
Rails connection issue
# Old way, creates too many connections, breaks transactional fixtures.
# More broken on 3.2 than 3.1, didn't identify exact change though.
#
# Our suite was passing on 3.1 by accident when we initially tried to upgrade to 3.2,
# which broke everything. Since then, have added new specs that exposed the problem in 3.1.
#
# This is probably not a rails bug, but was surprising.
module OtherModel
def self.included(klass)
klass.instance_eval do
class A
def say_name
puts name
end
private
def name; "A"; end
end
@xaviershay
xaviershay / threshold.sh
Created December 19, 2011 00:27
bash golf problem
#!/bin/bash
# Input:
# output this 6
# do not output this 5
# output this 7
#
# Output (and a non-zero exit code):
# output this 6
# output this 7
@xaviershay
xaviershay / http_client_spec.rb
Created December 13, 2011 02:19
Running a rack app in a thread for integration tests.
require 'integration_helper'
require 'rack'
require 'rack/handler/webrick'
describe HttpClient do
before :all do
@server = WEBrick::HTTPServer.new(
:Port => 9293,
:Logger => Rails.logger,
:AccessLog => Rails.logger
@xaviershay
xaviershay / transform_hash.rb
Created December 4, 2011 22:56
Ruby puzzle
describe '#transform_hash' do
it 'transforms a single entry hash' do
transform_hash(a: [1, 0]).should == [[1], [0]]
end
it 'transforms a double entry hash' do
transform_hash(a: [1, 0], b: [0, 1]).should == [[1, 0], [0, 1]]
end
it 'transforms a multiple entry hash' do