diff --git a/README.md b/README.md
index b5b97e3..3bfa42a 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,11 @@ Here's an example:
```js
const yrno = require('yr.no-forecast')({
- version: '1.9' // this is the default if not provided
+ version: '1.9', // this is the default if not provided,
+ request: {
+ // make calls to locationforecast timeout after 15 seconds
+ timeout: 15000
+ }
});
const LOCATION = {
@@ -44,16 +48,21 @@ yrno.getWeather(LOCATION)
### module(config)
This module exports a single factory function that can be used to get a
-configured instance that exports the `getWeather` function.
+configured `instance` that exports the `getWeather` function.
Currently supported config options:
-* version - This will be passed when making a call to the met.no API
+* version - Passed when making a call to the met.no API to select the
+locationforecast version to call
+* request - Can be populated with options for the `request` module. The only
+setting that you should need to pass is `timeout` and is demonstrated above
-### module.getWeather(params)
+### instance.getWeather(params[, version])
Returns a Promise that will resolve with a `LocationForecast` object that
-contains functions to get weather data.
+contains functions to get weather data. You can pass the version parameter if
+you want to override the default of 1.9, or the default you supplied when
+creating and instance.
### LocationForecast.getFiveDaySummary()
Returns a Promise that resolves to an Array of 5 weather data Objects.
diff --git a/example/dublin-weather.js b/example/dublin-weather.js
new file mode 100644
index 0000000..35408a3
--- /dev/null
+++ b/example/dublin-weather.js
@@ -0,0 +1,45 @@
+'use strict';
+
+const yrno = require('../index.js')({
+ version: '1.9', // this is the default if not provided,
+ request: {
+ // make calls to locationforecast timeout after 15 seconds
+ timeout: 15000
+ }
+});
+
+const LOCATION = {
+ // This is Dublin, Ireland
+ lat: 53.3478,
+ lon: 6.2597
+};
+
+console.log('\nGetting weather for Dublin, Ireland...\n');
+
+yrno.getWeather(LOCATION)
+ .then((weather) => {
+ // Get general weather for next five days (Array with five objects)
+ // weather.getFiveDaySummary()
+ // .then((data) => console.log('\n five day summary', data));
+
+ // Get a weather data point for a given time between now and 9 days ahead
+ weather.getForecastForTime(new Date())
+ .then((data) => {
+ if (data.hasOwnProperty('temperature')) {
+ console.log(`Temperature is around ${data.temperature}`);
+ }
+
+ if (data.hasOwnProperty('rain')) {
+ console.log(`Expected rainfall is ${data.rain}`);
+ }
+
+ if (data.hasOwnProperty('humidity')) {
+ console.log(`Humidity is ${data.humidity}`);
+ }
+
+ console.log('\n');
+ });
+ })
+ .catch((e) => {
+ console.log('an error occurred getting weather xml!', e);
+ });
diff --git a/index.js b/index.js
index 3dd730d..491f968 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,6 @@
'use strict';
const log = require('debug')(require('./package.json').name);
-const yrno = require('yr.no-interface');
const moment = require('moment');
const XML = require('pixl-xml');
const VError = require('verror');
@@ -11,7 +10,14 @@ const Promise = require('bluebird');
module.exports = (config) => {
// Make a default config, but extend it with the passed config
- config = Object.assign({version: 1.9}, config);
+ config = Object.assign({
+ version: 1.9
+ }, config);
+
+ // Create a yrno instance with any overrides required
+ const yrno = require('yr.no-interface')({
+ request: config.request
+ });
return {
/**
@@ -28,7 +34,10 @@ module.exports = (config) => {
return Promise.fromCallback(function (callback) {
// Make a standard call to the API
- yrno.locationforecast(params, version, function(err, body) {
+ yrno.locationforecast({
+ query: params,
+ version: version
+ }, function(err, body) {
if (err) {
log('failed to get locationforecast from yr.no API. Error:', err);
return callback(err, null);
diff --git a/package.json b/package.json
index 612be6d..8ddd7ad 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "yr.no-forecast",
- "version": "1.0.0",
+ "version": "1.0.1",
"description": "retrieve a weather forecast for a given time and location from met.no",
"main": "./index.js",
"scripts": {
@@ -10,7 +10,8 @@
"linelint": "linelint -l 120 index.js test/*.js",
"unit": "NODE_PATH=. mocha test/",
"coveralls": "npm run coverage && cat coverage/lcov.info | coveralls",
- "coverage": "NODE_PATH=. nyc mocha test/ && nyc report --reporter=lcov"
+ "coverage": "NODE_PATH=. nyc mocha test/ && nyc report --reporter=lcov",
+ "example": "node example/dublin-weather.js"
},
"files": [
"index.js"
@@ -20,15 +21,15 @@
"debug": "~2.6.3",
"lodash.filter": "~4.6.0",
"lodash.foreach": "~4.5.0",
- "moment": "~2.4.0",
+ "moment": "~2.18.1",
"pixl-xml": "~1.0.10",
"verror": "~1.9.0",
- "yr.no-interface": "~0.1.1"
+ "yr.no-interface": "~1.0.0"
},
"license": "MIT",
"repository": {
"type": "git",
- "url": "https://github.com/evanshortiss/yr.no-forecast"
+ "url": "git+https://github.com/evanshortiss/yr.no-forecast.git"
},
"keywords": [
"yr.no",
@@ -58,5 +59,13 @@
"proxyquire": "~1.7.11",
"require-uncached": "~1.0.3",
"sinon": "~2.1.0"
+ },
+ "bugs": {
+ "url": "https://github.com/evanshortiss/yr.no-forecast/issues"
+ },
+ "homepage": "https://github.com/evanshortiss/yr.no-forecast#readme",
+ "directories": {
+ "example": "example",
+ "test": "test"
}
}
diff --git a/test/test.js b/test/test.js
index 493b8aa..db6ead3 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,22 +1,22 @@
'use strict';
-var moment = require('moment');
-var proxyquire = require('proxyquire');
-var sinon = require('sinon');
-var chai = require('chai');
-var readFileSync = require('fs').readFileSync;
-var join = require('path').join;
-var uncached = require('require-uncached');
+const moment = require('moment');
+const proxyquire = require('proxyquire');
+const sinon = require('sinon');
+const chai = require('chai');
+const readFileSync = require('fs').readFileSync;
+const join = require('path').join;
+const uncached = require('require-uncached');
chai.use(require('chai-truthy'));
var expect = require('chai').expect;
-var SAMPLE_XML = readFileSync(
+const SAMPLE_XML = readFileSync(
join(__dirname, '../fixtures/weather-response-oslo.xml'), 'utf8'
);
-var LOCATION = {
+const LOCATION = {
lat: 53.3478,
lon: 6.2597
};
@@ -24,15 +24,17 @@ var LOCATION = {
describe('yr.no-forecast', function() {
this.timeout(5000);
- var lib, stubs;
+ var lib, stubs, yrno;
var YRNO = 'yr.no-interface';
beforeEach(function () {
+ yrno = {
+ locationforecast: sinon.stub().yields(null, SAMPLE_XML)
+ };
+
stubs = {
- [YRNO]: {
- locationforecast: sinon.stub().yields(null, SAMPLE_XML)
- }
+ [YRNO]: sinon.stub().returns(yrno)
};
lib = proxyquire('../index.js', stubs);
@@ -40,7 +42,7 @@ describe('yr.no-forecast', function() {
describe('#getWeather', function () {
it('should return an error for malformed xml payloads', function () {
- stubs[YRNO].locationforecast.yields(null, '');
+ stubs[YRNO]({}).locationforecast.yields(null, '');
return lib().getWeather(LOCATION)
.then(() => {
@@ -51,16 +53,16 @@ describe('yr.no-forecast', function() {
expect(err.toString()).to.contain('Parse Error: Mismatched closing tag');
expect(
- stubs[YRNO].locationforecast.getCall(0).args[1]
- ).to.equal(1.9);
- expect(
- stubs[YRNO].locationforecast.getCall(0).args[0]
- ).to.equal(LOCATION);
+ yrno.locationforecast.getCall(0).args[0]
+ ).to.deep.equal({
+ version: 1.9,
+ query: LOCATION
+ });
});
});
it('should return an error on yr.no API errors', function () {
- stubs[YRNO].locationforecast.yields(new Error('oh noes!'));
+ stubs[YRNO]({}).locationforecast.yields(new Error('oh noes!'));
return lib().getWeather(LOCATION)
.then(() => {
@@ -92,7 +94,7 @@ describe('yr.no-forecast', function() {
return lib({version: version}).getWeather(LOCATION)
.then(function() {
expect(
- stubs[YRNO].locationforecast.getCall(0).args[1]
+ yrno.locationforecast.getCall(0).args[0].version
).to.equal(version);
});
});
@@ -103,7 +105,7 @@ describe('yr.no-forecast', function() {
return lib().getWeather(LOCATION, version)
.then(function() {
expect(
- stubs[YRNO].locationforecast.getCall(0).args[1]
+ yrno.locationforecast.getCall(0).args[0].version
).to.equal(version);
});
});