Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@romanbsd
romanbsd / static_gzip.rb
Created May 27, 2011 12:06
Patch Rack::File to look for pre-gzipped files (mimic nginx's gzip_static behavior)
# Patch Rack::File to support pre-gzipped files
module Rack
class File
def _call_with_static_gzip(env)
@path_info = Utils.unescape(env["PATH_INFO"])
return forbidden if @path_info.include? ".."
@path = F.join(@root, @path_info) + '.gz'
if F.file?(@path) && F.readable?(@path)
serving
@romanbsd
romanbsd / request_with_retry.rb
Created May 27, 2011 15:18
Retry HTTP request if connection was reset
require 'net/http'
class Net::HTTP
def transport_request_with_retry(req)
# First attempt will be after 100ms
delay = 0.1
begin
transport_request_without_retry(req)
rescue Errno::ECONNRESET => e
if delay < 4
Kernel.sleep(delay)
@romanbsd
romanbsd / colored_logger.rb
Created December 22, 2011 11:10
Color logging for Mongoid
require 'logger'
class ColoredLogger < Logger
WHITE = "\e[37m"
CYAN = "\e[36m"
MAGENTA = "\e[35m"
BLUE = "\e[34m"
YELLOW = "\e[33m"
GREEN = "\e[32m"
RED = "\e[31m"
BLACK = "\e[30m"
@romanbsd
romanbsd / delete_by_query.rb
Created April 9, 2012 21:51
tire delete_by_query (ElasticSearch
# Temporary extension. This will be merged into Tire eventually.
module RestClient
def self.delete_with_payload(url, payload, headers={}, &block)
Request.execute(:method => :delete, :url => url, :payload => payload, :headers => headers, &block)
end
end
module Tire
module HTTP
@romanbsd
romanbsd / stathat.rb
Created May 22, 2012 13:11
Stathat API using Faraday
require 'faraday'
module StatHat
module API
extend self
CLASSIC_VALUE_URL = "http://api.stathat.com/v".freeze
CLASSIC_COUNT_URL = "http://api.stathat.com/c".freeze
EZ_URL = "http://api.stathat.com/ez".freeze
def post_value(stat_key, user_key, value)
@romanbsd
romanbsd / rack_timeout.rb
Created August 8, 2012 12:01
rack timeout
require 'timeout'
module Rack
class Timeout
def initialize(app, timeout = 5)
@app, @timeout = app, timeout
end
def call(env)
::Timeout.timeout(@timeout, ::Timeout::Error) { @app.call(env) }
rescue ::Timeout::Error
@romanbsd
romanbsd / ruby-ossl.patch
Created September 11, 2012 10:19
Ruby 1.9.3 openssl dynamic modules support
--- ext/openssl/extconf.rb.orig 2012-09-11 13:32:19.000000000 +0400
+++ ext/openssl/extconf.rb 2012-09-11 13:32:37.000000000 +0400
@@ -115,6 +115,7 @@
have_func("ENGINE_get_digest")
have_func("ENGINE_get_cipher")
have_func("ENGINE_cleanup")
+ have_func("ENGINE_load_dynamic")
have_func("ENGINE_load_4758cca")
have_func("ENGINE_load_aep")
have_func("ENGINE_load_atalla")
@romanbsd
romanbsd / gzip.rb
Created October 15, 2012 13:11
Faraday gzip response middleware
require 'faraday'
require 'zlib'
module FaradayMiddleware
class Gzip < Faraday::Response::Middleware
def on_complete(env)
encoding = env[:response_headers]['content-encoding'].to_s.downcase
case encoding
when 'gzip'
@romanbsd
romanbsd / compressed.rb
Created October 15, 2012 13:12
Compressed fields in Mongoid
require 'zlib'
module Mongoid
module Fields
module Compressed
extend ActiveSupport::Concern
class Field
include Mongoid::Fields::Serializable
@romanbsd
romanbsd / dendrogram.rb
Created October 25, 2012 15:07
dendrogram
class DrawTree
require 'RMagick'
def drawdendrogram(tree, labels={}, file='tree.jpg')
h = tree.width * 20
w = 1200
scaling = (w-150).to_f / tree.depth
canvas = Magick::Image.new(w, h)
draw = Magick::Draw.new
draw.line(0,h/2,10,h/2)