Skip to content

Instantly share code, notes, and snippets.

View mindscratch's full-sized avatar

Craig Wickesser mindscratch

View GitHub Profile
@torsten
torsten / proxy.rb
Last active April 30, 2024 17:53
A quick HTTP proxy server in Ruby.
#!/usr/bin/env ruby
# A quick and dirty implementation of an HTTP proxy server in Ruby
# because I did not want to install anything.
#
# Copyright (C) 2009-2014 Torsten Becker <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
@jed
jed / base64.js
Created November 19, 2009 23:26
a base64 adaptation for use with node.js
/* adapted by Jed Schmidt for use as a node.js module */
this.encode = base64encode;
this.decode = base64decode;
this.chars = function( string ) {
base64EncodeChars = string || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
base64DecodeChars = [];
for ( var i = 128; i--; ) {
if ( base64DecodeChars[ i ] === undefined )
@indirect
indirect / Gemfile.rb
Created June 18, 2010 19:05
Platform-specific Gemfile example
source :gemcutter
platforms :ruby_18, :ruby_19, :rbx do
gem 'fast-xml-parser', :require => 'xml_parser'
end
platforms :jruby do
gem 'xml-parser', :require => 'xml_parser'
end
@ejucovy
ejucovy / graphael-linechart-legend.js
Created June 24, 2010 16:15
manually building a legend for a g.raphael linechart
raph = Raphael([..]);
chart = raph.g.linechart([..]);
var labels = ["first variable", "second variable", "third variable"];
chart.labels = raph.set();
var x = 15; var h = 5;
for( var i = 0; i < labels.length; ++i ) {
var clr = chart.lines[i].attr("stroke");
chart.labels.push(raph.set());
chart.labels[i].push(raph.g["disc"](x + 5, h, 5)
var fromBase64 = function(str) {
return (new Buffer(str || "", "base64")).toString("ascii");
};
@mnutt
mnutt / Instrument Anything in Rails 3.md
Created September 6, 2010 06:50
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

function Person() {
// properties and validations
attr(
{ id:Number, unique:true, nullable:false },
{ email:String, unique:true, nullable:false, min:1, max:55, format:'[a-b]' },
{ salt:String },
{ pswd:String },
{ active:Boolean, init:false },
{ tags:Array }
@octplane
octplane / mongo_pubsub.rb
Created November 9, 2010 16:17
Simple Pub/Sub system using MongoDB, capped collections and tailable cursors in ruby
require 'rubygems'
require 'mongo'
module MongoPubSub
QUEUES_COLLECTION = 'queues'
class EndSubscriptionException < Exception; end
class Publisher
def initialize(queue_name, mongo_connection)
# Initialize queue collection as a capped collection
if not mongo_connection[QUEUES_COLLECTION].collection_names.include?(queue_name)
@mislav
mislav / gist:938183
Created April 23, 2011 02:28
Faraday SSL example
connection = Faraday::Connection.new('http://example.com') do |builder|
builder.request :url_encoded # for POST/PUT params
builder.adapter :net_http
end
# same as above, short form:
connection = Faraday.new 'http://example.com'
# GET
connection.get '/posts'
@nicksieger
nicksieger / jruby-rack-x-sendfile.rb
Created May 5, 2011 22:22
X-Sendfile middleware for JRuby-Rack 1.0.8 or greater
class JRubyRackXSendfile
def initialize(app)
@app = app
end
def call(env)
result, headers, body = @app.call(env)
if headers['X-Sendfile']
[result, headers, File.new(headers.delete('X-Sendfile'))]
else