Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Example DSL – GeeTwitter


[ 136 ]

shell.evaluate (new File(args[0]))
}
else
println "Usage: GTweet_2.1 <script>"

With this final version of our DSL, we have managed to distill the code down to
the barest outline that is needed to express what we want to do. Using only the
preceding launcher shell script, and the GeeTwitterScript.groovy class, we can
start to automate our Twitter experience from the command line. Here are some of
sample GeeTwitter scripts we can try out:



  • Send a direct message to a user:
    sendMessage "GroovyDSL", "Using GeeTwitter to send you a message."

  • Send a direct message to all of my followers:
    eachFollower {
    sendMessage it, "Thanks for taking the time to follow
    me!"
    }

  • List all of the users that I'm following:
    eachFriend { println it}

  • Follow all of my followers:
    eachFollower {
    follow it
    }


I think you'll agree that we've built quite an elegant DSL for Twitter, and we've
been able to do it with surprisingly little code. This version has limited functionality
to choose from, as I did not want to clutter the text with a fully functional Twitter
DSL. With a little bit more coding, we could extend this DSL to do a lot more. For
example, if you've got a follower on Twitter who is following hundreds of users, but
who has no followers, and who has issued only one tweet containing a link, then
chances are that the follower is a spammer. Wouldn't it be nice to be able to block
such a user automatically with a DSL script?


http://www.ebook3000.com
Free download pdf