Comparing version
@@ -1,72 +0,113 @@ | ||
'use strict'; | ||
const req = require('request'); | ||
const moment = require('moment'); | ||
const queryString = require('query-string'); | ||
'use strict' | ||
const req = require('request') | ||
const moment = require('moment') | ||
const queryString = require('query-string') | ||
const truthyOrZero = value => !!value || parseFloat(value) === 0 | ||
class DarkSky { | ||
constructor(apiKey) { | ||
this.apiKey = apiKey; | ||
this.long = null; | ||
this.lat = null; | ||
this.t = null; | ||
this.query = {} | ||
} | ||
constructor(apiKey) { | ||
this.apiKey = apiKey | ||
this.long = null | ||
this.lat = null | ||
this.t = null | ||
this.query = {} | ||
} | ||
longitude(long) { | ||
!long ? null : this.long = long; | ||
return this; | ||
} | ||
longitude(long) { | ||
!truthyOrZero(long) ? null : (this.long = long) | ||
return this | ||
} | ||
latitude(lat) { | ||
!lat ? null : this.lat = lat; | ||
return this; | ||
} | ||
latitude(lat) { | ||
!truthyOrZero(lat) ? null : (this.lat = lat) | ||
return this | ||
} | ||
time(time) { | ||
!time ? null : this.t = moment(time).format('YYYY-MM-DDTHH:mm:ss'); | ||
return this; | ||
} | ||
coordinates({ lat, lng }) { | ||
this.lat = parseFloat(lat) | ||
this.lng = parseFloat(lng) | ||
return this | ||
} | ||
units(unit) { | ||
!unit ? null : this.query.units = unit; | ||
return this; | ||
} | ||
time(time) { | ||
!truthyOrZero(time) | ||
? null | ||
: (this.t = moment(new Date(time)).format('YYYY-MM-DDTHH:mm:ss')) | ||
return this | ||
} | ||
language(lang) { | ||
!lang ? null : this.query.lang = lang; | ||
return this; | ||
} | ||
units(unit) { | ||
!unit ? null : (this.query.units = unit) | ||
return this | ||
} | ||
exclude(blocks) { | ||
!blocks ? null : this.query.exclude = blocks; | ||
return this; | ||
} | ||
language(lang) { | ||
!lang ? null : (this.query.lang = lang) | ||
return this | ||
} | ||
extendHourly(param) { | ||
!param ? null : this.query.extend = 'hourly'; | ||
return this; | ||
} | ||
exclude(blocks) { | ||
blocks = Array.isArray(blocks) ? blocks.join(',') : blocks | ||
!blocks ? null : (this.query.exclude = blocks) | ||
return this | ||
} | ||
generateReqUrl() { | ||
this.url = `https://api.darksky.net/forecast/${this.apiKey}/${this.lat},${this.long}`; | ||
this.t ? this.url += `,${this.t}` : this.url; | ||
this.query ? this.url += `?${queryString.stringify(this.query)}` : this.url; | ||
} | ||
extendHourly(param) { | ||
!param ? null : (this.query.extend = 'hourly') | ||
return this | ||
} | ||
get() { | ||
return new Promise((resolve, reject) => { | ||
if(!this.lat || !this.long) reject("Request not sent. ERROR: Longitute or Latitude is missing.") | ||
this.generateReqUrl(); | ||
req({ url: this.url, json: true }, (err, res, body) => { | ||
if (err) { | ||
reject(`Forecast cannot be retrieved. ERROR: ${err}`) | ||
return | ||
} | ||
res.statusCode !== 200 ? reject(`Forecast cannot be retrieved. Response: ${res.statusCode} ${res.statusMessage}`) : null; | ||
resolve(body) | ||
}) | ||
}) | ||
} | ||
options(options) { | ||
// get methods of "this" to invoke later | ||
let methods = Object.getOwnPropertyNames( | ||
Object.getPrototypeOf(this) | ||
).filter( | ||
method => | ||
method !== 'constructor' && | ||
method !== 'get' && | ||
method !== 'options' && | ||
method.indexOf('_') === -1 | ||
) | ||
// get keys of options object passed | ||
return Object.keys(options).reduce((acc, val) => { | ||
// ignore methods that do not exist | ||
if (methods.indexOf(val) > -1) { | ||
// invoke setter methods with values of option | ||
return this[val](options[val]) | ||
} | ||
}, this) | ||
} | ||
_generateReqUrl() { | ||
this.url = `https://api.darksky.net/forecast/${this.apiKey}/${this | ||
.lat},${this.long}` | ||
this.t ? (this.url += `,${this.t}`) : this.url | ||
this.query | ||
? (this.url += `?${queryString.stringify(this.query)}`) | ||
: this.url | ||
} | ||
get() { | ||
return new Promise((resolve, reject) => { | ||
if (!truthyOrZero(this.lat) || !truthyOrZero(this.long)) | ||
reject('Request not sent. ERROR: Longitute or Latitude is missing.') | ||
this._generateReqUrl() | ||
req({ url: this.url, json: true }, (err, res, body) => { | ||
if (err) { | ||
reject(`Forecast cannot be retrieved. ERROR: ${err}`) | ||
return | ||
} | ||
res.statusCode !== 200 | ||
? reject( | ||
`Forecast cannot be retrieved. Response: ${res.statusCode} ${res.statusMessage}` | ||
) | ||
: null | ||
resolve(body) | ||
}) | ||
}) | ||
} | ||
} | ||
module.exports = DarkSky |
{ | ||
"name": "dark-sky", | ||
"version": "1.0.10", | ||
"version": "1.1.0", | ||
"description": "A dead simple Dark Sky API wrapper for Nodejs using method chaining and promises.", | ||
@@ -5,0 +5,0 @@ "main": "dark-sky-api.js", |
@@ -16,26 +16,65 @@ # Dark Sky | ||
### Script Example | ||
### Usage Examples | ||
```Javascript | ||
'use strict'; | ||
const DarkSky = require('dark-sky') | ||
const forecast = new DarkSky('<< Your API Key >>') | ||
const darksky = new DarkSky('<< Your API Key >>') | ||
forecast | ||
.latitude('37.8267') \\ required: latitude, string. | ||
.longitude('-122.423') \\ required: longitude, string. | ||
// Example 1 - Method chaining, as promise. | ||
darksky | ||
.latitude('37.8267') \\ required: latitude, string || float. | ||
.longitude(-122.423) \\ required: longitude, string || float. | ||
.time('2016-01-28') \\ optional: date, string 'YYYY-MM-DD'. | ||
.units('ca') \\ optional: units, string, refer to API documentation. | ||
.language('en') \\ optional: language, string, refer to API documentation. | ||
.exclude('minutely,daily') \\ optional: exclude, string, refer to API documentation. | ||
.exclude('minutely,daily') \\ optional: exclude, string || array, refer to API documentation. | ||
.extendHourly(true) \\ optional: extend, boolean, refer to API documentation. | ||
.get() \\ execute your get request. | ||
.then(res => { \\ handle your success response. | ||
console.log(res) | ||
.then(console.log) | ||
.catch(console.log) \\ handle your error response. | ||
// Example 2 - Setting coordinates shorthand, as promise. | ||
darksky | ||
.coordinates({lat: 37.8267, lng: -122.423}) | ||
.units('ca') | ||
.language('en') | ||
.exclude('minutely,daily') | ||
.get() | ||
.then(console.log) | ||
.catch(console.log) | ||
// Example 3 - Setting options shorthand, as promise. | ||
darksky | ||
.options({ | ||
latitude: 37.8267, | ||
longitude: -122.423, | ||
time: '2017-08-10', | ||
language: 'en', | ||
exclude: ['minutely', 'daily'], | ||
extendHourly: true | ||
}) | ||
.catch(err => { \\ handle your error response. | ||
console.log(err) | ||
}) | ||
.get() | ||
.then(console.log) | ||
// Example 4 - Modern endpoint example, as async/await. | ||
app.use('/a-week-ago', async (req, res, next) => { | ||
try { | ||
const { latitude, longitude } = req.body | ||
const forecast = await darksky | ||
.options({ | ||
latitude, | ||
longitude, | ||
time: moment().subtract(1, 'weeks') | ||
}) | ||
.get() | ||
res.status(200).json(forecast) | ||
} catch (err) { | ||
next(err) | ||
} | ||
}) | ||
``` | ||
####License | ||
### License | ||
MIT License | ||
@@ -42,0 +81,0 @@ Copyright (c) 2016 Elias Hussary |
6754
35.11%98
60.66%86
82.98%