Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
diorahman committed May 28, 2015
0 parents commit f07fead
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Cached

```
$ npm install
$ npm start
```

Open `http://localhost:3000/docs`

## license

MIT

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "data-id",
"version": "1.0.0",
"description": "blankon data-id",
"main": "server.js",
"scripts": {
"start": "node server",
"test": "make test"
},
"keywords": [
"blankon",
"data",
"data-id"
],
"author": "Dhi Aurrahman <[email protected]>",
"license": "MIT",
"dependencies": {
"hapi": "^8.5.2",
"hyperquest": "^1.2.0",
"joi": "^6.4.3",
"lout": "^6.2.2"
}
}
62 changes: 62 additions & 0 deletions plugins/weather/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var request = require('hyperquest');
var joi = require('joi');

var ROOT_URL = 'http://api.openweathermap.org/data/2.5';
var SECOND = 1000;
var qs = require('querystring');
var plugin = {
register: function(server, options, next) {
function openweather(type, query, next) {
r = request(ROOT_URL + '/' + type + '?' + query);
var data = '';
r.on('data', function(chunk){
data += chunk;
});
r.on('end', function(){
// todo handle if not json
next(null, JSON.parse(data));
});
r.on('error', next);
}
server.method('openweather', openweather, {
cache: {
expiresIn: 60 * SECOND
}
});
server.route({
method: 'GET',
path: '/weather/current',
config: {
handler: function(req, res) {
server.methods.openweather('current', qs.stringify(req.query), function(err, data) {
res(err || data);
});
},
validate: {
query: {
q: joi.string().insensitive().required()
}
}
}
});
server.route({
method: 'GET',
path: '/weather/forecast',
config: {
handler: function(req, res) {
server.methods.openweather('forecast', qs.stringify(req.query), function(err, data) {
res(err || data);
});
},
validate: {
query: {
q: joi.string().insensitive().required()
}
}
}
});
}
}

plugin.register.attributes = require(__dirname + '/package.json');
module.exports = plugin;
4 changes: 4 additions & 0 deletions plugins/weather/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "openweather",
"version": "1.0.0"
}
8 changes: 8 additions & 0 deletions plugins/weather/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('Get Weather', function(){
it ('should get current', function(done){

});
it ('should get forecast', function(done){

});
});
18 changes: 18 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({port : process.env.PORT || 3000});
server.register({
register: require('lout')
}, function(err){
if (err) throw err;
});
server.register({
register: require('./plugins/weather'),
options: {
}
}, function(err){
if (err) throw err;
});
server.start(function(){
console.log('->', server.info.uri);
});

0 comments on commit f07fead

Please sign in to comment.