Skip to content

Instantly share code, notes, and snippets.

@sheldonh
sheldonh / binsearch.coffee
Last active August 29, 2015 14:17
Binary search worst case
binsearch = (a, t, probe) ->
min = 0
max = a.length - 1
while (min <= max)
i = Math.floor((min + max) / 2)
v = a[i]
probe? i, v
@sheldonh
sheldonh / application_context.yml
Last active August 29, 2015 14:16
Ruby dependency injection without Spring
---
beans:
invoice_service:
class: InvoiceService
method: constructor
arguments:
- bean:billing_provider
- bean:asset_provider
- dry_run: true
billing_provider:
@sheldonh
sheldonh / aget_at.rb
Created March 2, 2015 12:23
age_at.rb
#!/usr/bin/env ruby
#
# Taken from http://stackoverflow.com/questions/819263/2357790#2357790
def age_at(dob, date)
date.year - dob.year - ((date.month > dob.month || (date.month == dob.month && date.day >= dob.day)) ? 0 : 1)
end
require "date"
@sheldonh
sheldonh / gist:23cdc31dfb1835605be4
Created February 27, 2015 14:38
Decomposing fabrique
registry = ConstructorRegistry.new("My plugin registry")
type = ProviderClass
argument_adaptor = ArgumentAdaptor::PositionalArgument.new(:foo, [:bar], :baz)
constructor = Constructor.new(type, argument_adaptor)
registry.register(:provider1, constructor)
factory = Factory.new(registry)
provider = factory.create(:provider1, foo: "fooish", baz: "wombat")
@sheldonh
sheldonh / cow_factory.rb
Last active August 29, 2015 14:15
Ruby factory prototype
module Starjuice
module Factory
module Constructor
class Adaptor
def initialize(adaptor = nil)
@adaptor = adaptor || ->(properties) { [properties] }
end
@sheldonh
sheldonh / .kitchen.yml
Created January 15, 2015 18:57
Running test-kitchen from an EC2 instance
<%
require 'json'
require 'net/http'
json = Net::HTTP.get('169.254.169.254', '/latest/meta-data/iam/security-credentials/chef-lab-workstation')
creds = JSON.parse(json)
%>---
driver:
name: ec2
aws_access_key_id: <%= creds["AccessKeyId"] %>
@sheldonh
sheldonh / reboot-if-required.rb
Created January 15, 2015 17:04
recipes/reboot-if-required.rb
execute "reboot" do
only_if { ::File.exists?("/var/run/reboot-required") }
node.run_state['reboot'] = true
end
@sheldonh
sheldonh / gist:dd6a7244f35fb30069c9
Last active August 29, 2015 14:13
kitchen-ec2 .kitchen.yml
---
driver:
name: ec2
aws_access_key_id: ...
aws_secret_access_key: ...
aws_session_token: ...
aws_ssh_key_id: chef-lab
ssh_key: <%= ENV['HOME'] %>/.ssh/chef-lab.pem
subnet_id: subnet-3eb66215
vpc_id: vpc-1cf49679
@sheldonh
sheldonh / gist:589a636ef91b66e8c8a8
Last active August 29, 2015 14:08
Trying to encode ciphertext and its metadata as CMS
def self.encrypt(password, cleartext)
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
cipher.encrypt
iv = cipher.random_iv
salt = Time.now.nsec.to_s
iterations = ITERATIONS
key_len = cipher.key_len
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(password, salt, iterations, key_len)
ciphertext = cipher.update(cleartext) + cipher.final
return ciphertext
@sheldonh
sheldonh / my_plugin_spec.rb
Created November 4, 2014 09:14
How I'd like to use base library tests
require 'spec_helper'
require 'base_library/test/plugin_spec'
$LOAD_PATH.unshift('../../lib', __FILE__)
require 'base_library/plugin/my_plugin'
spec = BaseLibrary::Test::PluginSpec.new(BaseLibrary::Plugin::MyPlugin.new)
spec.run