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
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
}
require 'socket'
require 'open-uri'
require 'rubygems'
require 'servolux'
require 'nokogiri'
PATH = '/tmp/web_fetch.socket'
# Create a UNIX socket at the tmp PATH
@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
#!/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 / 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
@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 / 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 / explicit_cache.json
Created November 7, 2011 21:58
Examples of ES Filter Cache
{
"filter": {
"and": { "_cache": true, "filters": [
{ "or": { "_cache": true, "filters": [
{"prefix": { "_id": "http://www.example.com" }}
]}},
{ "not": { "_cache": true, "filter": { "or": { "_cache": true, "filters": [
{"term": { "_id": "http://www.example.com" }},
{"term": { "_id": "http://www.example.com/sitemap" }},
{
"filter": { "bool": {
"must": [
{ "prefix": { "_id": "http://www.example.com" }}
]
}}
}
@TwP
TwP / dw.rb
Created December 14, 2011 16:53
Test scripts for the directory_watcher gem
#!/usr/bin/env ruby
require 'rubygems'
$:.unshift 'lib'
Thread.abort_on_exception = true
require 'logging'
include Logging.globally
require 'directory_watcher'