Skip to content

Instantly share code, notes, and snippets.

View kplawver's full-sized avatar

Kevin Lawver kplawver

View GitHub Profile
@kplawver
kplawver / json_callbacks.rb
Created September 24, 2009 13:24
Adds :callback option to to_json for Array, Hash and ActiveRecord::Base.
# Adds :callback option to to_json for Array, Hash and ActiveRecord::Base. Usage:
# user = User.find(:first)
# user.to_json(:callback => "foo")
# => foo({"user":{"name":"The Dude"}})
# There's probably a much better way to do this - would love ideas / improvements!
class Array
alias orig_to_json to_json
def to_json(*a)
callback = nil
@kplawver
kplawver / optimize_tables.rb
Created October 7, 2009 13:06
I was tired of running optimize table by hand, so I wrote something do to it for me.
# Will run optimize table on either just that class's MySQL InnDB table, or on ALL of that connection's tables.
# Works only with MySQL InnoDB tables, but could be used to run repair if you're using MyISAM.
class ActiveRecord::Base
def self.optimize_table
begin
self.connection.execute("optimize table #{self.table_name}")
rescue Exception => e
logger.error("#{self.class_name} - error optimizing #{self.table_name}: #{e}")
else
@kplawver
kplawver / runner.rb
Created November 3, 2009 16:10
I needed a script/runner equivalent for Sinatra. This works pretty well so far.
#!/usr/bin/env ruby
# If you want to run it on a specific environment, set your RACK_ENV environment variable.
require 'rubygems'
require File.expand_path("../app",__FILE__)
# puts ARGV.options.inspect
if ARGV[0].length > 0 && ARGV[0].is_a?(String)
puts eval(ARGV[0])
@kplawver
kplawver / easy_created_at_named_scope.rb
Created December 15, 2009 18:44
Add this to your ActiveRecord models for an easy "find me everything that happened on this day" named scope
# Add this to your ActiveRecord models for an easy "find me everything that happened on this day" named scope:
named_scope :by_date, lambda {|date|
if date.is_a?(String)
date = Time.parse(date)
end
date_start = date.midnight
date_end = date_start.advance(:hours => 24)
{
:conditions => ["created_at between ? and ?",date_start,date_end]
}
@kplawver
kplawver / loadr.rb
Created January 12, 2010 21:16
Helps simplify and automate apache benchmark runs.
## Loader
# Does a pre-configured set of apache benchmark on an array of URLs passed in.
# Yes, it's silly, but it saved me a lot of time today.
#
# Usage: Loadr.test(['http://cnn.com','http://lwvr.net'],"news_and_me")
# - Created a log file for each URL, then fills it with the results of its ab runs.
# - Only works on OS's where ab is in the path.
class Loadr
@@run_sets = [
@kplawver
kplawver / cloud_front.rb
Created June 21, 2010 21:52
Simple example for streaming private content from CloudFront
require 'base64'
require 'openssl'
require 'sha1'
# This will generate URLs for streaming private content from CloudFront...
# I followed these instructions for setting up the bucket and CloudFront:
## http://support.rightscale.com/index.php?title=12-Guides/01-RightScale_Dashboard_User_Guide/02-Clouds/01-EC2/11-CloudFront/Serving_Private_Content
# I have a global config hash for my private key for S3:
# The access_key is the CloudFront key id you set up.
@kplawver
kplawver / beanstalkd_plugin.rb
Created December 7, 2010 14:57
A Scout plugin for monitoring a Beanstalkd. It remembers the current ready and buried jobs and does a crude measure of jobs/second.
class BeanstalkdPlugin < Scout::Plugin
needs 'beanstalk-client'
OPTIONS=<<-EOS
connection_string:
default: localhost:11300
name: Connection String
notes: The host and port to connect to.
EOS
@kplawver
kplawver / file_check_plugin.rb
Created December 7, 2010 15:00
A super simple Scout plugin to monitor just the existence of a file. We're using it to alert us when an NFS share goes down (if it times out, it's down).
class FileCheckPlugin < Scout::Plugin
OPTIONS=<<-EOS
file:
default: ~
name: File
notes: The file to check to make sure it exists.
EOS
def build_report
@kplawver
kplawver / paginate_array.js
Created October 11, 2011 13:23
I needed to page through results in an array and couldn't find any simple pagination functions through the googles, so I wrote this. I hope it's useful!
function paginateArray(arr,page_num,per_page) {
var i,r=[];
if (arr.length < 1) {
return r;
}
if (per_page == undefined || per_page == null) {
per_page = 10;
}
@kplawver
kplawver / index.html
Created February 13, 2012 21:11
Weird response from http://github.com
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
<!-- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"> -->
<HTML>
<HEAD>
<META HTTP-EQUIV="Refresh" CONTENT="0.1">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<TITLE></TITLE>
</HEAD>