Skip to content

Instantly share code, notes, and snippets.

@megaherz
Created February 24, 2016 10:26
Show Gist options
  • Save megaherz/c66c5dfa97d5f1a35cb0 to your computer and use it in GitHub Desktop.
Save megaherz/c66c5dfa97d5f1a35cb0 to your computer and use it in GitHub Desktop.
Get timezone offset with Google timezone API using Play2
package services
import play.api.libs.ws._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
class TimezoneProvider {
def apply(latitude: Double, longitude: Double)(implicit client: WSClient): Future[Either[Error, Long]] = {
val url: String = s"https://maps.googleapis.com/maps/api/timezone/json?location=${latitude},${longitude}&timestamp=0"
val response = WS.clientUrl(url).get()
response.map {
response => {
val status = (response.json \ "status").as[String]
if (status == "OK") {
Right((response.json \ "rawOffset").as[Long])
}
else
Left(new Error(s"Error status ${status}"))
}
}
}
}
@megaherz
Copy link
Author

The gist is for Play2 v.2.4 or higher. It requires libraryDependencies ++= Seq (ws) in build.sbt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment