Skip to content

Instantly share code, notes, and snippets.

View baconpat's full-sized avatar

Patrick Bacon baconpat

View GitHub Profile
package com.atomicobject.misc;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
public Map<String, String> getData(final Integer id) {
return new LazyImmutableMap<String, String>(new Callable<Map<String, String>() {
public Map<String, String> call() throws Exception {
return externalService.getSomeData(id);
}
});
}
public class LazyImmutableMap<K,V> extends ForwardingMap<K,V> {
public static class AccessException extends RuntimeException {
public AccessException(Throwable cause) {
super(cause);
}
}
private final FutureTask<Map<K, V>> task;
public LazyImmutableMap(final Callable<Map<K,V>> eval) {
class ActiveDirectory
include EnvironmentConfigurable
configure_with "config/active_directory.yml"
def self.valid_credentials?(login, password)
ldap = Net::LDAP.new(
:host => config.host, # active_directory.atomicobject.com
:port => config.port, # 389
:base => config.base, # dc=atomicobject,dc=com
:auth => {
require 'ldap/server'
module TestLdapServer
def start_ldap_server
logger = Logger.new(File.join(Rails.root, "log", "ldap.log"))
logger.level = Logger::DEBUG
logger.datetime_format = "%H:%M:%S"
@ldap_credentials = {"cn=server,dc=atomicobject,dc=com" => "server password"}
@ldap_server = LDAP::Server.new({
class LdapOperation < LDAP::Server::Operation
def initialize(connection, message_id, valid_credentials, logger)
super(connection, message_id)
@logger = logger
@valid_credentials = valid_credentials
end
def simple_bind(version, dn, password)
@logger.info "Got a simple_bind version: #{version.inspect}, dn: #{dn.inspect}, password #{password.inspect}"
if version != 3
Before('@active_directory') do
start_ldap_server
end
After('@active_directory') do
stop_ldap_server
end
@baconpat
baconpat / gist:984896
Created May 21, 2011 21:02
S3 upload with EventMachine and happening
require 'happening'
EM.run do
EventMachine::add_periodic_timer(1) { print "." }
item = Happening::S3::Item.new('my_bucket',
"22mb_file",
:aws_access_key_id => 'MY_ACCESS_KEY',
:aws_secret_access_key => 'my_secret_key',
:permissions => 'public-read')
item = Happening::S3::Item.new('my_bucket', 'my_file', :protocol => "http", ...
var addToTheList = function($elem, value) {
var itemHtml = '<li>' + value + '</li>',
selector = ".list";
$elem.find(selector).andSelf().filter(selector).append(itemHtml);
};