-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit f07fead
Showing
7 changed files
with
129 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 @@ | ||
node_modules |
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,13 @@ | ||
## Cached | ||
|
||
``` | ||
$ npm install | ||
$ npm start | ||
``` | ||
|
||
Open `http://localhost:3000/docs` | ||
|
||
## license | ||
|
||
MIT | ||
|
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,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" | ||
} | ||
} |
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,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; |
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 @@ | ||
{ | ||
"name": "openweather", | ||
"version": "1.0.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,8 @@ | ||
describe('Get Weather', function(){ | ||
it ('should get current', function(done){ | ||
|
||
}); | ||
it ('should get forecast', function(done){ | ||
|
||
}); | ||
}); |
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,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); | ||
}); |