Skip to content

Instantly share code, notes, and snippets.

@tomlea
tomlea / proxy.rb
Created October 11, 2009 21:11
Rack Middleware to proxy requests to a remote server. This is usefull for proxying AJAX request to remote services.
require "net/http"
require "enumerator"
# Example Usage:
#
# use Rack::Proxy do |req|
# if req.path =~ %r{^/remote/service.php$}
# URI.parse("http://remote-service-provider.com/service-end-point.php?#{req.query}")
# end
# end
@tomlea
tomlea / gist:207938
Created October 11, 2009 22:55
This is very rough and ready.
require "net/http"
# Example Usage:
#
# use Rack::Proxy do |req|
# if req.path =~ %r{^/remote/service.php$}
# URI.parse("http://remote-service-provider.com/service-end-point.php?#{req.query}")
# end
# end
#
class Rack::ProcTitle
F = ::File
PROGNAME = F.basename($0)
def initialize(app)
@app = app
@appname = Dir.pwd.split('/').reverse.
find { |name| name !~ /^(\d+|current|releases)$/ } || PROGNAME
@requests = 0
$0 = "#{PROGNAME} [#{@appname}] init ..."
@tomlea
tomlea / hero.sh
Created August 1, 2013 16:02
When you have multiple heroku apps, for one repo, you're going to need a hero! hero production run rake db:migrate
function _hero_getRemoteNames(){
git config --get-regexp remote.*.url heroku.com |
sed -E 's/^remote\.([^\.]*)\.url .*$/\1/'
}
function _hero_herokuAppFor(){
git config --get remote.$1.url |
sed -E 's/^git@heroku.com:([^.]*)\.git/\1/'
}
@tomlea
tomlea / quick_bulk_select.rb
Created January 6, 2012 12:06
Select Some Columns
# Introduction:
We need to select large numbers of items (300k or more), based on active record associations (and selectors, and all that faff), but we only care about the ids.
This is easily solved by bla_bla_bla_scope.map(&:id). This however takes about 16s of CPU grinding ruby to get to the final answer via AR objects. AR objects that contain only an ID.
Why not just use the SQL generated by the scopes and other voodoo to just pump the SQL through? That's what this does, in the most complex way imaginable!
# Usage:
@tomlea
tomlea / start_sql_servers.sh
Created March 3, 2011 13:51
Start a MySQL master/slave pair.
rm -R $HOME/Desktop/server-{1,2}
mkdir -p $HOME/Desktop/server-{1,2}
mysql_install_db --datadir=$HOME/Desktop/server-1 && mysqld --server-id 1 -h ~/Desktop/server-1 --socket ~/Desktop/server-1/mysql.sock -P 10001 --log-bin &
mysql_install_db --datadir=$HOME/Desktop/server-2 && mysqld --server-id 2 -h ~/Desktop/server-2 --socket ~/Desktop/server-2/mysql.sock -P 10002 --master-port=10001 --master-host=127.0.0.1 --master-user=root &
alias sql-master="mysql -h127.0.0.1 -P10001 -uroot"
alias sql-slave="mysql -h127.0.0.1 -P10002 -uroot"
alias sql-master-admin="mysqladmin -h127.0.0.1 -P10001 -uroot"
javascript:
if( !window.goviasGrid ){
window.goviasGrid = document.createElement("div");
document.getElementsByTagName("body")[0].appendChild(window.goviasGrid);
window.goviasGrid.style.background = "url(http://tomlea.co.uk/assets/the-grid.png) repeat-y 50% 0";
window.goviasGrid.style.width = "100%";
window.goviasGrid.style.height = "100%";
window.goviasGrid.style.position = "fixed";
window.goviasGrid.style.top = "0";
window.goviasGrid.style.bottom = "0";
AU-Colleen
AU-Jon
BE-Hendrik
BE-Minna
BE-Sofie
BR-Lygia
CA-Antonine
CA-Felix
CN-Pan
CZ-Zdenech
# A secure S3 storage engine for Paperclip.
# Usage:
#
# require "paperclip/storage/s3secure"
# has_attached_file :image, :storage => :S3secure, …
module Paperclip::Storage::S3secure
def self.extended base
base.extend(Paperclip::Storage::S3)
base.options[:s3_permissions] = :private
def sort_order
# Google was indexing "random" search orders, it parsed our javascript, badly.
SORT_METHODS[params[:sort_order]] or raise_redirect_to(@product, :status => 301)
end
def segment
segment = params[:segment]
segment = segment.to_sym if %w[all other].include?(segment)
segment = nil if segment == :all
# Bad or expired segments need 301ing