Skip to content

Instantly share code, notes, and snippets.

@evanleck
evanleck / http-decorator.rb
Created Sep 22, 2016
Decorating Ruby's Net::HTTP for Fun and Profit
View http-decorator.rb
# 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
A naive shootout between object serialization libraries
View results.txt
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
View proxy.erb
<script>proxyCheck = { hostname: '<%= request.host %>', pathname: '<%= request.path %>' };</script>
View proxy.js
/*
* 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');
View business-profile.html
<!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 Sep 3, 2013
A custom include function to shortcut around the rather robust verbiage required for the "some" method.
View include.coffee
# 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 Sep 3, 2013
A console shim to avoid errors. Taken from the HTML5 Boilerplate https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js
View console.coffee
# 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]
View map.coffee
# 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?
View reduce.coffee
# 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 / stats.coffee
Created Sep 3, 2013
Shim for basic statistical analysis methods on Array. Includes max, min, median, sum, mean, variance, and standard deviation.
View stats.coffee
# 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