Skip to content

Instantly share code, notes, and snippets.

View evanleck's full-sized avatar

Evan Lecklider evanleck

View GitHub Profile
@evanleck
evanleck / http-decorator.rb
Created September 22, 2016 17:12
Decorating Ruby's Net::HTTP for Fun and Profit
# encoding: UTF-8
# frozen_string_literal: true
require 'net/http'
require 'json'
require 'uri'
class HTTPDecorator
# Timeouts
OPEN_TIMEOUT = 10 # in seconds
READ_TIMEOUT = 120 # in seconds
@evanleck
evanleck / results.txt
Created May 23, 2017 22:44
A naive shootout between object serialization libraries
Warming up --------------------------------------
JSON Dump 20.000 i/100ms
Marshal Dump 64.000 i/100ms
MessagePack Dump 381.000 i/100ms
Oj Dump 107.000 i/100ms
YAML Dump 1.000 i/100ms
Calculating -------------------------------------
JSON Dump 187.571 (±10.1%) i/s - 940.000 in 5.067947s
Marshal Dump 611.550 (± 5.7%) i/s - 3.072k in 5.039944s
MessagePack Dump 3.945k (± 7.7%) i/s - 19.812k in 5.055238s
<script>proxyCheck = { hostname: '<%= request.host %>', pathname: '<%= request.path %>' };</script>
/*
* This is a naive attempt to protect against transparent proxies.
* - We pass the request host and path in from Rack and compare it against what JS sees.
* - If they don't match, put them where they should be.
*
*/
(function(location) {
if (location.hostname !== proxyCheck.hostname || location.pathname !== proxyCheck.pathname) {
/* Use an anchor tag as a parser. */
var redirectParser = document.createElement('a');
<!DOCTYPE html>
<html>
<head>
<title>Busines Profile</title>
<link rel='stylesheet' type='text/css' href='http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css'>
</head>
<body>
<div class='container' ng-app='business-profile' ng-controller='main'>
<div class='row'>
<div class='col-sm-1'></div>
@evanleck
evanleck / include.coffee
Created September 3, 2013 00:51
A custom include function to shortcut around the rather robust verbiage required for the "some" method.
# include
# depends on some, a shim for which can be found at https://gist.github.com/l3ck/6418548
if "function" isnt typeof Array::include
Array::include = (sought_value) ->
"use strict"
# throw on null
throw new TypeError("Array.prototype.include called on null or undefined") if null is this or "undefined" is typeof this
@some (element, index, array) ->
@evanleck
evanleck / console.coffee
Created September 3, 2013 01:01
A console shim to avoid errors. Taken from the HTML5 Boilerplate https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js
# Avoid `console` errors in browsers that lack a console.
(->
method = undefined
noop = noop = ->
methods = ["assert", "clear", "count", "debug", "dir", "dirxml", "error", "exception", "group", "groupCollapsed", "groupEnd", "info", "log", "markTimeline", "profile", "profileEnd", "table", "time", "timeEnd", "timeStamp", "trace", "warn"]
length = methods.length
console = (window.console = window.console or {})
while length--
method = methods[length]
@evanleck
evanleck / stats.coffee
Created September 3, 2013 00:47
Shim for basic statistical analysis methods on Array. Includes max, min, median, sum, mean, variance, and standard deviation.
# max
if "function" isnt typeof Array::max
Array::max = ->
"use strict"
# throw on null
throw new TypeError("Array.prototype.max called on null or undefined") if null is this or "undefined" is typeof this
Math.max.apply null, this
@evanleck
evanleck / reduce.coffee
Created September 3, 2013 00:43
Array.prototype.reduce shim for non-compliant browsers. Converted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce.
# reduce from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
if "function" isnt typeof Array::reduce
Array::reduce = (callback, opt_initialValue) ->
"use strict"
# At the moment all modern browsers, that support strict mode, have
# native implementation of Array.prototype.reduce. For instance, IE8
# does not support strict mode, so this check is actually useless.
throw new TypeError("Array.prototype.reduce called on null or undefined") if null is this or "undefined" is typeof this
throw new TypeError(callback + " is not a function") if "function" isnt typeof callback
@evanleck
evanleck / map.coffee
Created September 3, 2013 00:42
Array.prototype.map shim for non-compliant browsers. Converted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map.
# Production steps of ECMA-262, Edition 5, 15.4.4.19
# Reference: http://es5.github.com/#x15.4.4.19
# from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
unless Array::map
Array::map = (callback, thisArg) ->
T = undefined
A = undefined
k = undefined
throw new TypeError(" this is null or not defined") unless this?