Created
August 30, 2012 17:09
-
-
Save zthomae/3533641 to your computer and use it in GitHub Desktop.
FRED toolkit in node.js - The beginning
This file contains hidden or 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
| var express = require('express'); | |
| var app = express(); | |
| require('./core.js')(app, {'namespace': 'api', 'api_key': 'abcdefghijklmnopqrstuvwxyz123456' }); | |
| app.listen(3000); |
This file contains hidden or 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
| var express = require('express'); | |
| module.exports = function(parent, options) { | |
| var api_methods = require('./methods'), | |
| app = express(), | |
| api_endpoint = 'http://api.stlouisfed.org/fred/'; | |
| function create_url() { | |
| var args = Array.prototype.slice.call(arguments, 0, arguments.length-1), | |
| id = Array.prototype.pop.call(arguments); | |
| return api_endpoint + args.join('/') + '?' + args[0] + '_id=' + id + '&api_key=' + (options['api_key'] || 'NO_API_KEY_GIVEN'); | |
| }; | |
| api_methods.forEach(function(data) { | |
| var query = '/' + data.parent + '/:' + data.parent; | |
| if (options['namespace']) query = '/' + options['namespace'] + query; | |
| app.get(query, function(req, res) { | |
| res.send('looking at ' + create_url(data.parent, req.params[data.parent])); | |
| }); | |
| data.children.forEach(function(child) { | |
| var query = '/' + data.parent + '/' + child + '/:' + data.parent; | |
| if (options['namespace']) query = '/' + options['namespace'] + query; | |
| app.get(query, function(req, res) { | |
| res.send('looking at ' + create_url(data.parent, child, req.params[data.parent])); | |
| }); | |
| }); | |
| }); | |
| parent.use(app); | |
| }; |
This file contains hidden or 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
| module.exports = [ | |
| { | |
| 'parent': 'category', | |
| 'children': [ | |
| 'children', | |
| 'related', | |
| 'series'] | |
| }, | |
| { | |
| 'parent': 'releases', | |
| 'children': [ | |
| 'dates'] | |
| }, | |
| { | |
| 'parent': 'release', | |
| 'children': [ | |
| 'dates', | |
| 'series', | |
| 'sources'] | |
| }, | |
| { | |
| 'parent': 'series', | |
| 'children': [ | |
| 'categories', | |
| 'observations', | |
| 'release', | |
| 'search', | |
| 'updates', | |
| 'vintagedates'] | |
| }, | |
| { | |
| 'parent': 'sources', | |
| 'children': [] | |
| }, | |
| { | |
| 'parent': 'source', | |
| 'children': [ | |
| 'releases'] | |
| } | |
| ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment