Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Last active May 16, 2016 16:00
Show Gist options
  • Save juanpujol/e548a289727b799725264ec6331aceae to your computer and use it in GitHub Desktop.
Save juanpujol/e548a289727b799725264ec6331aceae to your computer and use it in GitHub Desktop.
Node.js Google timezone API wrapper
'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