Skip to content

Commit

Permalink
Merge pull request #6 from evanshortiss/dev
Browse files Browse the repository at this point in the history
update to mitigate vulnerabilities in dependencies.
  • Loading branch information
evanshortiss committed Apr 28, 2017
2 parents 47285c1 + a037b65 commit f7cf0a0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 35 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions example/dublin-weather.js
Original file line number Diff line number Diff line change
@@ -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);
});
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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 {
/**
Expand All @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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"
}
}
46 changes: 24 additions & 22 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
'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
};

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);
});

describe('#getWeather', function () {
it('should return an error for malformed xml payloads', function () {
stubs[YRNO].locationforecast.yields(null, '<xml><invalid </invalid></xml>');
stubs[YRNO]({}).locationforecast.yields(null, '<xml><invalid </invalid></xml>');

return lib().getWeather(LOCATION)
.then(() => {
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down

0 comments on commit f7cf0a0

Please sign in to comment.