geode
Advanced tools
Comparing version 0.0.6 to 1.0.0
271
geode.js
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
/* Geode | ||
@@ -6,3 +7,3 @@ * ========================== | ||
var request = require('request'); | ||
var request = require('request') | ||
@@ -12,135 +13,171 @@ /* @Constructor | ||
* @Param local :Object - local information (OPTIONAL) | ||
* @local countryCode :String - eg. "US", "CA" | ||
* @local language :String - eg. "en", "sp" | ||
* @local countryCode :String - eg. "US", "CA" | ||
* @local language :String - eg. "en", "sp", "local" | ||
*/ | ||
var Geode = function(username, local) { | ||
var that = this; | ||
that.username = (username) ? username : null; | ||
that.endpoint = 'http://api.geonames.org/'; | ||
var Geode = function (username, local) { | ||
this.username = (username) ? username : null | ||
this.endpoint = 'http://api.geonames.org/' | ||
/* only attempt to set countryCode and language | ||
if local object passed */ | ||
if(local) { | ||
that.countryCode = (local.countryCode ? local.countryCode : 'US'); | ||
that.language = (local.language ? local.language : 'en'); | ||
} | ||
/* only attempt to set countryCode and language | ||
if local object passed */ | ||
if (local) { | ||
this.countryCode = local.countryCode | ||
this.language = local.language | ||
} | ||
if(that.username) { | ||
that.ready = true; | ||
} | ||
else { | ||
throw new Error('username is required'); | ||
} | ||
if (this.username) { | ||
this.ready = true | ||
} else { | ||
throw new Error('username is required') | ||
} | ||
that.localize = { | ||
username : that.username, | ||
country : that.countryCode, | ||
language : that.language | ||
}; | ||
this.localize = { | ||
username: this.username, | ||
country: this.countryCode, | ||
lang: this.language | ||
} | ||
/* @Method error :Function - handle errors | ||
* @Param err :Object - Error object returned from request | ||
* @Param callback :Function - Function to pass error data back to | ||
*/ | ||
} | ||
that.error = function(err, callback){ | ||
if(process.env.NODE_ENV !== 'production') | ||
console.log(err); | ||
callback(err, {}); | ||
}; | ||
/* @Method error :Function - handle errors | ||
* @Param err :Object - Error object returned from request | ||
* @Param callback :Function - Function to pass error data back to | ||
*/ | ||
/* @Method merge :Function - *utility* merging objects | ||
* @Params * :Objects - passed in via arguments array, objects to merge | ||
*/ | ||
Geode.prototype.error = function (err, callback) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(err) | ||
} | ||
callback(err, {}) | ||
} | ||
that.merge = function(){ | ||
if(typeof arguments[0] === 'object' && !arguments[0].length){ | ||
var base = arguments[0]; | ||
for(var i = 1; i < arguments.length; i += 1){ | ||
for(var key in arguments[i]){ | ||
base[key] = arguments[i][key]; | ||
} | ||
} | ||
} | ||
return base; | ||
}; | ||
/* @Method merge :Function - *utility* merging objects | ||
* @Params * :Objects - passed in via arguments array, objects to merge | ||
*/ | ||
/* @Method request :Function - sends out request to geonames server | ||
* @Param collection :String - corresponds to url endpoints in api | ||
* @Param data :Object - Payload to send in query string | ||
* @Param callback :Function - Function to pass error data back to | ||
*/ | ||
Geode.prototype.merge = function () { | ||
if (typeof arguments[0] === 'object' && !arguments[0].length) { | ||
var base = arguments[0] | ||
for (var i = 1; i < arguments.length; i += 1) { | ||
for (var key in arguments[i]) { | ||
base[key] = arguments[i][key] | ||
} | ||
} | ||
} | ||
return base | ||
} | ||
that.request = function(collection, data, callback){ | ||
var url = that.endpoint + collection + 'JSON'; | ||
var payload = that.merge({},that.localize,data); | ||
request.get({ | ||
url : url, | ||
qs : payload | ||
}, function(err, res, body){ | ||
if(err) that.error(err, callback); | ||
else{ | ||
callback(null, JSON.parse(body)); | ||
} | ||
}); | ||
}; | ||
/* @Method errorOnResponseException :Function - returns an error upon an error code from server | ||
* @Param response :Object - The response coming from geonames.org | ||
*/ | ||
Geode.prototype.errorOnResponseException = function (response) { | ||
if (response && response.status && response.status.value >= 10) { | ||
var message = response.status.message | ||
var code = response.status.value | ||
return new Geode.GeodeError(message, code) | ||
} | ||
} | ||
/* All method requirement can be found here | ||
* http://www.geonames.org/export/web-services.html | ||
*/ | ||
/* @Method request :Function - sends out request to geonames server | ||
* @Param collection :String - corresponds to url endpoints in api | ||
* @Param data :Object - Payload to send in query string | ||
* @Param callback :Function - Function to pass error data back to | ||
*/ | ||
Geode.prototype.request = function (collection, data, callback) { | ||
var self = this | ||
var url = this.endpoint + collection + 'JSON' | ||
var payload = this.merge({}, this.localize, data) | ||
var parsedBody | ||
var geodeError | ||
that.methods = [ | ||
'search', | ||
'get', | ||
'postalCode', | ||
'postalCodeLookup', | ||
'findNearbyPostalCodes', | ||
'postalCodeCountryInfo', | ||
'findNearbyPlaceName', | ||
'findNearby', | ||
'extendedFindNearby', | ||
'children', | ||
'hierarchy', | ||
'neighbours', | ||
'siblings', | ||
'findNearbyWikipedia', | ||
'wikipediaSearch', | ||
'wikipediaBoundingBox', | ||
'cities', | ||
'earthquakes', | ||
'weather', | ||
'weatherIcaoJSON', | ||
'findNearByWeather', | ||
'countryInfo', | ||
'countryCode', | ||
'countrySubdivision', | ||
'ocean', | ||
'neighbourhood', | ||
'srtm3', | ||
'astergdem', | ||
'gtopo30', | ||
'timezone' | ||
]; | ||
request.get({ | ||
url: url, | ||
qs: payload | ||
}, function (err, res, body) { | ||
if (err) { | ||
self.error(err, callback) | ||
} else { | ||
parsedBody = JSON.parse(body) | ||
geodeError = self.errorOnResponseException(parsedBody) | ||
// Compile methods | ||
if (geodeError) self.error(geodeError, callback) | ||
else callback(null, parsedBody) | ||
} | ||
}) | ||
} | ||
for(var i = 0; i < that.methods.length; i += 1){ | ||
if(!that[that.methods[i]]){ | ||
(function(n){ | ||
that[that.methods[n]] = function(data, callback){ | ||
that.request(that.methods[n], data, callback); | ||
}; | ||
}(i)); | ||
} | ||
} | ||
/* All method requirement can be found here | ||
* http://www.geonames.org/export/web-services.html | ||
*/ | ||
/* Eg. | ||
* that.search = function(data, callback){ | ||
* that.request('search', data, callback); | ||
* }; | ||
*/ | ||
Geode.METHODS = [ | ||
'search', | ||
'get', | ||
'postalCode', | ||
'postalCodeLookup', | ||
'findNearbyPostalCodes', | ||
'postalCodeCountryInfo', | ||
'findNearbyPlaceName', | ||
'findNearby', | ||
'extendedFindNearby', | ||
'children', | ||
'hierarchy', | ||
'neighbours', | ||
'siblings', | ||
'findNearbyWikipedia', | ||
'wikipediaSearch', | ||
'wikipediaBoundingBox', | ||
'cities', | ||
'earthquakes', | ||
'weather', | ||
'weatherIcaoJSON', | ||
'findNearByWeather', | ||
'countryInfo', | ||
'countryCode', | ||
'countrySubdivision', | ||
'ocean', | ||
'neighbourhood', | ||
'srtm3', | ||
'astergdem', | ||
'gtopo30', | ||
'timezone' | ||
] | ||
}; | ||
// Compile methods | ||
module.exports = Geode; | ||
for (var i = 0; i < Geode.METHODS.length; i += 1) { | ||
var methodName = Geode.METHODS[i] | ||
if (!Geode.prototype[methodName]) { | ||
(function (n) { | ||
Geode.prototype[n] = function (data, callback) { | ||
this.request(n, data, callback) | ||
} | ||
}(methodName)) | ||
} | ||
} | ||
/* Eg. | ||
* Geode.prototype.search = function(data, callback){ | ||
* this.request('search', data, callback) | ||
* } | ||
*/ | ||
/* | ||
* Wraps an exception from geonames.org, see http://www.geonames.org/export/webservice-exception.html | ||
* @Constructor | ||
* @Param message :String - the error description | ||
* @Param code :Number - the error code | ||
*/ | ||
function GeodeError (message, code) { | ||
Error.captureStackTrace(this, this.constructor) | ||
this.message = message | ||
this.code = code | ||
} | ||
GeodeError.prototype = Object.create(Error.prototype) | ||
GeodeError.prototype.name = 'GeodeError' | ||
/* Accessible as class property of Geode */ | ||
Geode.GeodeError = GeodeError | ||
module.exports = Geode |
@@ -5,20 +5,26 @@ { | ||
"description": "wrapper for www.geonames.org api", | ||
"version": "0.0.6", | ||
"version": "1.0.0", | ||
"repository": "jacoblwe20/geode", | ||
"scripts" : { | ||
"test" : "mocha ./spec/geode_spec.js" | ||
"scripts": { | ||
"test": "standard && mocha ./spec/geode_spec.js" | ||
}, | ||
"main": "geode.js", | ||
"engines": { | ||
"node": "0.8" | ||
"node": "<=0.10" | ||
}, | ||
"dependencies": { | ||
"request" : "latest" | ||
"request": "^2.53.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*", | ||
"underscore": "*" | ||
"mocha": "*", | ||
"should": "*", | ||
"standard": "^2.10.0", | ||
"underscore": "*" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"spec/*" | ||
] | ||
}, | ||
"optionalDependencies": {} | ||
} |
# Geode | ||
locations api in node from www.geoname.org | ||
locations api in node from www.geonames.org | ||
@@ -5,0 +5,0 @@ ## Install |
@@ -5,2 +5,7 @@ var geode = require('../geode'), | ||
var username = process.env.USER; | ||
if ( !username ){ | ||
throw new Error( 'please run test with "USER={username} npm test"' ); | ||
} | ||
describe('#instantiate geode', function() { | ||
@@ -13,20 +18,18 @@ | ||
}); | ||
it('without "local" param should return data', function(done) { | ||
var geo = new geode('demo'); | ||
var geo = new geode(username); | ||
geo.search({ q: 'london', maxRows: 10 }, function(err, results) { | ||
should.not.exist(err); | ||
should.exist(results); | ||
results.should.have.property('geonames').with.length(10); | ||
_.first(results.geonames).should.have.property('name', 'London'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('with "local" param should return "US" only data', function(done) { | ||
var username = process.env.USER; | ||
if ( !username ){ | ||
throw new Error( 'please run test with "USER={username} npm test"' ); | ||
} | ||
var geo = new geode( username , { language: 'en', country : 'US' }); | ||
var geo = new geode( username , { language: 'en', countryCode : 'US' }); | ||
geo.search({ q: 'london', maxRows: 10 }, function(err, results) { | ||
@@ -36,7 +39,19 @@ should.not.exist(err); | ||
results.should.have.property('geonames').with.length(10); | ||
_.first(results.geonames).should.have.property('countryCode', 'US'); | ||
_.first(results.geonames).should.have.property('countryCode', 'US'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('returns an GeodeError with bad username', function(done) { | ||
var geo = new geode('some_weired_username_hopefully_never_exists'); | ||
geo.search({ q: 'london', maxRows: 10 }, function(err, results) { | ||
should.exist(err); | ||
err.should.have.property('name','GeodeError'); | ||
err.should.have.property('code',10); | ||
err.should.have.property('message','user does not exist.'); | ||
done(); | ||
}); | ||
}); | ||
}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8260
6
202
1
4
1
Updatedrequest@^2.53.0