Skip to content

Instantly share code, notes, and snippets.

View IronSavior's full-sized avatar

Erik Elmore IronSavior

View GitHub Profile
@IronSavior
IronSavior / set_ec2_cnames.rb
Last active August 29, 2015 14:06
Create CNAME records in Route 53 that refer to the running EC2 instance. Can be run at startup or on a cron job.
#!/bin/env ruby
# Create CNAME records in Route 53 that refer to the running EC2 instance
# Unless specified by the first argument, a default TTL of 300 is assumed.
# Instance tags specify CNAMES to be set:
# 'set_cname:0' => 'name.example.com', 'set_cname:1' => 'name2.example.com', etc
# Uses IAM credentials found in the AWS CLI config file:
# ~/.aws/config (use `aws configure` to create)
# Depends on ec2-metadata command to retrieve the current instance ID
@IronSavior
IronSavior / r53_zone_transfer.rb
Last active August 29, 2015 14:06
Poor man's domain transfer to Route 53
#!/bin/env ruby
### The Poor-Man's DNS Zone Transfer
# Copy specific records from a specific name service to Route 53, without using a conventional zone transfer mechanism.
#
# I realize this is unorthodox and might seem less-than-optimal for most use-cases. In my case, this is a means to
# help me continue to utilize Dreamhost's "Fully Hosted Domain" features even after migrating the authority of my zones
# as well as my name service to Route 53. The correct functioning of my web hosting and my email services depend on
# the ability of dreamhost to freely manage a known subset of the records in my zone. I chose to solve this by
# periodically copying that set of records from Dreamhost's name server for my domain into its actual authoritative
@IronSavior
IronSavior / indexed_collection.rb
Last active August 29, 2015 14:08
A collection class with built-in lookup tables.
# Author: Erik Elmore <erik@erikelmore.com>
# License: Public Domain
# A lazy-loaded collection with built-in lookup indexing.
# Good for using lots of memory.
class IndexedCollection
include Enumerable
DEFAULT_DATA = Array[].freeze
DEFAULT_LOADER = ->(){ DEFAULT_DATA }.freeze
@IronSavior
IronSavior / diffable.rb
Last active August 29, 2015 14:12
Simple diff methods for Hash or other Enumerable
# Author: Erik Elmore <erik@erikelmore.com>
# License: Public Domain
# Enable "diffing" and two-way transformations between collection objects
module Diffable
# Calculates the changes required to transform self to the given collection.
# @param b [Enumerable] The other collection object
# @return [Array] The Diff: A two-element change set representing items to exclude and items to include
def diff( b )
a, b = to_a, b.to_a
@IronSavior
IronSavior / client.rb
Last active August 29, 2015 14:12
Temporary SQS queues and SNS topics
# Author: Erik Elmore <erik@erikelmore.com>
# License: Public Domain
require 'securerandom'
require 'aws'
# Test the feasability of using temporary queues and topics as a means of receiving
# notifications from remote systems via SQS and SNS. TestClient creates either only a
# queue or both a queue and a topic before sending a request to the server. When the
# client wishes to receive its response directly in a temporary queue, the request
# body is the URL to its temporary queue. When the client wishes to be notified via a
@IronSavior
IronSavior / log_methods.rb
Last active August 29, 2015 14:17
Convenient logging methods
# Author: Erik Elmore <erik@erikelmore.com>
# License: Public Domain
# Convenience in logging
module LogMethods
def error( *args, &blk )
log :error, *args, &blk
end
private :error
@IronSavior
IronSavior / normalize_heredoc.rb
Created November 18, 2015 20:42
Normalize indents within heredocs so your code stays pretty
module NormalizeHeredoc
module_function
# Normalize indents, like for heredoc strings
def NormalizeIndent( msg )
msg = String(msg)
depth = msg.scan(/^\s*/).flatten.map(&:size).min
msg.gsub(/^\s{#{depth}}/, '')
end
end
@IronSavior
IronSavior / rate_limit.rb
Last active January 8, 2016 18:38
Simple Rate Limiter
# Enforces upper bound on the frequency of arbitrary actions.
# Thread-safe, but not terribly efficient.
class RateLimit
# Blocks passed to #enforce are executed with a frequency not exceeding the average of +limit+ calls per +period+ over
# +burst+ consecutive periods. Thread safe, but be advised there are no guarantees about the order or timeliness in
# which the given blocks are executed.
# @param limit [Numeric] Target average number of events per period
# @param period [Numeric] Duration of period in seconds (or equivalent, like ActiveSupport::Duration)
# @param burst [Numeric] Number of periods over which to average
def initialize( limit, period = 1, burst = 3 )
@IronSavior
IronSavior / y_combinator.rb
Last active May 5, 2016 06:54
Obligatory Y combinator exercise
# Fixed point combinator. Because reasons.
# @param [Block with arity 1] Receives recursion Proc, must return Proc representing function (any arity)
# @return [Proc]
def Y
fail unless block_given?
lambda{ |fn| fn.call fn }.call lambda{ |fn|
lambda{ |*args|
(yield fn.call fn).call *args
}
}
@IronSavior
IronSavior / hashlike_fetch_cache.rb
Last active May 11, 2016 02:11
Fun with Hash-like objects
class HashlikeFetchCache
include HashlikeOverlay
def initialize( context )
@overlay = Hash[]
@context = context
freeze
end
def []( key )