Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Example DSL – GeeTwitter


[ 120 ]

Searching

The Twitter APIs have a powerful search capability that allows us to search for what
people are commenting on at any given time. Twitter4J implements searching by
passing a Query object to the Twitter.search API. Using the following code,
we will search for tweets containing the keywords Groovy and DSL:


def twitter = TwitterFactory.singleton
// Create a query for tweets containing terms "Groovy" and "DSL"
def query = new Query("Groovy DSL")
// Search and iterate the results
twitter.search(query).tweets.each { tweet ->
println "${tweet.user.screenName} : ${tweet.text}"
}

Calling the Twitter.search API returns a QueryResult object.
QueryResult.getTweets() will return a list of tweet objects. We can use
the list of tweets matching our search criteria as a Groovy collection, and
iterate it through the use of the built-in each method. At the time of writing
this book, I got the following result from running this script:


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