weathercompanyapi-node
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -155,40 +155,56 @@ /** | ||
self._request = function (cb) { | ||
if (!self.options.apiKey) { | ||
cb.call(self, new Error('apiKey is Missing'), {}); | ||
return false; | ||
} | ||
var doRequest = function (callbk) { | ||
if (!self.options.apiKey) { | ||
callbk.call(self, new Error('apiKey is Missing'), {}); | ||
return false; | ||
} | ||
var url = 'https://api.weather.com/' + self.apiVersion + '/'; | ||
var url = 'https://api.weather.com/' + self.apiVersion + '/'; | ||
if (self.apiVersion === 'v1') { | ||
if (!self.method) { | ||
cb.call(self, new Error('method is missing'), false); | ||
return false; | ||
if (self.apiVersion === 'v1') { | ||
if (!self.method) { | ||
callbk.call(self, new Error('method is missing'), false); | ||
return false; | ||
} | ||
url += self.query + '/' + self.method + '.' + self.options.format + '?' + self._formatParams('format'); | ||
} else if (self.apiVersion === 'v3') { | ||
url += 'location/' + self.query + '&' + self._formatParams('units'); | ||
} | ||
url += self.query + '/' + self.method + '.' + self.options.format + '?' + self._formatParams('format'); | ||
} else if (self.apiVersion === 'v3') { | ||
url += 'location/' + self.query + '&' + self._formatParams('units'); | ||
// Request the url | ||
request(url, function (error, response, body) { | ||
var json = false; | ||
if (!error) { | ||
error = false; | ||
try { | ||
json = JSON.parse(body); | ||
if (_.has(json, 'success') && !json.success) { | ||
// console.error('HTTP Error in request', json.success, response.statusCode, json.errors); | ||
error = json.errors; | ||
} | ||
} catch (err) { | ||
// console.error('Exception caught in JSON.parse', body); | ||
err.body = body; | ||
error = err; | ||
} | ||
} | ||
callbk.call(self, error, json); | ||
}); | ||
}; | ||
if (cb) { | ||
return doRequest(cb) | ||
} | ||
// Request the url | ||
request(url, function (error, response, body) { | ||
var json = false; | ||
if (!error) { | ||
error = false; | ||
try { | ||
json = JSON.parse(body); | ||
if (_.has(json, 'success') && !json.success) { | ||
// console.error('HTTP Error in request', json.success, response.statusCode, json.errors); | ||
error = json.errors; | ||
} | ||
} catch (err) { | ||
// console.error('Exception caught in JSON.parse', body); | ||
err.body = body; | ||
error = err; | ||
return new Promise(function(resolve, reject){ | ||
doRequest(function(err, json){ | ||
/* istanbul ignore if */ | ||
if (err){ | ||
reject(err); | ||
} else { | ||
resolve(json); | ||
} | ||
} | ||
cb.call(self, error, json); | ||
}); | ||
}); | ||
}; | ||
@@ -195,0 +211,0 @@ |
{ | ||
"name": "weathercompanyapi-node", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Node Client to access The Weather Company API", | ||
@@ -5,0 +5,0 @@ "main": "./lib/weathercompanyapi-node", |
@@ -323,2 +323,37 @@ /** | ||
}); | ||
it('should return data for current conditions using promise', function (done) { | ||
var api = new WeatherApi(cachedDevKey); | ||
api.should.be.instanceOf(WeatherApi); | ||
api.units('e').language('en-US').geocode('37.317850,-122.035920').call('observations/current').then(function (data) { | ||
data.should.be.Object(); | ||
data.should.be.instanceOf(Object).and.have.property('observation'); | ||
data.metadata.language.should.equal('en-US'); | ||
data.metadata.units.should.equal('e'); | ||
data.metadata.status_code.should.equal(200); | ||
data.observation.should.have.property('imperial'); // because of units = e | ||
data.observation.imperial.should.have.property('temp').and.is.a.Number(); | ||
data.observation.imperial.should.have.property('altimeter').and.is.a.Number(); | ||
data.observation.imperial.should.have.property('wspd').and.is.a.Number(); | ||
}).catch(function (err) { | ||
err.should.be.false(); | ||
}).then(done); | ||
}); | ||
it('should fail retrieving current conditions using promise', function (done) { | ||
var api = new WeatherApi(); | ||
api.should.be.instanceOf(WeatherApi); | ||
api.units('e').language('en-US').geocode('37.317850,-122.035920').call('observations/current').then(function (data) { | ||
// should never get into this | ||
data.should.be.false(); | ||
}).catch(function (err) { | ||
err.should.be.Error(); | ||
err.message.should.equal('apiKey is Missing'); | ||
}).then(done); | ||
}); | ||
}); |
22323
6
493