Skip to content

Instantly share code, notes, and snippets.

View jazzytomato's full-sized avatar
🎶
🎷 🍅

Tom Haratyk jazzytomato

🎶
🎷 🍅
View GitHub Profile
@jazzytomato
jazzytomato / worker_helper.rb
Created December 7, 2015 15:41
helper for the resque workers to interact with redis
module WorkerHelper
REDIS_EXPIRY_TIME = 20.minutes
def logger
unless @logger
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |severity, datetime, _progname, msg|
"[#{severity}] #{datetime}: #{msg}\n"
end
end
@jazzytomato
jazzytomato / to_bool.rb
Created December 4, 2015 12:13
string to boolean
def to_bool(str)
str == true || str =~ (/^(true|1)$/) ? true : false
end
@jazzytomato
jazzytomato / camel_snake_hash.rb
Created November 30, 2015 16:36
A helper for JSON interaction. Treat the hashes recursively
module Utilities
# A helper for JSON interaction. Treat the hashes recursively
module CamelSnakeHash
# camelize and stringify keys
def camelize_keys(value)
case value
when Array
value.map { |v| camelize_keys(v) }
when Hash
Hash[value.map { |k, v| [k.to_s.camelize(:upper), camelize_keys(v)] }]
def readable secs
return "0 minute" if secs < 60
secs /= 60
[[60, :minute], [24, :hour], [Float::INFINITY, :day]].map{ |count, name|
if secs > 0
secs, n = secs.divmod(count)
pluralize(n.to_i, name.to_s) if n > 0
end
}.compact.reverse.join(' ')
end
# troll your friends and enemies
class Fixnum
alias_method :add, :+
def +(other); rand(10) == 0 ? self * other : self.add(other); end
end