Skip to content

Instantly share code, notes, and snippets.

@whoisstan
whoisstan / snippet.js
Created December 5, 2021 13:48
Simple Express Logging Middleware
//don't log tests
if (process.env.NODE_ENV !== 'test') {
app.use( (req, res, next) => {
//don't log OPTIONS calls
if (req.method !== 'OPTIONS' ) {
let startTime = Date.now();
res.on('finish', ()=> {
@whoisstan
whoisstan / gist:dcba1471094b984514c436fd395364e2
Created August 21, 2017 12:31
Unhandled rejection Error: Integer is unsafe
const obj={
"_id": "5998e0e7190855d671bbce52",
"source_creator": {
"type": "scripts",
"id": "55ec63d54db0c69c6293baa5"
},
"source_property": {
"id": "5998e0e6e33c9cd672b21727"
},
[{
"$match": {
"createdAt": {
$gte:new Date(1479189560111)
}
}
}, {
"$group": {
"_id": {
"year": { "$year": "$createdAt" },
@whoisstan
whoisstan / gist:9908350ea17543e2a651
Created February 26, 2015 17:07
Random Number generator using the ANU Quantum Random Numbers Server
module QRandom
def next
RestClient.get('http://qrng.anu.edu.au/API/jsonI.php?type=uint16&length=1'){ |response, request, result, &block|
case response.code
when 200
_json=JSON.parse(response)
if _json["success"]==true && _json["data"]
_json["data"].first || Random.rand(65535)
else
@whoisstan
whoisstan / gist:5f77f6c7aa021de27600
Created February 7, 2015 03:55
ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
# IRBRC file by Iain Hecker, http://iain.nl
# put all this in your ~/.irbrc
require 'rubygems'
require 'yaml'
alias q exit
class Object
def local_methods
@whoisstan
whoisstan / gist:9956176
Created April 3, 2014 15:09
Add File to Github
require 'rest_client'
access_token = YOUR_TOKEN
github_user = "whoisstan"
github_repo = "test_for_github_api"
path = "test7.html"
content = Base64.encode64('my file content2')
sha = nil
url="https://api.github.com/repos/#{github_user}/#{github_repo}/contents/#{path}"
@whoisstan
whoisstan / gist:4437834
Created January 2, 2013 20:45
Very Basic benchmark use example to clarify results
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |r|
N = 100000
r.report("<< ") do
s = ""
N.times { s << "." }
end
@whoisstan
whoisstan / gist:2838062
Created May 30, 2012 18:17
log HTTP headers
logger.warn "*** BEGIN HTTP HEADER DUMP ***"
self.request.env.each do |header|
if header[0].match("^HTTP.*")
logger.warn "#{header[0]}: #{header[1]}"
end
end
logger.warn "*** END HTTP HEADER DUMP ***"