Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 6

[ 135 ]

}
}

void search(Map args, String terms = "") {
def queryString = ""
args.each { arg ->
switch (arg.key.toString().toLowerCase()) {
case 'from':
queryString += "from:${arg.value} "
break
case 'to':
queryString += "to:${arg.value} "
break
case 'hashtag':
queryString += "#${arg.value} "
break
case 'username':
queryString += "@${arg.value} "
break
}
}
queryString += terms
def query = new Query(queryString)
singleton.search(query).tweets.each {
println it.text
}
}

def block(user) {
singleton.createBlock(user)
}
}

With this version, we will supply the name of the new subclass to the
CompilerConfiguration object in the launch script. The new launch
script appears as follows:


#!/usr/bin/env groovy
import org.codehaus.groovy.control.*

if(args) {
def conf = new CompilerConfiguration()
conf.setScriptBaseClass("GeeTwitterScript")
def shell = new GroovyShell(this.class.classLoader,
new Binding(), conf)
Free download pdf