Skip to content

Instantly share code, notes, and snippets.

View alexisbernard's full-sized avatar

Alexis Bernard alexisbernard

View GitHub Profile

Keybase proof

I hereby claim:

  • I am alexisbernard on github.
  • I am alexis_bernard (https://keybase.io/alexis_bernard) on keybase.
  • I have a public key whose fingerprint is FCA3 D196 1FB9 7CF8 7EAD D95D 2712 9EF2 E8A7 447B

To claim this, I am signing this object:

@alexisbernard
alexisbernard / net_http_extension.rb
Created December 30, 2012 09:12
Convenient way to fetch URLs. It follows redirections and handles SSL.
# Convenient way to fetch URLs. It follows redirections and handles SSL. Usage:
# Net::HTTP.fetch_url('http://google.com')
# Net::HTTP.fetch_url('https://www.google.fr/?q=ruby')
module Net
class HTTP
def self.fetch_url(url, limit = 10)
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
url = URI.parse(url)
options = {use_ssl: url.scheme.downcase == 'https'}
request = Net::HTTP::Get.new(url.path.to_s + '?' + url.query.to_s)
@alexisbernard
alexisbernard / id_rsa.pub
Created September 6, 2012 07:44
Public key
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAznBfbfnCJMibMMPSI7Fxeo02Y9fgy7yrMVlON2AKhTB3QMhjUORqONRCQG6g1qHJX2Osg/NQ6kxZEU7pHC8aTCXpPnKMrSWz/73B3vSsfV2dZIp1q5pz35MGqCkXKhfM7xO8NHGbzzVOs9mNb0txWU1fPI9parPqCvosjFKN5uJDpMUKV3yVwqztsSgRp9hwMtErg5SkBeRbFYRl2Aa1pEmRwjwdQyS23B4uwus9cFE2xkR86FOkJu4/5Rr1WKmqOLFyObjk1MSwYqfmHtCgwO0GWB3OTYtQbBD/4UC159efKDKvivVqCnItzg2huHsSRr4JugnQ7AIpKhbnrIUx9w== alexis
@alexisbernard
alexisbernard / infinite_string_stub.rb
Created January 25, 2012 17:05
Infinite string stub
# InfiniteStringStub.new.foo.bar.baz # => 'baz'
class InfiniteStringStub < String
def method_missing(name)
InfiniteStringStub.new(name.to_s)
end
end
@alexisbernard
alexisbernard / prepare-commit-msg
Created January 3, 2012 17:21
Git hook to append Pivotal's story tag
#!/bin/sh
# File: .git/hooks/prepare-commit-msg
# Append the Pivotal's story tag at the end of the commit message.
# Your branch must be named like this: my_branch_12345.
story=$(git symbolic-ref HEAD|sed -rn 's/^.*_([[:digit:]]+)/\1/p') # -E instead of -r for Mac OS X
test $story && echo "[Story#$story]" >> $1
exit 0
$LOAD_PATH.unshift '/home/alexis/veritas/lib'
require 'veritas'
include Veritas
header = Relation::Header.new([[:name, String], [:is_admin, Integer], [:is_author, Integer]])
body = [['Foo', 1, 1], ['Bar', 1, 0], ['Baz', 0, 1]].each
users = Relation.new(header, body)
admins = Relation.new(header, body).restrict { |r| r[:is_admin].eq(1) }
@alexisbernard
alexisbernard / gist:270749
Created January 6, 2010 22:28
Fluent scopes
# Called dynamically named scopes on a model
module Fluent
module Scopes
def self.included(klass)
klass.extend(ClassMethods)
# The code below fixes the following statement:
# @project = Project.find(123)
# @project.developers.apply_scopes(filters).find
# Without the code below the condition where developers.project_id = 123 is erased
# by apply_scopes, because filters are built from the class Developer instead of
@alexisbernard
alexisbernard / gist:270744
Created January 6, 2010 22:27
Fluent factory
# Fluent Factory overrides the new class method to provide an intuitive factory.
# Instead of using
# MyFactory.new_instance(args)
# Which may return an instance of MyClass, you now use
# MyClass.new(args)
# The second way is more intuitive and the big advantage is to use it rails and
# inheritance.
#
# Read the full example below to understand how to use it.
# class Animal
@alexisbernard
alexisbernard / gist:270730
Created January 6, 2010 22:14
Generate random string
# Generates a random String
# 'abc'.rand(10) # => 'aacbacccbc'
class String
def rand(length)
(1..length).inject('') {|str, i| str << self[Kernel::rand(self.length)]}
end
end
@alexisbernard
alexisbernard / gist:270727
Created January 6, 2010 22:12
The root resource
# This enables the '/' resource:
# map.resources :knickers, :as => ''
#
# GET /blabla
# => KnickersController#show params = {:controller = 'knickers', :action => 'show, :id => 'blabla'}
module ActionController
module Resources
class Resource #:nodoc:
def path
@path ||= path_segment.blank? ? path_prefix.to_s : "#{path_prefix}/#{path_segment}"