Skip to content

Instantly share code, notes, and snippets.

James Brooks jamesbrooks

Block or report user

Report or block jamesbrooks

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jamesbrooks
jamesbrooks / gist:5812100
Created Jun 19, 2013
Rails - Internal Requests
View gist:5812100
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
@jamesbrooks
jamesbrooks / gist:5306657
Created Apr 4, 2013
Generate self-signed SSL certificate
View gist:5306657
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout domain.key -out domain.crt
@jamesbrooks
jamesbrooks / team_builder.rb
Last active Dec 14, 2015
Block-based object constructor
View team_builder.rb
class TeamBuilder
attr_accessor :name
attr_accessor :members
def initialize(name)
self.name = name
self.members = []
end
@jamesbrooks
jamesbrooks / DBAnalyzer.rb
Created Dec 1, 2011
Analyses a MySQL general query log, filters queries by structure and presents the most frequently ran query kinds
View DBAnalyzer.rb
#!/usr/bin/env ruby
# Example usage
# DBAnalyzer.rb general-query.log
class DBAnalyzer
OPERATIONS = %w(SELECT)
SHOW_TOP = 10
@jamesbrooks
jamesbrooks / EXAMPLE.js
Created Nov 18, 2010
jQuery Skeleton Plugin
View EXAMPLE.js
$('#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
View basketball
#!/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"
View cron script for backing up and e-mailing mysql databases
#!/bin/bash
# configuration
PROJECT_NAME='Foo'
PATH_TO_BACKUPS='/backups/foo_database_backups'
RECIPIENT_EMAIL='james@whatever.com'
DATABASE_USERNAME='root'
DATABASE_PASSWORD='password'
DATABASE_DATABASE='database_name'
# end of configuration. should not need to edit from this point
View TMail extension to collect plain-text parts.rb
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'
View gist:15925
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
View String slug generation
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
You can’t perform that action at this time.