Skip to content

Instantly share code, notes, and snippets.

View benpickles's full-sized avatar
🏎️

Ben Pickles benpickles

🏎️
View GitHub Profile
# Brutal hack to make all string/text columns alert("hack") if they're not
# being properly escaped.
models = Dir['app/models/*.rb'].map { |path|
name = File.basename(path, '.*')
name.camelize.constantize
}.reject { |klass|
klass.superclass != ActiveRecord::Base
}
@benpickles
benpickles / common-ancestor.js
Last active August 9, 2020 13:49
Written as a response to a Stack Overflow question "How to find the nearest common ancestors of two or more nodes?" https://stackoverflow.com/a/5350888/194664
function parents(node) {
var nodes = [node]
for (; node; node = node.parentNode) {
nodes.unshift(node)
}
return nodes
}
function commonAncestor(node1, node2) {
var parents1 = parents(node1)
@benpickles
benpickles / action.js
Created March 18, 2016 16:19
Inject an Authorization header into a redux-api-middleware request with a Redux middleware.
import { CALL_API } from 'redux-api-middleware'
export function fetchLocations() {
return {
[CALL_API]: {
endpoint: 'http://api.somesite.com/api/locations',
method: 'GET',
// Don't have to manually add the Authorization header to every request.
headers: { 'Content-Type': 'application/json' },
types: ['REQUEST', 'SUCCESS', 'FAILURE']
@benpickles
benpickles / gsub_sub_bench.rb
Last active October 15, 2019 09:54
When you only want to substitute one thing use `sub` instead of `gsub`.
Rehearsal ----------------------------------------
gsub 0.013451 0.000356 0.013807 ( 0.013817)
sub 0.005154 0.000086 0.005240 ( 0.005302)
------------------------------- total: 0.019047sec
user system total real
gsub 0.011792 0.000351 0.012143 ( 0.012186)
sub 0.003583 0.000001 0.003584 ( 0.003583)
@benpickles
benpickles / gist:2596158
Created May 4, 2012 16:48
WebMock.last_request
# Thankyou Bartosz Blimke!
# https://twitter.com/bartoszblimke/status/198391214247124993
module LastRequest
def last_request
@last_request
end
def last_request=(request_signature)
@last_request = request_signature
@benpickles
benpickles / .block
Last active November 20, 2016 16:57
Twitter Analytics-style line chart with Peity
height: 90
@benpickles
benpickles / index.html
Last active November 20, 2016 16:23
Follow SVG <path>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.path {
fill: transparent;
stroke: #ddd;
stroke-dasharray: 2 4;
stroke-width: 2;
}
@benpickles
benpickles / README.md
Last active November 9, 2016 14:52
Freight Train

A train runs along this (randomly generated) track.

@benpickles
benpickles / apiAuthInjector.js
Created March 17, 2016 21:28
Inject an Authorization header into a redux-api-middleware request with a Redux middleware.
import { CALL_API } from 'redux-api-middleware'
export default function({ getState }) {
return function(next) {
return function(action) {
const callApi = action[CALL_API]
if (callApi) {
const { authToken } = getState()
@benpickles
benpickles / ding.sh
Created November 25, 2010 14:33
Make a "ding" sound on your Mac!
# .___.__
# __| _/|__| ____ ____
# / __ | | |/ \ / ___\
# / /_/ | | | | \/ /_/ >
# \____ | |__|___| /\___ /
# \/ \//_____/
#
# Make a "ding" sound on your Mac! Useful for notifying you when a command
# line script has finished.
#