Skip to content

Instantly share code, notes, and snippets.

@softr8
softr8 / gist:be3a08309dadaf947012
Created September 4, 2015 18:23
Backup github repos locally
require 'github_api'
require 'git'
client = Github.new oauth_token: 'TOKEN', org: 'ORG_NAME'
BASE_DIR ||= '../repos'
i = 1
while true
repos = client.repos.list(page: i)
@softr8
softr8 / responders_decorator.rb
Last active August 29, 2015 14:25
Fix spree vulnerability with old versions
#lib/spree/api/responders_decorator.rb
Spree::Api::Responders::AppResponder.class_eval do
def template
options[:default_template]
end
end
@softr8
softr8 / gist:d1d6195105ed25facc5d
Created August 27, 2014 23:21
Fonts without CDN in a Rails app
config.action_controller.asset_host = proc do |source|
if source =~ /(ttf|ttc|otf|eot|woff|svg)$/
"https://www.domain.com"
else
"https://cdn#{Digest::MD5.hexdigest(source).to_i(16) % 4 }.domain.com"
end
end
@softr8
softr8 / gist:6a650de7e505126cb082
Created July 31, 2014 20:32
Monitor puma workers
#!/bin/bash
MASTER_PID=`cat /home/deploy/app/shared/sockets/puma.state | grep pid | awk '{print $2}'`
PIDS=`ps auxwww | grep -v $MASTER_PID | grep [p]uma | awk '{print $2}'`
MAX_MEMORY=500000000
for pid in $PIDS
do
MEM_USAGE=`ls -l /proc/$pid/as | awk '{print $5}'`
if [ $MEM_USAGE -gt $MAX_MEMORY ]
then
echo "Memory $MEM_USAGE exceeded $MAX_MEMORY killing $pid"
@softr8
softr8 / gist:2bd96fc296505616adff
Created May 9, 2014 19:46
Auto retry 3 times a resque job without any dependency
#it retries only if Timeout is present
module HookTimeoutRetry
def on_failure_retry(e, *args)
key = "AutoRetry:#{self.name}:#{Digest::MD5.hexdigest(Resque.encode(args))}"
counter = Resque.redis.incr(key)
Resque.redis.expire(key, 180)
if [Timeout::Error, Redis::TimeoutError].include?(e.class) && counter <= 3
Rails.logger.info "Performing #{self} caused an exception (#{e}). Retrying..."
Resque.enqueue self, *args
@softr8
softr8 / gist:7043527
Created October 18, 2013 15:49
Measure gem loading time with bundler, add this at the top of your application.rb
if ENV['DEBUG_GEMLOADING']
Bundler::Runtime.class_eval do
def Kernel.require file
puts Benchmark.measure("#{file}") {
super
}.format("%t - %n: %r")
end
end
end
@softr8
softr8 / gist:7042584
Last active December 25, 2015 21:29
faster kaminary pagination view helper replacement
def faster_paginate collection
output = ''
if collection.num_pages > 1
output << "<nav class='pagination'>"
page_base = collection.current_page
if page_base > 4
output << content_tag(:span, class: 'first') do
link_to raw(t 'views.pagination.first'), url_for(params.merge(page: 1))
end
output << content_tag(:span, class: 'page gap') { '... '}
@softr8
softr8 / gist:6625828
Created September 19, 2013 16:16
hash ._magic accessor
class Hash
def method_missing( symbol, *args )
a, method = symbol.to_s.split('_')
if method
if self.has_key?(method.to_sym) || self.has_key?(method)
self[method.to_sym] || self[method]
else
if method.include?('=')
key = method.split('=').first.strip
if has_key?(key)
@softr8
softr8 / gist:6622944
Created September 19, 2013 12:48
pascal's triangle in ruby
def pascalsTriangle(n)
(1..n).inject([]) do |final, level|
final << (1..level).inject([]) do |horizontal, index|
if index == 1 || index == level
horizontal << 1
else
horizontal << (final[level - 2][index - 2]) + (final[level - 2][index - 1])
end
end
final
@softr8
softr8 / gist:6539707
Last active December 22, 2015 22:19
Roman to integers in ruby
def solution(roman)
roman.scan(/IV|IX|CM|XC|I|V|L|X|C|M|D/).inject(0) do |final, current|
final += case current
when 'IV' then 4
when 'IX' then 9
when 'XC' then 90
when 'CM' then 900
when 'I' then 1
when 'V' then 5
when 'X' then 10