Skip to content

Instantly share code, notes, and snippets.

@nel
nel / decode_cookie.rb
Created February 29, 2012 18:16
Session decoding in Rails
@nel
nel / ruby-contest.rb
Created January 12, 2012 16:47
Solution to the ruby chrismas contest http://contest.dimelo.com
#!/usr/bin/env ruby
# Submission for Ruby Christmas Contest (http://contest.dimelo.com/)
# Author: Adrien Jarthon (http://adrienjarthon.com)
data = {"login" => "defunkt",
"followers" => 4674,
"commits" => 8901,
"repositories" => [{"watchers" => 79, "forks" => 9, "name" => "choice"},
{"watchers" => 28, "forks" => 1, "name" => "mapreducerb"},
@nel
nel / xmas-contest-test-rules
Created January 9, 2012 13:55
Rules use to test chrismas contest program
repository = 5
repository = 1 if repository.watchers < 5 && repository.forks < 5
repository += 50 if repository.watchers > 20
repository += 50 if repository.forks > 20
repository = 100 if repository.watchers > 50 && repository.forks > 50
commit = 1
follower = 2
foo # NameError: undefined local variable or method `foo' for main:Object
foo = "bar" unless defined?(foo)
foo # => nil
@nel
nel / gist:1243778
Created September 26, 2011 23:39
Never ever do this in a Rack middleware, memory leak + security issue included
class MyMiddleWare
REDIRECT = [302, { 'Content-Type' => 'text/html; charset=utf-8', 'Location' => '/admin' }, []]
def initialize(app)
@app = app
end
def call(env)
if <blabla>
REDIRECT
@nel
nel / gist:1186564
Created September 1, 2011 16:23
Memcache Store with controlled expiration
module ActiveSupport
module Cache
class AtomicMemCacheStore < MemCacheStore
ALREADY_STORED = "STORED\r\n"
cattr_accessor :grace_period
@@grace_period = 3.minutes
def read(key, options = nil)
result = super
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
it "should detect text_part and remain consistent" do
m = Mail.new(:content_type => 'mixed/multipart')
p = Mail::Part.new(:content_type => 'mixed/alternative')
p.add_part(Mail::Part.new(:content_type => 'text/html', :body => 'HTML TEXT'))
p.add_part(Mail::Part.new(:content_type => 'text/plain', :body => 'PLAIN TEXT'))
a = Mail::Part.new(:content_type => 'text/plain', :body => 'signature')
m.add_part(p)
m.add_part(a)
m.text_part.body.decoded.should == 'PLAIN TEXT'
m.to_s
@nel
nel / active_record.rb
Created February 3, 2009 17:51
Manage ActiveRecord connection pool with rack handler
#This is a Rack middle to release the connections checkout from the connection pool
#in a rack multithreaded environment
# This is mandatory outside of ActionController, as ActiveRecord request you to handle the
# release of the connections into the pool. If you don't do it, every n thread (where n is
# the AR connection pool size) will be stucked waiting for a 5s AR empty pool timeout
module Rack
module ActiveRecord
def initialize(app)
@app = app
end