Skip to content

Instantly share code, notes, and snippets.

View npj's full-sized avatar

Peter Brindisi npj

  • SoundCloud
  • Berlin
View GitHub Profile
class Object
def method_missing(name, *args, &block)
fbomb = [102, 117, 99, 107, 105, 110, 103].inject("") { |s, c| s << c }
if respond_to?(n = name.to_s.gsub(/#{fbomb}_|_#{fbomb}/, ""))
send(n, *args, &block)
else
super(name, *args, &block)
end
end
end
def ancestors
[self] + (parent ? parent.ancestors : [])
end
@npj
npj / gist:757875
Created December 28, 2010 23:05
99 Bottles
lambda { |c| 99.downto(0) { |n| puts n == 0 ? "" << c[29] << c[30] << c[0] << c[37] << c[30] << c[32] << c[23] << c[0] << c[21] << c[30] << c[34] << c[34] << c[28] << c[23] << c[33] << c[0] << c[30] << c[24] << c[0] << c[21] << c[23] << c[23] << c[32] << c[0] << c[30] << c[29] << c[0] << c[34] << c[25] << c[23] << c[0] << c[36] << c[20] << c[28] << c[28] << c[1] : "" << (n < 10 ? "" : c[6 + (n / 10)]) << c[6 + (n % 10)] << c[0] << c[21] << c[30] << c[34] << c[34] << c[28] << c[23] << (n > 1 ? c[33] : "") << c[0] << c[30] << c[24] << c[0] << c[21] << c[23] << c[23] << c[32] << c[0] << c[30] << c[29] << c[0] << c[34] << c[25] << c[23] << c[0] << c[36] << c[20] << c[28] << c[28] << c[5] << c[5] << c[5] << c[0] << (n < 10 ? "" : c[6 + (n / 10)]) << c[6 + (n % 10)] << c[0] << c[21] << c[30] << c[34] << c[34] << c[28] << c[23] << (n > 1 ? c[33] : "") << c[0] << c[30] << c[24] << c[0] << c[21] << c[23] << c[23] << c[32] << c[1] << c[5] << c[5] << c[5] << c[0] << c[19] << c[20] << c[27] << c[23] << c[0] << c[30] << c
@npj
npj / gist:2044147
Created March 15, 2012 13:16
Rack IP Filter + HTTP Basic Auth
class FilterAuth < ::Rack::Auth::Basic
def initialize(app, options = { }, realm=nil, &authenticator)
super(app, realm, &authenticator)
@ip_filter = ::Rack::Access.new(app, options)
end
def call(env)
# only need to do auth if the ip filter blocks the request
response = @ip_filter.call(env)
@npj
npj / gist:2044141
Created March 15, 2012 13:15
Rack IP Filter + HTTP Basic Auth
MyApp::Application.config.middleware.use FilterAuth, "/" => [ "127.0.0.1" ] do |username, password|
username == CORRECT_USERNAME && password == CORRECT_PASSWORD
end
# app/controllers/post_controller.rb
class PostsController < ApplicationController
before_filter :find_post
protected
def find_post
@post = Post.find(Slug[params[:id]])
rescue ActiveRecord::RecordNotFound
$redis = Redis::Namespace.new("my_app", :redis => Redis.new)
> $redis.set("foo", "bar")
=> "OK"
> $redis.get("foo")
=> "bar"
> $redis.get("baz")
=> nil
> $redis.del("foo")
=> 1
> $redis.get("foo")
=> nil
> $redis["foo"] = "bar"
=> "bar"
> $redis["foo"]
=> "bar"
> $redis["baz"]
=> nil
> $redis.del("foo")
=> 1
> $redis["foo"]
=> nil
class Slug
class << self
def [](slug)
redis.hget(hash, slug)
end
private
def redis