Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Example DSL – GeeTwitter


[ 134 ]

import static twitter4j.TwitterFactory.*

abstract class GeeTwitterScript extends Script {
def sendMessage(user, message) {
// Send a direct messsage to twitter user GroovyDSL
singleton.sendDirectMessage(user, message)
}
// Method to apply a closure to each friend
def eachMessage(Closure c) {
singleton.directMessages.each {
c.call(it.senderScreenName,it.text)
}
}

// Method to apply a closure to each friend
def eachFriend(Closure c) {
singleton.getFriendsList('groovydsl',-1).each {
c.call(it.screenName)
}
}

// Method to apply a closure to each follower
def eachFollower(Closure c) {
singleton.getFollowersList('groovydsl',-1).each {
c.call(it.screenName)
}
}

// Method to follow another twitter user
def follow(user) {
singleton.createFriendship(user)
}

void search(terms) {
def query = new Query(terms)
singleton.search(query).tweets.each {
println it.text
}
}

def search(terms, Closure c) {
def query = new Query(terms)
singleton.search(query).tweets.each {
c.call(it.user.screenName,it.text)

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