Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / InputsHaveLabels508check.groovy
Created May 15, 2015 15:23
Crude 508 verification script to ensure each input has a corresponding label.
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
Set<String> inputIds = [] as Set
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
def html = parser.parse(new File(/C:\Users\jwolf2\Desktop\swagger-ui.html/))
html.'**'.findAll { it.name() == 'select' || it.name() == 'textarea' || (it.name() == 'input' && it.@type != 'button' && it.@type != 'submit') }.each { el ->
if (!inputIds.add(el.@id)) {
println "${el.@id} is a dupe"
}
}
@bdkosher
bdkosher / GetDependencies.groovy
Last active August 29, 2015 14:20
Sonar library scraper
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
def url = 'https://example.com/sonar/dependencies?resource=commons-io%3Acommons-io&search=commons-io%3Acommons-io&version=1.1'
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
new URL(url).withReader { page ->
def html = parser.parse(page)
html.'**'.find { it.@id == 'results_col' }.'**'.findAll { it.name() == 'td' }.each { td ->
println td.span.text()
}
@bdkosher
bdkosher / FizzBuzzCompileStatic.groovy
Created March 25, 2015 00:49
Groovy FizzBuzz with CompileStatic.
@groovy.transform.CompileStatic
List<String> fizzbuzz(int from = 1, int to = 100) {
String f = 'fizz', b = 'buzz'
def m = {int x, int y ->
x % y == 0
}
def F = { int n ->
m(n,15) ? "$f$b" : m(n,3) ? f:m(n,5) ? b : n
}
(from..to).collect {int i -> F(i) }
@bdkosher
bdkosher / FizzBuzz.groovy
Created March 24, 2015 18:45
FizzBuzz in Groovy
f='fizz';b='buzz';m={x,y->x%y==0};F={n->m(n,15)?"$f$b":m(n,3)?f:m(n,5)?b:n};(1..100).collect{F(it)}
@bdkosher
bdkosher / curl_write-out_fmt
Last active August 29, 2015 14:17
Gathering statistics on download speeds using curl httpd wtih mod_ratelimit
%{url_effective},%{size_download},%{time_total},%{speed_download}\n
@bdkosher
bdkosher / HttpProxyServer.groovy
Created March 6, 2015 18:59
Groovy-based HTTP local proxy server heavily inspired by https://github.com/vert-x/vertx-examples/blob/master/src/raw/java/proxy/ProxyServer.java. To use, set other Java process's JAVA_OPTS: C:\dev\cp\PRPS\prps\prps-services>mvn install tomcat6:run -Ptomcat -Dhttp.proxyHost=localhost -Dhttp .proxyPort=8000
import org.vertx.groovy.core.*
String headersAsString(MultiMap headers) {
headers == null ? '' : headers.entries.collect({ e -> "$e.key : $e.value" }).join('\n')
}
String requestAsString(req) {
"$req.version $req.method $req.uri\n${headersAsString(req.headers)}\n\n"
}
String responseAsString(res) {
@bdkosher
bdkosher / HTTPRequestInspectionServer.groovy
Last active August 29, 2015 14:16
A vert.x script to printout information of an HTTP request.
vertx.createHttpServer().requestHandler { req ->
req.bodyHandler { body ->
def headerInfo = req.headers.entries.collect({ e -> "$e.key : $e.value" }).join('\n')
println """
$req.version $req.method $req.absoluteURI
$headerInfo
"""
if (body) {
@bdkosher
bdkosher / PromptToPause.groovy
Created March 4, 2015 22:04
Prompt to pause script in Groovy console
import javax.swing.*
void pause() {
JOptionPane.showMessageDialog(new JFrame(), 'Click OK to continue.', 'Script Execution Paused', JOptionPane.INFORMATION_MESSAGE)
}
println 1
pause()
println 2
pause()
@bdkosher
bdkosher / blog20150206.md
Last active August 29, 2015 14:16
Blog post about adding methods to anonymous types in Java

Methods in Anonymous Java Object Types

Java syntax allows you to create instances of anonymous types without much boilerplate. This approach is commonly use to implement single-use instance of Single Abstract Method-style interfaces such as Comparable and Runnable:

new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello, world!");
    }

}).start();

@bdkosher
bdkosher / ListToHierarchy.groovy
Created February 16, 2015 21:22
Convert a List of Nodes to a single Node where the subsequent nodes in the List are the children of the predecessor.
class Node {
String name
Node child
static Node asHierarchy(List<Node> nodes) {
nodes.reverse().inject(null) { prev, curr ->
curr.child = prev
curr
}
}