Last active
May 16, 2016 16:00
-
-
Save juanpujol/e548a289727b799725264ec6331aceae to your computer and use it in GitHub Desktop.
Node.js Google timezone API wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const request = require('request-promise'); | |
module.exports = TimeZone; | |
function TimeZone(options) { | |
if(!options.apiKey) { | |
throw new Error('api key is required'); | |
} | |
if (!(this instanceof TimeZone)) { | |
return new TimeZone(); | |
} | |
this.url = 'https://maps.googleapis.com/maps/api/timezone/json'; | |
this.apiKey = options.apiKey; | |
} | |
function validateOptions(options) { | |
if(!options.lat) { throw new Error('lat parameter is required') } | |
if(!options.lon) { throw new Error('lon parameter is required') } | |
if(!options.timestamp) { throw new Error('timestamp parameter is required') } | |
} | |
TimeZone.prototype.get = function(options) { | |
validateOptions(options); | |
let rOptions = { | |
uri: this.url, | |
qs: { | |
location: `${options.lat},${options.lon}`, | |
timestamp: options.timestamp, | |
key: this.apiKey | |
}, | |
json: true | |
} | |
return request(rOptions); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment