Skip to content

Instantly share code, notes, and snippets.

View jazzytomato's full-sized avatar
🎶
🎷 🍅

Tom Haratyk jazzytomato

🎶
🎷 🍅
View GitHub Profile
# troll your friends and enemies
class Fixnum
alias_method :add, :+
def +(other); rand(10) == 0 ? self * other : self.add(other); end
end
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
@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)] }]
@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 / 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
# Example for replace spec files modules:
module MyFirstModule
module MySecondModule
describe Something do
# code
end
end
end
# Enumerable extensions
module Enumerable
# this will be able to handle a pool of workers (actors)
# can be useful to have a pool to limit the amount of concurrent work
class ParallelWorker
POOL_SIZE = ENV.fetch('CELLULOID_POOL_SIZE', [Celluloid.cores, 2].max).to_i
include Celluloid
def yielder(element, block)
block.call(element)
end
@jazzytomato
jazzytomato / spec_middleware.clj
Last active April 21, 2017 17:27
Ring middleware for parsing query params with clojure.spec
(defn wrap-query-params-spec [h]
"Try to parse query params with clojure spec. Look for a spec named like 'query-params/[uri]'
If found and the spec does not conform, return the explained message"
(fn [req]
(try
(let [query-spec-k (keyword (str "query-params" (:uri req)))
query-params (keywordize-keys (:query-params req))
conformed-query-params (s/conform query-spec-k query-params)]
(if (= :clojure.spec/invalid conformed-query-params)
(->

Keybase proof

I hereby claim:

  • I am jazzytomato on github.
  • I am jazzytomato (https://keybase.io/jazzytomato) on keybase.
  • I have a public key ASAmqKM9EXyy5yLsm9tLpJEObivFIzwlxe1tq18KEFvLywo

To claim this, I am signing this object:

# Local tunnel to connect to remote DB via bastion ec2 instance
ssh -N -L 5433:DBHOSTNAME.eu-west-1.rds.amazonaws.com:5432 tom@ecX-XX-XXX-XXX-XXX.eu-west-1.compute.amazonaws.com
# then connects to db locally with localhost:5433