Comparing version 1.1.2 to 1.1.3
@@ -1,8 +0,5 @@ | ||
'use strict' | ||
const req = require('request') | ||
const moment = require('moment') | ||
const queryString = require('query-string') | ||
const req = require("request") | ||
const moment = require("moment") | ||
const queryString = require("query-string") | ||
const truthyOrZero = value => !!value || parseFloat(value) === 0 | ||
class DarkSky { | ||
@@ -13,8 +10,15 @@ constructor(apiKey) { | ||
this.lat = null | ||
this.t = null | ||
this.timeVal = null | ||
this.query = {} | ||
this.timeoutVal = 20000 | ||
} | ||
static truthyOrZero(value) { | ||
return !!value || parseFloat(value) === 0 | ||
} | ||
longitude(long) { | ||
!truthyOrZero(long) ? null : (this.long = long) | ||
if (DarkSky.truthyOrZero(long)) { | ||
this.long = long | ||
} | ||
return this | ||
@@ -24,3 +28,5 @@ } | ||
latitude(lat) { | ||
!truthyOrZero(lat) ? null : (this.lat = lat) | ||
if (DarkSky.truthyOrZero(lat)) { | ||
this.lat = lat | ||
} | ||
return this | ||
@@ -36,5 +42,7 @@ } | ||
time(time) { | ||
!truthyOrZero(time) | ||
? null | ||
: (this.t = moment(new Date(time)).format('YYYY-MM-DDTHH:mm:ss')) | ||
if (DarkSky.truthyOrZero(time)) { | ||
this.timeVal = moment(new Date(time)).format("YYYY-MM-DDTHH:mm:ss") | ||
} else { | ||
this.timeVal = null | ||
} | ||
return this | ||
@@ -44,3 +52,7 @@ } | ||
units(unit) { | ||
!unit ? null : (this.query.units = unit) | ||
if (unit) { | ||
this.query.unit = unit | ||
} else { | ||
this.query.unit = null | ||
} | ||
return this | ||
@@ -50,3 +62,7 @@ } | ||
language(lang) { | ||
!lang ? null : (this.query.lang = lang) | ||
if (lang) { | ||
this.query.lang = lang | ||
} else { | ||
this.query.lang = null | ||
} | ||
return this | ||
@@ -56,4 +72,8 @@ } | ||
exclude(blocks) { | ||
blocks = Array.isArray(blocks) ? blocks.join(',') : blocks | ||
!blocks ? null : (this.query.exclude = blocks) | ||
blocks = Array.isArray(blocks) ? blocks.join(",") : blocks | ||
if (blocks) { | ||
this.query.exclude = blocks | ||
} else { | ||
this.query.exclude = null | ||
} | ||
return this | ||
@@ -63,6 +83,19 @@ } | ||
extendHourly(param) { | ||
!param ? null : (this.query.extend = 'hourly') | ||
if (param) { | ||
this.query.extend = "hourly" | ||
} else { | ||
this.query.extend = null | ||
} | ||
return this | ||
} | ||
timeout(milliseconds) { | ||
if (milliseconds) { | ||
this.timeoutVal = milliseconds | ||
} else { | ||
this.timeoutVal = null | ||
} | ||
return this | ||
} | ||
options(options) { | ||
@@ -74,6 +107,7 @@ // get methods of "this" to invoke later | ||
method => | ||
method !== 'constructor' && | ||
method !== 'get' && | ||
method !== 'options' && | ||
method.indexOf('_') === -1 | ||
method !== "constructor" && | ||
method !== "get" && | ||
method !== "options" && | ||
method !== "truthyOrZero" && | ||
method.indexOf("_") === -1 | ||
) | ||
@@ -91,8 +125,12 @@ // get keys of options object passed | ||
_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 | ||
this.url = `https://api.darksky.net/forecast/${this.apiKey}/${this.lat},${ | ||
this.long | ||
}` | ||
if (this.timeVal) { | ||
this.url += `,${this.timeVal}` | ||
} | ||
if (this.query) { | ||
this.url += `?${queryString.stringify(this.query)}` | ||
} | ||
return true | ||
} | ||
@@ -102,18 +140,28 @@ | ||
return new Promise((resolve, reject) => { | ||
if (!truthyOrZero(this.lat) || !truthyOrZero(this.long)) | ||
reject('Request not sent. ERROR: Longitute or Latitude is missing.') | ||
if (!DarkSky.truthyOrZero(this.lat) || !DarkSky.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 | ||
req( | ||
{ url: this.url, json: true, timeout: this.timeoutVal }, | ||
(err, res, body) => { | ||
if (err) { | ||
reject(`Forecast cannot be retrieved. ERROR: ${err}`) | ||
return | ||
} | ||
if (res.statusCode !== 200) { | ||
reject( | ||
`Forecast cannot be retrieved. Response: ${res.statusCode} ${ | ||
res.statusMessage | ||
}` | ||
) | ||
return | ||
} | ||
resolve(body) | ||
} | ||
res.statusCode !== 200 | ||
? reject( | ||
`Forecast cannot be retrieved. Response: ${res.statusCode} ${res.statusMessage}` | ||
) | ||
: null | ||
resolve(body) | ||
}) | ||
) | ||
}) | ||
@@ -120,0 +168,0 @@ } |
@@ -1,5 +0,5 @@ | ||
const DarkSky = require('./dark-sky-api') | ||
const DarkSky = require("./dark-sky-api") | ||
let forecast | ||
test('return instance of darksky class', async () => { | ||
test("return instance of darksky class", async () => { | ||
const darksky = new DarkSky(process.env.DARK_SKY) | ||
@@ -9,3 +9,3 @@ expect(darksky).toBeInstanceOf(DarkSky) | ||
test('return the forecast for Toronto on 1991-06-02 using options method', async () => { | ||
test("return the forecast for Toronto on 1991-06-02 using options method", async () => { | ||
const darksky = new DarkSky(process.env.DARK_SKY) | ||
@@ -16,6 +16,6 @@ const result = await darksky | ||
latitude: 43.761539, | ||
time: '1991-06-02', | ||
units: 'ca', | ||
language: 'en', | ||
exclude: ['minutely', 'daily'], | ||
time: "1991-06-02", | ||
units: "ca", | ||
language: "en", | ||
exclude: ["minutely", "daily"], | ||
extendHourly: true | ||
@@ -29,7 +29,7 @@ }) | ||
expect(result.hourly).toBeTruthy() | ||
expect(result.currently.time).toBe(675820800) | ||
expect(result.timezone).toBe('America/Toronto') | ||
expect(result.currently.time).toBe(675817200) | ||
expect(result.timezone).toBe("America/Toronto") | ||
}) | ||
test('return the current forecast for Toronto using coordinates method and method chaining', async () => { | ||
test("return the current forecast for Toronto using coordinates method and method chaining", async () => { | ||
const darksky = new DarkSky(process.env.DARK_SKY) | ||
@@ -36,0 +36,0 @@ const result = await darksky |
{ | ||
"name": "dark-sky", | ||
"version": "1.1.2", | ||
"description": "A dead simple Dark Sky API wrapper for Nodejs using method chaining and promises.", | ||
"version": "1.1.3", | ||
"description": | ||
"A dead simple Dark Sky API wrapper for Nodejs using method chaining and promises.", | ||
"main": "dark-sky-api.js", | ||
@@ -6,0 +7,0 @@ "scripts": { |
@@ -0,0 +0,0 @@ # Dark Sky |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114601
3173
1