Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / ExecutIOr.groovy
Last active August 29, 2015 14:05
Groovy script that can execute the provided command repeatedly at a fixed interval.
import java.util.concurrent.*
def execSingle = { String cmd, out, showTs ->
if (showTs) out << "${System.currentTimeMillis()}: "
out << cmd.execute().in
}
def exec = { cmdStrOrList, out, showTs ->
switch (cmdStrOrList) {
case String:
@bdkosher
bdkosher / extractPathOnly.groovy
Created August 22, 2014 20:12
Crude Groovy closure that can take a URI and strip off the query string and fragment, returning just the path part
def extractPathOnly = { path ->
switch (path) {
case '/':
path = 'index.html'
break
case { it.contains('?') }:
path = path.split('\\?')[0]
break
case { it.contains('#') }:
path = path.split('#')[0]
@bdkosher
bdkosher / TwoFileArgsValidateExample.groovy
Created August 25, 2014 16:34
Pattern for assigning and transforming String args to target File type, plus validating their existence--all in one compact piece of code
def (f1, f2) = [new File(args[0]), new File(args[1])].each { f ->
if (!f.exists() || f.isDirectory()) {
println "$f does not exist or is a directory"
System.exit(0);
} else {
f
}
}
println "$f1 and $f2 provided"
@bdkosher
bdkosher / constructor.js
Created September 3, 2014 19:59
Douglas Crockford shares his "recipe" for creating objects in JavaScript ES6 now that he's abandoned from both 'new', 'Object.create', and prototypal inheritance. http://www.infoq.com/presentations/efficient-programming-language-es6
function constructor(spec) {
let {member} = spec,
{other} = other_constructor(spec),
method = function () {
// member, other, method
};
return Object.freeze({
method,
other
});
@bdkosher
bdkosher / FindWindowReferences.js
Last active August 29, 2015 14:06
Scans an object tree looking for references to Windows or DOM Nodes. TODO: handle stack overflows, function scope?
var isWindowOrNode = function (obj, callback, callbackArg) {
var result = false;
if (obj != null) {
if (obj instanceof Window) {
callback(obj, callbackArg);
result = true;
} else if (obj instanceof HTMLDocument) {
callback(obj.defaultView, callbackArg);
result = true;
} else if (obj instanceof HTMLElement) {
@bdkosher
bdkosher / ForLoopLastness.groovy
Created September 16, 2014 13:42
Metaprogramming approaches to exposing "last"-ness within a traditional for loop. Pretty useless, but a good launching point for exploring metaprogramming. TODO: dynamically applied traits, mixins?, ExpandoMetaClass to override iterator() method? See http://stackoverflow.com/questions/25852346/identify-last-element-of-an-array-in-groovy-for-stat…
import groovy.transform.TupleConstructor
// approach 1. Custom subtype coercion
@TupleConstructor
class LastAwareIterator<T> implements Iterator<T> {
Iterator itr
boolean hasNext() {
itr.hasNext()
}
@bdkosher
bdkosher / SpockUnrollDesc.groovy
Created September 26, 2014 16:24
Unroll descriptions - "Idiomatic Spock" by Rob Fletcher - http://youtu.be/RuTupC0I59M?t=12m26s
@Unroll
def "the string '#string' is #description"() {
expect:
string.isInteger() == expected
where:
string | expected
"ABC" | false
"123" | true
"1.2" | false
@bdkosher
bdkosher / StackoverflowTagCounter.groovy
Created October 9, 2014 18:54
Counts the number of tagged questions on Stackoverflow for a given tag.
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
import java.text.*
class SOTagCounter {
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
int count(String tag) {
new URL("http://stackoverflow.com/questions/tagged/${new URI(tag).path}").withReader { page ->
def html = parser.parse(page)
def c = html.body.div.find { it.@class == 'container' }.div.find { it.@id == 'content' }.div.find { it.@id == 'sidebar' }.div.find { it.@class == 'module' }.div[0].text()
@bdkosher
bdkosher / PDFScanner.java
Last active August 29, 2015 14:10
Crude Java class (depends on pdfbox-1.8.5 and common-logging) to recursively scan directories of PDF files to determine if they contain text or not (or are non-PDF files)
package prpsutil;
import java.io.*;
import java.util.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;
public class PDFScanner {
private static final byte[] expectedMagicNumber = "%PDF".getBytes();
@bdkosher
bdkosher / OutageImpactOnAvailability.groovy
Created December 18, 2014 21:01
What's the impact on availability due to a single outage?
import groovy.time.*
def (from, to) = ['12/16/2014 2:52 PM','12/18/2014 3:32 PM'].collect { Date.parse('MM/dd/yyyy h:mm a', it) }
int minutesDown = TimeCategory.minus(to, from).toMilliseconds() / 1000 / 60
int minutesPerYear = 365 * 24 * 60
(minutesPerYear - minutesDown) / minutesPerYear