Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justjohn committed Feb 16, 2013
0 parents commit 428c9ac
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"author": "John Roepke <john@justjohn.us> (http://john.sh/)",
"name": "weather-api",
"description": "Low level API abstractions for weather services.",
"version": "0.0.5",
"homepage": "https://github.com/justjohn/weather-api",
"licenses": [
{ "type": "BSD-2-Clause",
"url": "https://raw.github.com/justjohn/weather-api/master/LICENSE" }
],
"repository": {
"type": "git",
"url": "git://github.com/justjohn/weather-api.git"
},
"dependencies": {
"q": "0.8.x",
"common-api": "latest"
},
"main": "weather.js",
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
69 changes: 69 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var ansiTrim = require('cli-color/lib/trim'),
Q = require("q");

exports.getTerminalSize = function() {
var deferred = Q.defer();

if (process.stdout.columns) {
deferred.resolve({
columns: process.stdout.columns,
rows: process.stdout.rows
});
} else {
deferred.resolve({
columns: 70, // a sensible default
rows: -1
});
}

return deferred.promise;
}

exports.repeat = function(str, length) {
return new Array(length + 1).join(str);
};

exports.padLeft = function(string, pad, length) {
return (exports.repeat(pad, length) + string).slice(-length);
};

exports.padRight = function(string, pad, length) {
return (string + exports.repeat(pad, length)).slice(0, length);
};

exports.pad = function(string, length, chr) {
chr = chr || " ";

var pad = (length - ansiTrim(string).length) / 2,
intPad = Math.floor(pad),
start = intPad + (pad == intPad?0:1);

return exports.repeat(chr, start) + string + exports.repeat(chr, intPad);
}

exports.wordAwareFormat = function(string, length) {
var words = string.split(" ");

var output = "",
line_length = 0,
line = [];

while(words.length > 0) {
var word = ansiTrim(words.shift()),
word_length = word.length + 1;

line_length += word_length;
if (line_length > length) {
output += line.join(" ") + "\n";
line = [];
line_length = word_length;
}

line.push(word);
}

if (line.length > 0)
output += line.join(" ") + "\n";

return output;
};
107 changes: 107 additions & 0 deletions src/wunderground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var API = require('common-api').API,
Q = require('q');

var WeatherUnderground = function(api_key) {
this.api_key = api_key;
};

WeatherUnderground.prototype = new API({
hostname: 'api.wunderground.com',
format: API.FORMAT.JSON,
base: '/api/{api_key}',
urlTransform: function(url) { return url.replace('{api_key}', this.api_key); }
});

WeatherUnderground.prototype.call2 = function(path) {
var deferred = Q.defer();

var options = {
host: 'api.wunderground.com',
port: 80,
path: '/api/' + this.api_key + path,
method: 'GET'
};

var req = http.request(options, function(res) {
var output = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});

res.on('end', function () {
var json = JSON.parse(output);
deferred.resolve(json);
});
});

req.on('error', function(e) {
deferred.reject('problem with request: ' + e.message);
});

req.end();

return deferred.promise;
};

WeatherUnderground.prototype.geoforecast = function() {
var deferred = Q.defer();

this.call('/conditions/forecast/q/autoip.json').then(function(data) {
deferred.resolve(data);

}, function(err) {
deferred.reject({
type: 'callfail',
description: err
});
});

return deferred.promise;
};

WeatherUnderground.prototype.geolocate = function() {
var deferred = Q.defer();

this.call('/geolookup/q/autoip.json').then(function(data) {
if (!data.location) {
var error = data.response.error;
deferred.reject(error);

} else {
deferred.resolve(data.location);
}

}, function(err) {
deferred.reject({
type: 'callfail',
description: err
});
});

return deferred.promise;
};

WeatherUnderground.prototype.queryForecast = function(path) {
return this.forecast('/q/' + path);
};

WeatherUnderground.prototype.forecast = function(path) {
var deferred = Q.defer(),
url = '/conditions/forecast' + path + '.json';

this.call(url).then(function(data) {
deferred.resolve(data);

}, function(err) {
deferred.reject({
type: 'callfail',
description: err
});
});

return deferred.promise;
};

exports.WeatherUnderground = WeatherUnderground;

11 changes: 11 additions & 0 deletions weather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.WeatherUnderground = require('./src/wunderground').WeatherUnderground;

var api_key = process.env.WEATHER_API,
API = new exports.WeatherUnderground(api_key);

API.geoforecast().then(function(data) {
console.log(data);
});

// tests

0 comments on commit 428c9ac

Please sign in to comment.