Skip to content

Instantly share code, notes, and snippets.

pixeltrix / routes.rb
Created Oct 29, 2010
Examples of advanced Rails 3.0 routes
View routes.rb
Rails.application.routes.draw do
get '/(:locale)/products/(:category)/(page/:page).:extension',
:to => 'products#index',
:as => :products,
:constraints => {
:locale => /[a-z]{2}/,
:category => /.+?/,
:page => /\d+/
jtanium / Rakefile
Created Nov 29, 2010
Service Oriented Design with Ruby and Rails (examples that actually work)
View Rakefile
require 'rubygems'
require 'active_record'
require 'sinatra'
require 'models/user'
require 'logger'
env_index = ARGV.index("-e")
env_arg = ARGV[env_index+1] if env_index
env = env_arg || ENV["SINATRA_ENV"] || "development"
databases = YAML.load_file("config/database.yml")
derekwyatt / updateboundingbox.vim
Created Sep 27, 2011
An example of integrating a simple shell script into Vim
View updateboundingbox.vim
" When I'm editing some LaTeX, I use PDF files to handle any inserted images and
" LaTeX has some difficulty lining them up right, so I explicitly state the
" viewport. To get the bounding box from the PDF file, I have a script called
" 'getbb'. This function is /very/ specific to my needs. It pulls the filename
" from the current line, which always looks something like this:
" \includegraphics[scale=0.5, viewport = 40 39 703 153]{target/filename.pdf}
" Pulls out the filename (i.e. target/filename.pdf), runs 'getbb' on that and
" inserts the output back overtop of the "viewport = 40 39 703 153".
fideloper /
Created Nov 1, 2012
Ubuntu - Install forwarder
# Splunk Storm
$ wget [ Download Splunk url ]
$ gunzip -c splunkforwarder-5.0-140868-Linux-x86_64.tgz | tar -xvpf -
$ ./splunkforwarder/bin/splunk start
$ ./splunkforwarder/bin/splunk install app stormforwarder_3de50bc823a711e2b4a81231390e9c34.spl -auth admin:changeme
$ ./splunkforwarder/bin/splunk login -auth admin:changeme
$ ./splunkforwarder/bin/splunk edit user admin -password whatever_you_want
$ ./splunkforwarder/bin/splunk add forward-server
$ ./splunkforwarder/bin/splunk add monitor /var/www/monologapp/log/fideloper.log # Starts sending data
$ ./splunkforwarder/bin/splunk add monitor /var/log/apache2/error.log
thisisbrians / jquery-time-zone-select.js
Created Feb 3, 2013
Dynamically select a timezone in a Rails time_zone_select based on the browser's timezone using jQuery/JavaScript.
View jquery-time-zone-select.js
jQuery.fn.selectTimeZone = function() {
var $el = $(this[0]); // our element
var offsetFromGMT = String(- new Date('1/1/2009').getTimezoneOffset() / 60); // using 1/1/2009 so we know DST isn't tripping us up
if (offsetFromGMT[0] != '-') {
offsetFromGMT = '+' + offsetFromGMT; // if it's not negative, prepend a +
if (offsetFromGMT.length < 3) {
offsetFromGMT = offsetFromGMT.substr(0, 1) + '0' + offsetFromGMT.substr(1); // add a leading zero if we need it
var regEx = new RegExp(offsetFromGMT); // create a RegExp object with our pattern

Testing OAuth Apis in rails 3.x apps

  1. Install phantomjs
  2. Add rspec, poltergeist, capybara, vcr to Gemfile
  3. bundle install
  4. Create spec/acceptance folder
  5. Create spec/acceptance/acceptance_helper.rb (see below)
  6. Create spec for oauth flow (see below) read the comments carefully

NB: My signup flow is something like:

niklas / rails-fields-with-errors-get-class-by-nokogiri.rb
Created Jan 9, 2011
Add .error CSS class to all invalid form fields. Rails wraps them in div.fieldWithErrors, which still sucks in 3.0
View rails-fields-with-errors-get-class-by-nokogiri.rb
# put this in config/initializer/fields_with_errors.rb
ActionView::Base.field_error_proc = do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
error_class = 'error'
doc = Nokogiri::XML(html_tag)
doc.children.each do |field|
unless field['type'] == 'hidden'
unless field['class'] =~ /\berror\b/
View gist:108444
# This is a code example for the Ruby HTTP library Typhoeus
# here's an example for twitter search
# Including Typhoeus adds http methods like get, put, post, and delete.
# What's more interesting though is the stuff to build up what I call
# remote_methods.
class Twitter
include Typhoeus
remote_defaults :on_success => lambda {|response| JSON.parse(response.body)},
:on_failure => lambda {|response| puts "error code: #{response.code}"},
flodel /
Created May 2, 2012
Basic Craigslist API
fduran / gist:1870502
Created Feb 20, 2012
Linux monitor & react to event in log file
View gist:1870502
# Linux. Act upon an event in a log file
apt-get upgrade; apt-get install inotify-tools
# create file
# example finding Exception in tomcat log and sending email
while inotifywait -e modify /path/to/file.log; do
You can’t perform that action at this time.