Skip to content

Instantly share code, notes, and snippets.

View robertsosinski's full-sized avatar

Robert Sosinski robertsosinski

View GitHub Profile
@robertsosinski
robertsosinski / oauth.rb
Created March 26, 2011 23:25
Step-by-step processes of creating an OAuth Authorization Header
require 'uri'
require 'base64'
require 'openssl'
# NOTE: Oauth uses a specific unreserved character list for URL encoding.
def url_encode(input)
unreserved = '-._~0-9A-Za-z' # These are the only characters that should not be encoded.
URI.escape(input, Regexp.new("[^#{unreserved}]"))
end
@robertsosinski
robertsosinski / application.html.haml
Created March 28, 2011 04:28
Specify controller and action for jQuery load event.
!!!
%html
%head
%body{:class => params[:controller], :id => params[:action]}
= yield
@robertsosinski
robertsosinski / caching_with_request_forgery_protection.rb
Created April 27, 2011 23:14
Rack middleware and form tag patch to insert csrf tokens into cached pages
# NOTE: Remember to add "before_filter :form_authenticity_token" to ApplicationController
# and patch ActionView::ActionView::FormTagHelper#token_tag in form_tag_helper.rb
class CachingWithRequestForgeryProtection
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
@robertsosinski
robertsosinski / gist:945453
Created April 27, 2011 23:20
Rack::Cache helpers
# Expiration Methods
cache_control :public, :max_age => 10.seconds
expires 10.seconds.from_now
# Validation Methods
last_modified current_user.updated_at
etag current_user.etag
@robertsosinski
robertsosinski / document.rb
Created May 9, 2011 21:54
Mongo typecasting library for defining field and embed types.
module Document
class CannotLoadConfig < Exception; end
class DocumentNotValid < Exception; end
class DocumentNotFound < Exception; end
class InvalidFieldType < Exception; end
class InvalidEmbedType < Exception; end
def self.hookup
@config ||= YAML.load(File.read(Rails.root + "config" + "mongo.yml"))[Rails.env]
@db ||= Mongo::Connection.new(@config["host"], @config["port"]).db(@config["database"])
@robertsosinski
robertsosinski / positioning.rb
Created May 18, 2011 16:03
A sequel plugin that allows positioning with options.
module Sequel
module Plugins
module Positioning
def self.apply(model, options = {})
options[:field] ||= :position
field = options[:field]
scope = options[:scope]
if scope
@robertsosinski
robertsosinski / gist:1002440
Created June 1, 2011 14:49
Correlation ID
[Time.now.to_f.to_s.ljust(16, '0'), Digest::SHA1.hexdigest("request" + rand(0xffffffff).to_s), "#{12}-#{3456}"].join(":")
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\u@\h:\W\$(parse_git_branch)$ "
@robertsosinski
robertsosinski / gist:1018013
Created June 9, 2011 23:50
Get quick information about your database in PostgreSQL
select
psut.schemaname,
pc.relname,
pg_size_pretty(pg_relation_size(pc.relname::varchar)) relsize_pret,
pg_size_pretty(pg_total_relation_size(pc.relname::varchar)) total_relsize_pret,
pg_relation_size(pc.relname::varchar) relsize,
pg_total_relation_size(pc.relname::varchar) total_relsize,
pc.reltuples::integer,
pc.relpages,
coalesce(round((8000 / (nullif(pc.reltuples, 0) / nullif(pc.relpages, 0)))), 0) avg_tuplesize,
@robertsosinski
robertsosinski / fulltext-plpgsql.sql
Created June 10, 2011 15:51
Full-Text Search stored procedure
create or replace function p_search(search_query text, search_limit int default 10, search_page int default 1)
returns table (
id int,
model varchar,
search_rank real,
headlined_name text,
headlined_description text,
headlined_blank text
) as $$
declare