Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Example DSL – GeeTwitter


[ 114 ]

With Twitter, you can tweet from the Web or from your mobile phone. If you register
your mobile phone with your account, you can send your tweets via SMS, and in
certain countries, you can get the tweets of the folks that you follow sent directly to
your mobile phone as SMS messages.


To begin with, like most people, I was very skeptical about Twitter. My initial
Twitter experiences mostly involved reading friends' tweets about what they were
having for breakfast. The experience was not unlike reading early bloggers blogging
about the mundane aspects of their day. Since then Twitter has evolved, and many
eminently sensible people are out there tweeting about stuff that really does have
value.


Try searching Twitter for keywords such as "Groovy DSL" and you'll find tweets
from folks such as Guillaume LaForge, who is a senior figure in the Groovy
community. It's probably worth following the user @glaforge on Twitter for that
reason. In fact, it's safe to say that it is worth following anybody who is tweeting
about "Groovy DSL".


Like most major modern websites, Twitter has a self-contained API that allows us
to interface with it. By providing an open API to developers, Twitter has fostered
a community of developers who have developed numerous different client
applications. As a result, there are numerous third-party and open source client
applications for Twitter, for both mobile and desktop platforms.


Suppose we were able to use Twitter APIs to develop a Groovy-based scripting
interface, what would it look like? Imagine a script that allows us to follow every
Twitter user who has recently tweeted about "Groovy DSL". Based on our knowledge
of Groovy, something like the following would probably make sense:


// Follow all that have tweeted recently about Groovy and DSL
search ("Groovy DSL") { from, tweet ->
follow(from)
println "Following ${from}"
}

For the rest of this chapter, we will walk through the steps it takes to turn this script
into a working reality.


Working with the Twitter APIs


Twitter provides APIs that cover the whole gamut of operations that we might like to
invoke for interacting with the service. Through the APIs, we can update our status,
send direct messages to other users, list our friends and followers, and search for
tweets by keyword, among many other useful features.


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