Skip to content

Instantly share code, notes, and snippets.

View jamesbrooks's full-sized avatar

James Brooks jamesbrooks

View GitHub Profile
class String
def slugify
returning self.downcase.gsub(/'/, '').gsub(/\//, ' ').gsub(/&/, ' and ').gsub(/[^a-z0-9]+/, '-') do |slug|
slug.chop! if slug.last == '-'
end
end
end
module ActionController
module Routing
class RouteSet
class Mapper
def hush_cms_pages(path)
named_route 'hush_cms_page',
"#{path}/*path",
:controller => HushCMS.configuration['controllers']['pages'],
:action => 'show'
end
module TMail
class Mail
def plain_text_body
gather_plain_text_parts(self).flatten
end
private
def gather_plain_text_parts(part)
returning [] do |message|
message << part.body.strip if part.content_type == 'text/plain'
#!/bin/bash
# configuration
PROJECT_NAME='Foo'
PATH_TO_BACKUPS='/backups/foo_database_backups'
RECIPIENT_EMAIL='[email protected]'
DATABASE_USERNAME='root'
DATABASE_PASSWORD='password'
DATABASE_DATABASE='database_name'
# end of configuration. should not need to edit from this point
#!/usr/bin/ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.sportingpulse.com/comp_info.cgi?a=FIXTURE&compID=107624&c=1-4401-0-0-0'))
against = doc.xpath('//*[text()="Owned"]').first.xpath('../../..//b[text()!="Owned"]').first.text
time = doc.xpath('//*[text()="Owned"]').first.xpath('../../../td').first.inner_html.gsub(/\302\240/, ' ').gsub('<br>', ', ')
puts "Owned play #{against} at #{time}\n\n"
@jamesbrooks
jamesbrooks / EXAMPLE.js
Created November 18, 2010 12:50
jQuery Skeleton Plugin
$('#element').plugin(); // calls init
$('#element').plugin({ foo: 'bar' }); // calls init and extends the hash into settings
$('#element').plugin('doSomething'); // calls init (if it hasn't already been done) and calls doSomething
$('#element').plugin('doSomething', { cake: 'delicious' }); // calls init and calls doSomething with the provided argument
@jamesbrooks
jamesbrooks / DBAnalyzer.rb
Created December 1, 2011 01:50
Analyses a MySQL general query log, filters queries by structure and presents the most frequently ran query kinds
#!/usr/bin/env ruby
# Example usage
# DBAnalyzer.rb general-query.log
class DBAnalyzer
OPERATIONS = %w(SELECT)
SHOW_TOP = 10
@jamesbrooks
jamesbrooks / team_builder.rb
Last active December 14, 2015 19:58
Block-based object constructor
class TeamBuilder
attr_accessor :name
attr_accessor :members
def initialize(name)
self.name = name
self.members = []
end
@jamesbrooks
jamesbrooks / gist:5306657
Created April 4, 2013 00:20
Generate self-signed SSL certificate
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout domain.key -out domain.crt
@jamesbrooks
jamesbrooks / gist:5812100
Created June 19, 2013 06:38
Rails - Internal Requests
def internal_request(path, params={})
request_env = Rack::MockRequest.env_for(path, params: params.to_query).merge({
'rack.session' => session
})
Rails.application.routes.call(request_env).last.body
end