Skip to content

Instantly share code, notes, and snippets.

@bmarini
bmarini / redis_store.rb
Created July 21, 2010 15:11
ActiveSupport::Cache::RedisStore, a drop in replacement for MemCacheStore
require 'redis'
# This is a direct port of ActiveSupport::Cache::MemCacheStore
# Drop this file in lib/active_support/cache/redis_store and in your env files
# add:
#
# config.cache_store = :redis_store
module ActiveSupport
module Cache
class SysAdmin
class Dsl
def self.evaluate(file)
builder = new
builder.instance_eval(file.read, "config", 1)
builder.to_definition
end
def initialize
@servers = []
require 'capistrano'
unless Capistrano::Configuration.respond_to?(:instance)
abort "capistrano/ext/multiserver requires Capistrano 2"
end
# Example Usage:
# servers.define :app1, 'user@app1.servers', :port => 9999
# servers.define :app2, 'user@app2.servers', :port => 9999
# servers.define :db1, 'user@db1.servers', :port => 9999
# servers.define :db2, 'user@db2.servers', :port => 9999
# Goal:
# Authenticate a params hash with an authenticity_token
require 'openssl'
secret = "9d0d287c71f2e2629ca34bdbc03772509b815a4d2ad6392c7a436249698a29f34d2883d3cb640d9fbdd0a7fc45b42c6fa293d6119d97aeb324c0b1eeafb51e19"
params = {:a => 'a', :b => 'b', :c => 'b'}
params[:_authenticity_token] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('SHA1'), secret, params.values.join)
p params
@bmarini
bmarini / bash_login.sh
Last active September 5, 2015 00:04
My current bash_login
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# rbenv support
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
#!/bin/bash
PROGNAME="/app/script/delayed_job"
RAILS_ENV="production"
USER="user"
HOME="/home/user"
start() {
echo "Starting $PROGNAME"
sudo -u $USER /usr/bin/env HOME=$HOME RAILS_ENV=$RAILS_ENV $PROGNAME start
}
require 'rubygems'
require 'ruport'
t = Table(%w[date vert a b])
t << ["2010-01-01", "life",10,20]
t << ["2010-01-01", "life",10,20]
t << ["2010-01-02", "life",10,20]
t << ["2010-01-03", "life",10,20]
t << ["2010-01-04", "life",10,20]
t << ["2010-01-01", "health",10,20]
require "net/http"
http = Net::HTTP.new("example.com")
http.open_timeout = 2
http.read_timeout = 3 # Must be greater than open_timeout
begin
http.start
begin
http.request_get("/whatever?") do |res|
res.read_body
end
class Hash
def map
inject({}) { |memo,(k,v)| memo[k] = yield(v); memo }
end
end
def past(interval)
interval.map { |v| v.abs * -1 }
end
module Bitmap
def self.bgr_to_rgba(raw_bgr_array, width, height, new_dimension)
width_padding = new_dimension - width
height_padding = new_dimension - height
# First convert to array of Pixel::BGR objects
raw_bgr_array.enum_for(:each_slice, 3).map { |a| Pixel::BGR.new(*a) }.
reverse. # Reverse it
map { |bgr| bgr.to_rgba }. # Map to array of Pixel::RGBA objects
enum_for(:each_slice, width).inject([]) { |memo, row| memo << row }. # Create a 2d array based on width