Skip to content

Instantly share code, notes, and snippets.

View mindscratch's full-sized avatar

Craig Wickesser mindscratch

View GitHub Profile
@mindscratch
mindscratch / ruby-gc-profiling.rb
Created November 6, 2012 02:25
ruby "complex" stuff - from Matt Aimonetti presentation
GC::Profiler.enable
# your code
puts GC::Profiler.result
# GC.disable
puts ObjectSpace.count_objects.inspect
@mindscratch
mindscratch / html-escape.js
Created December 7, 2012 17:56
un/escape HTML in Javascript
// Use the browser's built-in functionality to quickly and safely escape the
// string
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
};
// UNSAFE with unsafe strings; only use on previously-escaped ones!
function unescapeHtml(escapedStr) {
var AutocompleteView = Backbone.View.extend({
initialize: function() {
this.availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
@mindscratch
mindscratch / restore_rollback.rb
Created January 23, 2013 13:27
mongo replicaset rollback data
# http://docs.mongodb.org/manual/reference/bsondump/
fh = File.open("somefile.bson", "rb")
while not fh.eof? do
obj = BSON.read_bson_document fh
id = obj.delete "_id"
# assume Thing is a Mongoid::Document
thing = Thing.new obj
thing["_id"] = id.to_s
thing.save
@mindscratch
mindscratch / config.ru
Created January 29, 2013 14:13
Run puma with control server for a Sinatra app
# Usage: rackup config.ru
require 'gollum'
require 'gollum/frontend/app'
require 'puma'
Precious::App.set(:gollum_path, Dir.pwd)
Precious::App.set(:server, :puma)

Capybara

save_and_open_page

Matchers

have_button(locator)
@mindscratch
mindscratch / mongoid_close_connections.rb
Created February 14, 2013 14:26
Rack middleware to close Mongoid's connections to MongoDB. See https://github.com/mongoid/mongoid/issues/2369.
require 'mongoid'
class MongoidCloseConnections
def initialize(app)
@app = app
end
def call(env)
path = env['PATH_INFO'] || ''
begin
@mindscratch
mindscratch / secure_rails
Created February 25, 2013 10:07
Start WEBrick with SSL enabled. ## Usage bundle exec ruby script/secure_rails s -e <environment> -p <port>
#!/usr/bin/env jruby
%W{rails/command/server rack webrick webrick/https}.each { |lib| require lib }
ENV['HTTPS'] = 'on'
module Rails
class Server < ::Rack::Server
def default_options
super.merge({
#!/usr/bin/env ruby
# Usage:
# git clog # prints
# git clog -w # writes
#
# https://gist.github.com/2880525
module Clog
extend self
@mindscratch
mindscratch / fake_server.js
Last active July 13, 2018 05:56
Fake server-side using [Sinon.JS](http://sinonjs.org/). Inspiration from this [presentation](http://emdin.info/r/sinon-talk/).
var srv = sinon.fakeServer.create();
srv.autoRespond = true; // server should automatically respond after a timeout
srv.autoRespondAfter = 500; // response timeout in milliseconds
srv.xhr.useFitlers = true; // tell Sinon that some requests should not be faked
// add a filter that will tell Sinon to allowa all requests to access the server
// except fitlers defined when calling Server#fake.
srv.xhr.addFilter(function(method, url, async, username, password) {
var fakedRequest = _.find(Server.fakedRequests, function(request) {
return request.method === method && request.regex.test(url);