forked from cendrizzi/wundergroundnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
coverage | ||
.idea | ||
node_modules | ||
/test/devkey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) | ||
}) | ||
|
||
}); | ||
}); |