Skip to content

Instantly share code, notes, and snippets.

@wildchild
wildchild / devise.rb
Created May 24, 2010 18:07
Do you like it?
module ActionDispatch::Routing
class Mapper
def authenticate(scope = Devise.default_scope)
constraint = lambda do |request|
request.env["warden"].authenticate!(:scope => scope)
end
constraints(constraint) do
yield
end
@wildchild
wildchild / couchdb_attachments.rb
Created May 20, 2010 07:32
Serving CouchDB attachments with Rails 3
module CouchDBAttachments
def attachment(*args)
lambda do |env|
request = ActionDispatch::Request.new(env)
doc = DocWithAttachments.get(request.params[:doc])
serve_attachment(doc, request.params[:path])
end
end
private
if defined?(Rails)
class ActionController::Base
include RailsWarden::Mixins::HelperMethods
include RailsWarden::Mixins::ControllerOnlyMethods
end
module ApplicationHelper
include RailsWarden::Mixins::HelperMethods
end
end
@wildchild
wildchild / ip2country.erl
Created September 4, 2009 14:44
Extremely fast lookup country by IP address
%% This is http://code.google.com/p/ip2country-erlang on steroids.
%% Database: http://software77.net/cgi-bin/ip-country/geo-ip.pl?action=download
%% Speed: over 200000 lookups in a second.
-module(ip2country).
-compile(export_all).
for_each_line_in_file(Name, Proc, Acc) ->
{ok, Device} = file:open(Name, [read]),
for_each_line(Device, Proc, Acc).
radius.erl
has_attribute(A) when is_list(A) ->
X = radius:lookup_attribute(A),
Code = ...
has_attribute(Code);
has_attribute(A) when is_integer(A) ->
...
has_attribute({Vendor, Specific}) ->