metno-client
Advanced tools
Comparing version 0.1.1 to 0.1.2
'use strict'; | ||
// var debug = require('debug')('metno:request'); | ||
var debug = require('debug')('metno:request'); | ||
var request = require('request'); | ||
@@ -15,27 +15,34 @@ var xml2js = require('xml2js'); | ||
*/ | ||
module.exports = function(options, callback) { | ||
module.exports = function(options, format, callback) { | ||
options = utils.defaults({}, options, OPTIONS); | ||
// console.log(options); | ||
// debug('starting request') | ||
if (typeof format === 'function') { | ||
callback = format; | ||
format = undefined; | ||
} | ||
var now = Date.now(); | ||
return request(options, function(err, res, body) { | ||
var sc = res && res.statusCode; | ||
debug('got request body in ' + (Date.now() - now), options.qs); | ||
if (err) { | ||
return callback(err); | ||
} else if (sc === 203 || sc === 200) { | ||
// console.log('got request in', Date.now() - time); | ||
// time = Date.now(); | ||
new xml2js.Parser({ | ||
async: true, | ||
mergeAttrs: true, | ||
explicitArray: false | ||
}).parseString(body, function(parseError, json) { | ||
if (parseError) { | ||
return callback(parseError); | ||
} | ||
// console.log('parsed request body in', Date.now() - time); | ||
return callback(null, json); | ||
}); | ||
if (format === 'xml') { | ||
new xml2js.Parser({ | ||
async: true, | ||
mergeAttrs: true, | ||
explicitArray: false | ||
}).parseString(body, function(parseError, json) { | ||
if (parseError) { | ||
return callback(parseError); | ||
} | ||
// console.log('parsed request body in', Date.now() - time); | ||
return callback(null, json); | ||
}); | ||
} else { | ||
return callback(null, body); | ||
} | ||
} else { | ||
@@ -47,3 +54,6 @@ return callback({ | ||
} | ||
}).on('response', function(response) { | ||
debug('got request response in ' + (Date.now() - now), options.qs); | ||
now = Date.now(); | ||
}); | ||
}; |
@@ -28,3 +28,3 @@ 'use strict'; | ||
return apiRequest(opts, callback); | ||
return apiRequest(opts, options.format, callback); | ||
}; | ||
@@ -31,0 +31,0 @@ } |
@@ -11,4 +11,4 @@ 'use strict'; | ||
icemap: { version: 1.0 }, | ||
locationforecast: { version: 1.9 }, | ||
locationforecastlts: { version: 1.2 }, | ||
locationforecast: { version: 1.9, format: 'xml' }, | ||
locationforecastlts: { version: 1.2, format: 'xml' }, | ||
metfare: { version: 0.1 }, | ||
@@ -15,0 +15,0 @@ metgm: { version: 1.0 }, |
{ | ||
"name": "metno-client", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "met.no node.js client", | ||
@@ -33,2 +33,3 @@ "main": "./lib/client.js", | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"request": "^2.69.0", | ||
@@ -35,0 +36,0 @@ "xml2js": "^0.4.16" |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var client = require('../lib/client'); | ||
var apiRequest = require('../lib/api_request'); | ||
@@ -25,3 +26,3 @@ describe('Client', function() { | ||
}, | ||
request: { timeout: 6000 } | ||
request: { timeout: 15000 } | ||
}, function(error, data) { | ||
@@ -36,6 +37,6 @@ assert.ok(data); | ||
params: { | ||
lat: 53.3478, | ||
lon: 6.2597 | ||
lat: 67.5000, | ||
lon: 51.8667 | ||
}, | ||
request: { timeout: 4000 }, | ||
request: { timeout: 15000 }, | ||
version: 1.9, | ||
@@ -49,2 +50,30 @@ days: 2 | ||
}); | ||
it('should get weather icon', function(done) { | ||
client.api.weathericon({ | ||
params: { | ||
symbol: 5, | ||
content_type: 'image/png' | ||
}, | ||
request: { timeout: 15000 } | ||
}, function(error, data) { | ||
assert.ok(data); | ||
done(); | ||
}); | ||
}); | ||
it('should get reuters homepage', function(done) { | ||
apiRequest({ | ||
url: 'http://www.reuters.com/' | ||
}, function(error, data) { | ||
assert.ok(data); | ||
done(); | ||
}); | ||
}); | ||
it('should get yr xml', function(done) { | ||
apiRequest({ | ||
url: 'http://www.yr.no/place/Russia/Nenetsia/Nizhnyaya_Kamenka/forecast.xml' | ||
}, function(error, data) { | ||
assert.ok(data); | ||
done(); | ||
}); | ||
}); | ||
}); |
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
9042
275
3
+ Addeddebug@^2.2.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)