Skip to content

Instantly share code, notes, and snippets.

View eterps's full-sized avatar

Erik Terpstra eterps

View GitHub Profile
@eterps
eterps / encryption.rb
Created April 22, 2009 10:20
Simple encryption wrapper for Ruby
require 'openssl'
require 'digest/sha1'
require 'base64'
class Encryption
def self.encrypt(src, key, cipher = 'aes-256-cbc')
c = OpenSSL::Cipher::Cipher.new(cipher)
c.encrypt
c.key = Digest::SHA1.hexdigest(key)
e = c.update(src)
class DirTree < String
include Enumerable
def each
Dir.entries(self).reject{|n| %w[. ..].include? n}.each do |filename|
yield path = "#{self}/#{filename}"
DirTree.new(path).each{|n| yield n} if File.directory? path
end
end
end
@eterps
eterps / Sinatra
Created November 1, 2010 20:48
Play with Coffeescript / Sinatra
Install node.js:
# See: http://nodejs.org/#download
NJSVERSION=v0.2.4
cd ~/tmp && wget http://nodejs.org/dist/node-${NJSVERSION}.tar.gz && tar zxf node-${NJSVERSION}.tar.gz && cd node-${NJSVERSION}
./configure --prefix=/opt/node-${NJSVERSION} && make && make install && ln -s /opt/node-${NJSVERSION} /opt/node
ln -s /opt/node/bin/node ~/bin/node
vi .bashrc
..
export PATH=$PATH:/opt/node/bin
@eterps
eterps / mrisc.rb
Created July 8, 2011 11:07 — forked from pachacamac/mrisc.rb
A Simple Assembler Language and VM
#!/usr/bin/env ruby
class MRISC
def run(code)
tokens = code.gsub(/(\*.*?\*)|[^a-z0-9,-;@\._]/,'').split(';')
@vars,stack,i = {:_pc=>-1,:_oc=>0},[],0
tokens.map!{|t| t.chars.first=='@' ? (@vars[t.to_sym]=i-1;nil) : (i+=1;t.split(',').map{|e|numeric?(e) ? e.to_i : e.to_sym})}.compact!
while @vars[:_pc] < tokens.size-1
@vars[:_pc] += 1
@vars[:_oc] += 1
Gem::Specification.new do |s|
s.name = 'open-s3uri'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'Erik Terpstra'
s.email = '[email protected]'
s.summary = 'Opens an S3 uri'
s.description = 'Opens an S3 uri like open-uri does for http URLs'
s.files = ['open-s3uri.rb']
@eterps
eterps / config.ru
Created January 30, 2015 15:49
REST/hypermedia webservice in Ruby using Webmachine, Webmachine linking and Roar for HAL/JSON support
require 'webmachine/adapters/rack'
require_relative 'webservice'
run Webservice.adapter
backend s3 { .host = "s3.amazonaws.com"; .port = "80"; }
sub vcl_recv {
if (req.url ~ ".(css|gif|ico|jpg|jpeg|js|png|swf|txt)$") {
unset req.http.cookie;
unset req.http.cache-control;
unset req.http.pragma;
unset req.http.expires;
unset req.http.etag;
unset req.http.X-Forwarded-For;
@eterps
eterps / conversion_functions.rb
Created October 29, 2015 13:42
Ruby conversion functions pattern
# From Confident Ruby, page 72
module Graphics
module Conversions
module_function
def Point(*args)
case args.first
when Point then args.first
when Array then Point.new(*args.first)

Keybase proof

I hereby claim:

  • I am eterps on github.
  • I am eterps (https://keybase.io/eterps) on keybase.
  • I have a public key ASBdMN2hfTUxgMovEIPOEH4e9qAXplhPeYciuLAl2LnMKwo

To claim this, I am signing this object:

@eterps
eterps / event_sourcing.rb
Created March 22, 2016 11:11
Playing around with event sourcing principles
require 'yaml'
class Storage # Normally should be Redis or Kafka?
def self.events
@events ||= []
end
end
# Aggregates (models)