JavaDude's Groovy Snippets

July 28, 2010

Accessing world time engine with Groovy and httpbuilder

Filed under: Groovy Tutorial — Tags: , , — admin @ 9:41 am

World Time Engine provides all kind of time related information for any given place on this planet. You can use their free web portal and enter GPS coordinates, city names or postal codes and retrieve timezone, daylight saving and more. Otherwise they provide an API to get the data via an http GET request. This service is not free, they charge you a 14 U$ for 50.000 requests.

API details you find here, but in short:

Request   
  1. http://worldtimeengine.com/api/<apikey>/<latitude>/<longitude>

and you get something like this:

Response   
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="http://worldtimeengine.com/timezone.xsd">
  4. <version>1.1</version>
  5. <location>
  6. <region>Australia/New South Wales</region>
  7. <latitude>-33.8671</latitude>
  8. <longitude>151.207</longitude>
  9. </location>
  10. <time>
  11. <utc>2008-02-13 00:50:30</utc>
  12. <local>2008-02-13 11:50:30</local>
  13. <zone>
  14. <hasDST>true</hasDST>
  15. <current>
  16. <abbreviation>EDT</abbreviation>
  17. <description>Eastern Australia Daylight Savings Time</description>
  18. <utcoffset>+11:00</utcoffset>
  19. <isdst>true</isdst>
  20. <effectiveUntil>2008-03-29 19:00:00</effectiveUntil>
  21. </current>
  22. <next>
  23. <abbreviation>EST</abbreviation>
  24. <description>Eastern Australia Standard Time</description>
  25. <utcoffset>+10:00</utcoffset>
  26. <isdst>false</isdst>
  27. <effectiveUntil>2008-10-25 18:00:00</effectiveUntil>
  28. </next>
  29. </zone>
  30. </time>
  31. </timezone>

How can we use Groovy to request for the data and access its detail ?

  • Get yourself a API Key from world time engine
  • We use the Groovy Console (groovyConsole from the shell)
  • We need the httpbuilder library
  • Copy http-builder-0.5.0.jar and all dependencies into the ./groovy/lib folder in your home folder
  • Execute this code:
    request   
    1. import groovyx.net.http.*
    2. import static groovyx.net.http.ContentType.*
    3. def worldTime = new RESTClient("http://worldtimeengine.com/api/{yourkey}/35.7647018432617/140.386001586914")
    4. def resp = worldTime.get( contentType : XML)
    5. println resp.data.time.zone.current.abbreviation.text()
    6. println resp.data

    Groovy Console

    XML Response

  • You see it is fairly simple to access specific fields with the groovy notation resp.data.time.zone.current.abbreviation.text()

How do I use this ?

  • I use Groovy classes in my java EE application. You can cut down on boilperplate code dramatically by using Groovy !
  • The above service I use in an location aware application.

July 26, 2010

Add libraries to groovyConsole ?

Filed under: groovy — Tags: , , , — admin @ 8:25 am

For little experiments and trying snippets of Groovy-Code the console is good enough. But soon you will realize, you need more libraries. You can either manually add them via “Script | Add jar file to classpath” (every time you open the console you would need to do this) or, for a permanent solution, copy it into you Groovy Home / lib folder, but that is not a  proper approach, rather copy the desired files into .groovy/lib in your user home directory (Linux).

groovyConsole

Add libraries

Powered by WordPress