Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cendrizzi committed Jul 30, 2013
1 parent 1a3acce commit d8c4337
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage
.idea
node_modules
/test/devkey
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
REPORTER = Spec

test:
./node_modules/.bin/mocha --reporter $(REPORTER)
.PHONY: test
coverage:
istanbul cover _mocha -- -R spec
@echo
@echo open coverage/lcov-report/index.html file in your browser
.PHONY: coverage
Empty file added README.md
Empty file.
91 changes: 91 additions & 0 deletions lib/wundergroundnode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var request = require('request');
var _ = require('underscore');

var Wunderground = function(apikey) {
"use strict";

var that = this;

that.chainedRequests = [];
var format = ".json";

that.conditions = function() {
this.chainedRequests.push("conditions/");
return this;
};

that.hourlyForecast = function() {
this.chainedRequests.push("hourly/");
return this;
};

that.hourlyTenDayForecast = function(){
this.chainedRequests.push('hourly10day/');
return this;
};

that.forecast = function() {
this.chainedRequests.push("forecast/");
return this;
};

that.almanac = function() {
this.chainedRequests.push("almanac/");
return this;
};

that.yesterday = function() {
this.chainedRequests.push("yesterday/");
return this;
};

that.geolookup = function() {
this.chainedRequests.push("geolookup/");
return this;
};

that.astronomy = function() {
this.chainedRequests.push("astronomy/");
return this;
};

/**
* Performs the actual request
*
* @param query
* @param callback
*/
that.request = function(query, callback){
// console.log('request called ', query, this.chainedRequests.length);

// A little pre-query validation
if (!query){
callback(true, "You must supply a query");
return;
}else if (!that.chainedRequests.length){
callback(true, "You must specify a resource to request first (e.g., wu.conditions().req...)");
return;
}else if (!_.isFunction(callback)){
throw "The second argument must be a function";
}

// Construct the url
var url = 'http://api.wunderground.com/api/' + apikey + '/' + this.chainedRequests.join('') + 'q/'+query + format;
console.log('url', url);

// Request the url
request(url, function (error, response, body) {
that.chainedRequests = [];
if (!error && response.statusCode == 200) {
callback.call(that, error, JSON.parse(body));
return;
} else if (error) {
callback.call(that, error, false);
return;
}

});
}
};

module.exports = Wunderground;
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "wundergroundnode",
"version": "0.1.0",
"main": "./lib/wundergroundnode.js",
"description": "Weather Underground chainable API for nodeJS",
"homepage": "http://www.github.com/cendrizzi/wundergroundnode",
"keywords": [
"weather",
"wunderground"
],
"author": {
"name": "Clark Endrizzi"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/cendrizzi/wundergroundnode/LICENSE.txt"
}
],
"dependencies": {
"request": "= 2.9.201",
"limiter": "=1.0.3",
"underscore" : "1.x"
},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"repository": {
"type": "git",
"url": "git://github.com/cendrizzi/wundergroundnode.git"
},
"engines": {
"node": ">=0.8.14"
},
"readmeFilename": "README.md",
"_id": "wundergroundnode@0.1.0"
}
106 changes: 106 additions & 0 deletions test/testWundergroundnode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
require('should');
var Wunderground = require('./../lib/wundergroundnode');
var fs = require('fs')

// Callback on connect
cachedDevKey = null;
var getDevKey = function(callback){
"use strict";

if (cachedDevKey){
callback(cachedDevKey);
return;
}

fs.readFile(__dirname+'/devkey', 'utf8', function (err,data) {
"use strict";

if (err) {
return console.error(err);
}

cachedDevKey = data;
callback(cachedDevKey);
});
}



describe('Testing Weather Underground Node Client:', function(){
"use strict";

it('Simple single call for conditions.', function(done){

getDevKey(function(key){
var wunderground = new Wunderground(key);
wunderground.conditions().request('84111', function(err, response){
response.should.have.property('current_observation');
done();
})
})

});

it('Request for geolookup.', function(done){

getDevKey(function(key){
var wunderground = new Wunderground(key);
wunderground.geolookup().request('84111', function(err, response){
response.should.have.property('location');
done();
})
})

});

it('Request for astronomy.', function(done){

getDevKey(function(key){
var wunderground = new Wunderground(key);
wunderground.astronomy().request('84111', function(err, response){
response.should.have.property('moon_phase');
done();
})
})

});

it('Chain the most of the rest resources.', function(done){

getDevKey(function(key){
var wunderground = new Wunderground(key);
wunderground.hourlyForecast().hourlyTenDayForecast().forecast().almanac().yesterday().request('84111', function(err, response){
response.should.have.property('almanac');
response.should.have.property('hourly_forecast');
response.should.have.property('forecast');
response.should.have.property('history');
done();
})
})

});

it('Call without a resource', function(done){

getDevKey(function(key){
var wunderground = new Wunderground(key);
wunderground.request('84111', function(err, response){
err.should.be.true;
done();
})
})

});

it('Call without a query', function(done){

getDevKey(function(key){
var wunderground = new Wunderground(key);
wunderground.conditions().request(false, function(err, response){
err.should.be.true;
done();
})
})

});
});

0 comments on commit d8c4337

Please sign in to comment.