Skip to content

Instantly share code, notes, and snippets.

View kivanio's full-sized avatar
🏠
Working from home

Kivanio Barbosa kivanio

🏠
Working from home
View GitHub Profile
require 'net/dns/resolver'
# Custom Domain
#
# Require net-dns gem
#
# A Rack middleware to to resolve the custom domain to original subdomain
# for your multi telent application.
#
# It's all transperant to your application, it performs cname lookup and
#--
# Copyright (c) 2009 Szymon Kurcab szymon.kurcab@gmail.com>
#
# 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,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# Rack::Middleware that returns the number of models in the database for any call to a URL like modelname/count.
#
# Examples:
# /users/count
# => returns User.count in plain text
#
# /articles/1/comments/count.xml
# => returns Comment.count as XML, e.g.
# <comments type="integer">5</comments>
# Note: this does not return Articles.find(1).comments.count like it maybe should!
# Full code is at http://github.com/cwninja/rack-cache-buster
require 'digest/md5'
module Rack
class CacheBuster
def initialize(app, key, target_time = nil)
@app, @target_time = app, target_time
@key = "-"+Digest::MD5.hexdigest(key || "blank-key").freeze
@key_regexp = /#{@key}/.freeze
require 'nokogiri'
require 'open-uri'
gem 'maca-fork-csspool'
require 'csspool'
module InlineStyle
module Rack
class Middleware
#
require 'RMagick'
class WatermarkMe
def initialize(app, args)
@app = app
@watermark_text, @mime_types = *args
@mime_types ||= %w[image/jpeg image/png image/gif]
end
def call(env)
module Rack
class Censor
WORDS = [ 'shit', 'fuck', 'cock', 'cunt', 'cameltoe', 'mooseknuckle' ].map { |w| Regexp.new(w, Regexp::IGNORECASE) }.freeze
attr_reader :options, :request
def initialize(app, options={})
@app, @options = app, {
:replacement => '*****'
require 'geoip'
module Rack
# Rack::GeoIPCountry uses the geoip gem and the GeoIP database to lookup the country of a request by its IP address
# The database can be downloaded from:
# http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
#
# Usage:
# use Rack::GeoIPCountry, :db => "path/to/GeoIP.dat"
#
# This is actually available as a gem: gem install rack-rewrite
# Full source code including tests is on github: http://github.com/jtrupiano/rack-rewrite
module Rack
# A rack middleware for defining and applying rewrite rules. In many cases you
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
class Rewrite
def initialize(app, &rule_block)
@app = app
@rule_set = RuleSet.new
# BruteForceKilla
#
# A Rack middleware to limit requests by ip address, coded for fun as my first
# middleware, thanks http://coderack.org for giving me a reason :)
#
# For production use, one would want to make a memcache or redis tracker.
#
# options:
#
# :tracker => Class name of the tracker to use (default Memory (all there is for now!))