dark-sky-api
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -44,2 +44,3 @@ 'use strict'; | ||
// _language; | ||
// _postProcessor | ||
@@ -50,3 +51,3 @@ /** | ||
*/ | ||
function DarkSkyApi(apiKey, proxyUrl, units, language) { | ||
function DarkSkyApi(apiKey, proxyUrl, units, language, processor) { | ||
var _this = this; | ||
@@ -56,3 +57,3 @@ | ||
this.setPosition = function (_ref) { | ||
this.position = function (_ref) { | ||
var latitude = _ref.latitude, | ||
@@ -70,2 +71,3 @@ longitude = _ref.longitude; | ||
this._language = language || 'en'; | ||
this._postProcessor = processor || null; | ||
} | ||
@@ -83,3 +85,3 @@ | ||
value: function initialize(position) { | ||
this.setPosition(position); | ||
this.position(position); | ||
this.initialized = true; | ||
@@ -128,2 +130,14 @@ return this; | ||
/** | ||
* Add a post processor for weather items - accepts a weather data object as single parameter - must return object | ||
* @param {function} func | ||
*/ | ||
}, { | ||
key: 'postProcessor', | ||
value: function postProcessor(func) { | ||
this._postProcessor = func; | ||
return this; | ||
} | ||
/** | ||
* Get forecasted week of weather | ||
@@ -172,2 +186,3 @@ */ | ||
}); | ||
daily.updatedDateTime = (0, _moment2.default)(); | ||
return daily; | ||
@@ -195,4 +210,5 @@ }); | ||
!item.apparentTemperatureMaxTime ? null : item.apparentTemperatureMaxDateTime = _moment2.default.unix(item.apparentTemperatureMaxTime); | ||
!this._postProcessor ? null : item = this._postProcessor(item); | ||
return item; | ||
!item.precipIntensityMaxTime ? null : item.precipIntensityMaxDateTime = _moment2.default.unix(precipIntensityMaxTime); | ||
} | ||
@@ -257,3 +273,3 @@ | ||
*/ | ||
value: function initialize(apiKey, proxyUrl, units, language) { | ||
value: function initialize(apiKey, proxyUrl, units, language, postProcessor) { | ||
if (this._api) { | ||
@@ -271,3 +287,4 @@ return; | ||
var lang = language || this.language || ''; | ||
this._api = new DarkSkyApi(key, proxy, unit, lang); | ||
var processor = postProcessor || this.postProcessor || null; | ||
this._api = new DarkSkyApi(key, proxy, unit, lang, processor); | ||
} | ||
@@ -312,2 +329,14 @@ | ||
/** | ||
* Set post processor for weather items - accepts a weather data object as single parameter - initialize or configure with api key or proxy first - must return object | ||
* @param {function} func | ||
*/ | ||
}, { | ||
key: 'setPostProcessor', | ||
value: function setPostProcessor(func) { | ||
this.initialize(); | ||
this._api.postProcessor(func); | ||
} | ||
/** | ||
* Get today's weather - Promise | ||
@@ -314,0 +343,0 @@ * @param {object} [position] - if omitted will use loadPositionAsync |
{ | ||
"name": "dark-sky-api", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "a simple and robust dark sky api service for client-side js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
121
README.md
@@ -42,2 +42,6 @@ # dark-sky-api | ||
DarkSKyApi.language = 'de'; // default 'en' | ||
DarkSkyApi.postProcessor = (item) => { // default null; | ||
item.day = item.dateTime.format('ddd'); | ||
return item; | ||
} | ||
``` | ||
@@ -50,3 +54,3 @@ | ||
```javascript | ||
DarkSkyApi.getCurrent() | ||
DarkSkyApi.loadCurrent() | ||
.then(result => console.log(result)); | ||
@@ -58,3 +62,3 @@ ``` | ||
```javascript | ||
DarkSkyApi.getForecast() | ||
DarkSkyApi.loadForecast() | ||
.then(result => console.log(result)); | ||
@@ -73,3 +77,3 @@ ``` | ||
}; | ||
DarkSkyApi.getCurrent(position) | ||
DarkSkyApi.loadCurrent(position) | ||
.then(result => console.log(result)); | ||
@@ -80,13 +84,116 @@ ``` | ||
To get the units used in dark sky api responses per configured unit type (default is 'us') use GetResponseUnits after configuration. | ||
To get the units used in dark sky api responses per configured unit type (default is 'us') use GetResponseUnits after configuration. Keep in mind that the units would need to be retrieved again if you changed the api units. | ||
```javascript | ||
const units = DarkSkyApi.getResponseUnits(); | ||
const responseUnits = DarkSkyApi.getResponseUnits(); | ||
DarkSkyAPi.loadCurrent() | ||
.then((data) => { | ||
console.log(`The temperature is ${data.temperature} degrees ${responseUnits.temperature}`); | ||
console.log(`The wind speed is ${data.windSpeed} ${responseUnits.windSpeed}`); | ||
}); | ||
``` | ||
### Post Processor | ||
The post processor method is mapped to all weather items. It's an easy way to add or manipulate responses for an app. | ||
```javascript | ||
// import | ||
import DarkSkyApi from 'dark-sky-api'; | ||
// configure | ||
DarkSkyApi.apiKey = 'my-api-key'; | ||
DarkSkyApi.postProcessor = (item) => { // must accept weather data item param | ||
// add a nice date representation using moment.calender | ||
item.dayNice = item.dateTime.calendar(null, { | ||
sameDay: '[Today]', | ||
nextDay: 'ddd', | ||
nextWeek: 'ddd', | ||
lastDay: '[Yesterday]', | ||
lastWeek: '[Last] ddd', | ||
sameElse: 'ddd' | ||
}); | ||
// add units object onto item | ||
item.units = DarkSkyApi.getResponseUnits(); // this would be outdated if you changed api units later | ||
return item; // must return weather dat item | ||
}; | ||
// use | ||
DarkSkyApi.loadCurrent() | ||
.then(data => console.log(data.dayNice)); // Today | ||
``` | ||
### Initialization | ||
tldr: it's automatic, but configure first or do it manually. | ||
Implicit (suggested) | ||
This happens automatically when making a method call such as loadCurrent or LoadForecast. Remember to configure before hand. | ||
explicite | ||
```javascript | ||
DarkSkyApi.apiKey = 'my-api-key'; | ||
DarkSkyApi.initialize(); | ||
``` | ||
or | ||
```javascript | ||
DarkSkyApi.initialize(apiKey, proxyUrl, units, language, postProcessor); // only apiKey or proxyUrl are required | ||
``` | ||
#### Configure after initialization/use | ||
```javascript | ||
DarkSkyApi.setUnits('auto'); | ||
DarkSkyApi.setLanguage('x-pig-latin'); | ||
DarkSkyApi.setPostProcessor((item) => { | ||
return { | ||
temperature: item.temperatureMax || item.temperature, | ||
icon: item.icon | ||
}; | ||
}) | ||
``` | ||
### Instantiation | ||
If you need to maintain multiple instances (configurations) of dark-sky-api create an instance. | ||
```javascript | ||
// import | ||
import DarkSkyApi from 'dark-sky-api'; | ||
// instantiate | ||
const api = new DarkSkyApi(apiKey, proxyUrl, units, language, processor); // only apiKey or proxyUrl are required | ||
// instance config methods support method chaining | ||
api.units('us') | ||
.language('en') | ||
.postProcessor(item => item) | ||
.loadCurrent() | ||
.then(console.log); | ||
// change position | ||
position = { | ||
latitude: 43.075284, | ||
longitude: -89.384318 | ||
}; | ||
api.setPosition(position); | ||
// change back | ||
api.loadPositionAsync() // get current position | ||
.then(position => { | ||
api.setPosition(position); | ||
}); | ||
``` | ||
#### To Do | ||
* show examples of instantiation | ||
* show example of using results with units | ||
* add hourly and minutely api methods | ||
* add flags and alerts apit methods |
@@ -28,2 +28,3 @@ import darkSkySkeleton from 'dark-sky-skeleton'; | ||
// _language; | ||
// _postProcessor | ||
@@ -34,6 +35,7 @@ /** | ||
*/ | ||
constructor(apiKey, proxyUrl, units, language) { | ||
constructor(apiKey, proxyUrl, units, language, processor) { | ||
this.darkSkyApi = new darkSkySkeleton(apiKey, proxyUrl); | ||
this._units = units || 'us'; | ||
this._language = language || 'en'; | ||
this._postProcessor = processor || null; | ||
} | ||
@@ -47,3 +49,3 @@ | ||
initialize(position) { | ||
this.setPosition(position); | ||
this.position(position); | ||
this.initialized = true; | ||
@@ -57,3 +59,3 @@ return this; | ||
*/ | ||
setPosition = ({ latitude, longitude }) => { | ||
position = ({ latitude, longitude }) => { | ||
this.darkSkyApi | ||
@@ -92,2 +94,11 @@ .latitude(latitude) | ||
/** | ||
* Add a post processor for weather items - accepts a weather data object as single parameter - must return object | ||
* @param {function} func | ||
*/ | ||
postProcessor(func) { | ||
this._postProcessor = func; | ||
return this; | ||
} | ||
/** | ||
* Get forecasted week of weather | ||
@@ -123,2 +134,3 @@ */ | ||
daily.data = daily.data.map(item => this.processWeatherItem(item)); | ||
daily.updatedDateTime = moment(); | ||
return daily; | ||
@@ -143,4 +155,5 @@ }); | ||
!item.apparentTemperatureMaxTime ? null : item.apparentTemperatureMaxDateTime = moment.unix(item.apparentTemperatureMaxTime); | ||
!this._postProcessor ? null : item = this._postProcessor(item); | ||
return item; | ||
!item.precipIntensityMaxTime ? null : item.precipIntensityMaxDateTime = moment.unix(precipIntensityMaxTime); | ||
} | ||
@@ -192,2 +205,3 @@ | ||
static language; | ||
static postProcessor; | ||
@@ -204,3 +218,3 @@ /** | ||
*/ | ||
static initialize(apiKey, proxyUrl, units, language) { | ||
static initialize(apiKey, proxyUrl, units, language, postProcessor) { | ||
if (this._api) { | ||
@@ -218,3 +232,4 @@ return; | ||
const lang = language || this.language || ''; | ||
this._api = new DarkSkyApi(key, proxy, unit, lang); | ||
const processor = postProcessor || this.postProcessor || null; | ||
this._api = new DarkSkyApi(key, proxy, unit, lang, processor); | ||
} | ||
@@ -231,3 +246,2 @@ | ||
/** | ||
@@ -252,2 +266,11 @@ * Set unit type for response formatting - initialize or configure with api key or proxy first | ||
/** | ||
* Set post processor for weather items - accepts a weather data object as single parameter - initialize or configure with api key or proxy first - must return object | ||
* @param {function} func | ||
*/ | ||
static setPostProcessor(func) { | ||
this.initialize(); | ||
this._api.postProcessor(func); | ||
} | ||
/** | ||
* Get today's weather - Promise | ||
@@ -254,0 +277,0 @@ * @param {object} [position] - if omitted will use loadPositionAsync |
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
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
34555
833
194