Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Example DSL – GeeTwitter


[ 118 ]

You will find full documentation for using Grape and the @Grab
annotation at http://docs.groovy-lang.org/latest/html/
documentation/grape.html.
If you are running the preceding example from behind an http proxy,
you may need to refer to the Proxy settings section in this documentation.

Tweeting

We'll start by trying out the APIs to get and set our current Twitter status. We will
use the Twitter class from Twitter4J to log in to our Twitter account and access the
APIs to update and get our status, so you will need to have added your OAuth
credentials into twitter4j.properties before you try this yourself:


// Get a twitter connection
def twitter = TwitterFactory.singleton
// Update twitter status
twitter.updateStatus("Updating my status via the Twitter4J APIS")
println twitter.showUser("groovydsl").status.text

The sample code pack has several examples that make use of the Twitter4J
libraries. You will need to locate each of these twitter4j.properties
files and edit them. Each file contains the following properties that will
need to be filled in:

debug=true
oauth.consumerKey=
oauth.consumerSecret=
oauth.accessToken=
oauth.accessTokenSecret=

Once we've got a connection to the service with the Twitter object, we can start to
play with the APIs. The Twitter.updateStatus method sets a new status message
(tweet) for us on our Twitter account. Try out this script yourself. Check your Twitter
status through your favorite Twitter client or on the Web, and you will see it has
been updated.


Retrieving our status is just as simple. We can use the Twitter.showUser method to
get a User object for any user, including ourselves. Calling getStatus on this object
retrieves the current status message for any user.


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