Skip to content

Instantly share code, notes, and snippets.

View TwP's full-sized avatar
🛰️
Spacing Out

Tim Pease TwP

🛰️
Spacing Out
View GitHub Profile
@TwP
TwP / demo_server.rb
Created October 20, 2011 17:14
Enter debug mode via USR1 signal
require 'servolux'
require 'logging'
# This is a simple demonstration server that shows how to register a signal handler
# for toggling the log level of your application. This is very useful in a production
# application where you want to enable debug messages for a brief period of time. The
# Servolux::Server class looks for a few special methods and registers them as signal
# handlers. Here, the "usr1" method will be called when the USR1 signal is sent to the
# process.
@TwP
TwP / monkey_proof.rb
Created September 13, 2011 04:27
monkey-proof your ruby code
# This is a very simple-minded and inelegant way to prevent others from
# modifying (monkey patching) your code. A global method "monkey_proof" is
# provided that can be used to prevent monkey patching on most any Object or
# Class.
#
moddule MonkeyProof
VERSION = '1.0.0'
def self.included( other )
@TwP
TwP / concat.rb
Created September 9, 2011 16:23 — forked from copiousfreetime/concat.rb
#!/usr/bin/env
# Possibly one of the faster wasy to concatenate two files in ruby.
# Another way might be to use the lower level IO#sysread, IO#syswrite methods
# I think the main benefit in this case is the reusing of the String instance as
# the buffer.
dest = ARGV.shift
src = ARGV.shift
#!/usr/bin/env ruby
require 'rubygems'
Thread.abort_on_exception = true
require 'directory_watcher'
STDOUT.sync = true
puts "Directory Watcher v #{DirectoryWatcher.version}"
@TwP
TwP / gist:981176
Created May 19, 2011 16:29
A simple rack middleware that allows you to post JSON body data. We are using the YAJL json library for ruby. Feel free to substitute your own.
require 'yajl'
module Rack
# A Rack middleware for parsing POST/PUT body data when Content-Type is
# <tt>application/json</tt>.
#
class JsonPostBody
require 'socket'
require 'open-uri'
require 'rubygems'
require 'servolux'
require 'nokogiri'
PATH = '/tmp/web_fetch.socket'
# Create a UNIX socket at the tmp PATH
def fix_escape( str )
str.gsub(%r/\\(\d+|[nr])/) { |match|
case $1
when 'n': "\n"
when 'r': "\r"
else
[$1.to_i(8)].pack('C')
end
}
# Be sure to restart your server when you modify this file
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.8' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
require 'logging-rails'
Rails::Initializer.run do |config|
# A time interval represents a period of time between two instants.
# Intervals are inclusive of the start instant and exclusive of the end.
# The end instant is always greater than or equal to the start instant.
#
# Intervals have a fixed duration (or length) seconds. This is the
# difference between the start and end instants.
#
# Methods that are passed an interval as a parameter will treat +nil+ as a
# zero length interval at the current instant in time.
#
class FastOpenObject
def method_missing( symbol, *args, &block )
eigenclass = class << self; self; end
eigenclass.class_eval <<-__
def #{symbol}
@#{symbol} ||= Hash.new {|h,k| h[k] = Hash.new(0)}
end