Skip to content

Instantly share code, notes, and snippets.

View bensie's full-sized avatar

James Miller bensie

View GitHub Profile
@bensie
bensie / ghuser.rb
Created October 17, 2012 23:21
GHUser - MonkDevAcademy - class 5
require "test/unit"
class GHUserTest < Test::Unit::TestCase
def setup
@user = GHUser.new("bensie")
end
def test_has_a_name
assert_equal "James Miller", @user.name
end
require "bundler/setup"
require "nokogiri"
builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.name "James" do
xml.number 1234
end
end
# Any unmatched request within the /api/ namespace should render 404 as JSON
# Stop the request here so that JSON gets returned instead of having it
# run through the whole Rails stack and spit HTML.
get "/*" do
halt_with_404_not_found
end
post "/*" do
halt_with_404_not_found
end
@bensie
bensie / celluloid.rb
Created June 7, 2012 17:45
playing with celluloid
require "celluloid"
class Runner
include Celluloid
def initialize
if block_given?
puts "starting instance run"
yield self
puts "finished instance run"
@bensie
bensie / time.rb
Created June 1, 2012 21:22
Parse iso8601 time in RubyMotion
class Time
def self.iso8601(time)
formatter = NSDateFormatter.alloc.init
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
formatter.dateFromString time
end
end
@bensie
bensie / dup.sql
Created May 16, 2012 00:38
Find duplicate rows in MySQL
# Assumes you're looking to add a unique index to [:field1, :field2] and want to find duplicates to remove/fix them
SELECT id, field1, field2, count(*) FROM table GROUP BY field1, field2 HAVING count(*) > 1
@bensie
bensie / gist:2642524
Created May 9, 2012 06:50
Kill passenger instance PIDs using over 400MB
for i in `sudo /usr/local/src/passenger-XXX/bin/passenger-memory-stats | grep "Passenger RackApp" | awk '{if ($4>400) print $1}'`; do kill -9 $i; done
@bensie
bensie / no_www.rb
Created March 27, 2012 19:17 — forked from trevorturk/no_www.rb
no-www rack middleware
class NoWww
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.host.starts_with?("www.")
[301, {"Location" => request.url.sub(/www./, "")}, self]
@bensie
bensie / alldbs.txt
Created March 25, 2012 14:59
MySQL database size
SELECT table_schema "DB Name", SUM( data_length + index_length) / 1024 / 1024 "DB Size in MB" FROM information_schema.TABLES GROUP BY table_schema;
@bensie
bensie / gist:1607616
Created January 13, 2012 17:21
SSHKey error on rbx
>> SSHKey.generate.ssh_public_key
ArgumentError: invalid string for Integer
from { } in SSHKey#ssh_public_key_conversion at /Users/jkmiller/.rvm/gems/rbx-head/gems/sshkey-1.3.0/lib/sshkey.rb:160
from Array#each at kernel/bootstrap/array.rb:66
from SSHKey#ssh_public_key_conversion at /Users/jkmiller/.rvm/gems/rbx-head/gems/sshkey-1.3.0/lib/sshkey.rb:159
from SSHKey#ssh_public_key at /Users/jkmiller/.rvm/gems/rbx-head/gems/sshkey-1.3.0/lib/sshkey.rb:128
from { } in Object#irb_binding at (irb):5
from Rubinius::BlockEnvironment#call_on_instance at kernel/common/block_environment.rb:72