Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
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
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
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
fideloper / splunkstorm.sh
Created Nov 1, 2012
Ubuntu - Install Splunkstorm.com forwarder
View splunkstorm.sh
# 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 forwarder.splunkstorm.com:9997
$ ./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
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
View 0-Instructions.md

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
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 = Proc.new 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
flodel / image.gallery.R
Created May 2, 2012
Basic Craigslist API
View image.gallery.R
@fduran
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
# www.fduran.com
apt-get upgrade; apt-get install inotify-tools
# create file myalert.sh:
# example finding Exception in tomcat log and sending email
#!/bin/bash
while inotifywait -e modify /path/to/file.log; do
You can’t perform that action at this time.