Skip to content

Instantly share code, notes, and snippets.

Avatar

Charles Barbier unixcharles

View GitHub Profile
@unixcharles
unixcharles / gilded_rose.rb
Last active Jun 23, 2021
guilded_rose_kata
View gilded_rose.rb
class ItemUpdater
class AgedBrie < ItemUpdater
def value_change
-super
end
end
class BackstagePass < ItemUpdater
def value_change
case sell_in
@unixcharles
unixcharles / hash_object.rb
Created Nov 15, 2018
Hash Driven Development
View hash_object.rb
class HashObject
def []=(method_name, implementation)
define_singleton_method(method_name, &implementation)
end
def [](method_name)
if method(method_name).arity == 0
method(method_name).call
else
method(method_name).curry
@unixcharles
unixcharles / rate_limiter.rb
Created May 2, 2017
30 lines rate limited
View rate_limiter.rb
# USAGE:
# rate_limiter = RateLimiter.new('throttle_key', limit: 5, ttl: 1.hour)
# rate_limiter.throttled { not_too_often }
#
class RateLimiter
attr_reader :topic, :limit, :ttl, :cache
def initialize(topic:, limit:, ttl:, cache: default_cache)
@topic, @limit, @ttl, @cache = topic, limit, ttl, cache
end
View wtf.rb
class Foo
def self.define_attribute(name)
class_eval %{
def #{name}(*args)
attribute(#{name.inspect}, *args)
end
}
end
@unixcharles
unixcharles / problem.rb
Created Oct 29, 2012
DelegateClass and "superclass mismatch for class" errors
View problem.rb
require 'delegate'
class Klass < DelegateClass(String)
# ...
end
class Klass < DelegateClass(String)
# Re-open the class
end
# => TypeError: superclass mismatch for class Klass
View heroku_env.rb
#!/usr/bin/env ruby -rubygems
reject_keys = %w(PATH STACK URL APP_NAME LOG_LEVEL)
reject_keys += %w(RACK_ENV RAILS_ENV NODE_ENV)
reject_keys += %w(COMMIT_HASH CONSOLE_AUTH LANG LAST_GIT_BY)
reject_keys += %w(REDISTOGO NEW_RELIC DATABASE MONGOHQ MEMCACHE SENDGRID)
reject_keys += (ENV['REJECT_ENV_KEYS'].nil? ? [] : ENV['REJECT_ENV_KEYS'].split(','))
keys_regexp = %r/#{reject_keys.join('|')}/i
configuration = `heroku config`.split("\n").inject({}) do |hash, line|
@unixcharles
unixcharles / redis_clean.rb
Created Aug 23, 2012
Clean up redis queue
View redis_clean.rb
queue_name = 'queue:queue_name'
len = Resque.redis.llen(queue_name)
deletion = Resque.redis.lrange(queue_name,0, len).select do |entry|
JSON.parse(entry)['args'][0] == "some_criterial" rescue nil
end
deletion.each do |entry|
puts entry
puts Resque.redis.lrem queue_name, -1, entry
@unixcharles
unixcharles / creditcard.coffee
Created May 11, 2012 — forked from masylum/creditcard.coffee
Credit Card number validation in Coffee Script
View creditcard.coffee
# Ported from https://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js
creditcard = (value)->
# accept only spaces, digits and dashes
return unless /[^0-9 \-]+/.test(value)
nCheck = nDigit = 0
bEven = false
checkDigit = (n) ->
@unixcharles
unixcharles / upload.rb
Created Feb 3, 2012
Batch upload to teambox.com
View upload.rb
#!/usr/bin/env ruby -rubygems
@api_token = api_token
@project_id = integer_value_or_permalink
@paths = [ array_of_file_path ]
require 'faraday' # >= 0.8
require 'faraday_middleware'
@conn = Faraday.new(:url => 'https://beta.teambox.com/') do |builder|
@unixcharles
unixcharles / backbone.iframe.sync.js
Created Oct 11, 2011
Backbone.sync implementation that use iframe + multipart/form-data
View backbone.iframe.sync.js
(function () {
Backbone.syncWithoutUpload = Backbone.sync
Backbone.syncWithUpload = function(method, model, options) {
// Create iframe
var iframe_id = 'file_upload_iframe_' + Date.now()
, iframe = jQuery('<iframe id="' + iframe_id + '" name="' + iframe_id + '" ></iframe>').hide()
// Create an hidden form
var authToken = jQuery('meta[name=csrf-token]').attr('content')
, authParam = jQuery('meta[name=csrf-param]').attr('content')