From 80d3cc4ff10282cb69e0a7d99530bc1de87eb3ee Mon Sep 17 00:00:00 2001 From: Brett Langdon Date: Tue, 19 Feb 2013 19:25:27 -0500 Subject: [PATCH] version bump, use node-units over half baked self made thing --- lib/index.js | 3 +- lib/units/index.js | 162 ++++++++------------------------------------- package.json | 7 +- 3 files changed, 32 insertions(+), 140 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1becbbd..b703411 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,7 +25,8 @@ server.get('/', function(req, res){ ', example (/bjcp?id=28A, /bjcp?mouthfeel=tannin)', }; answer.units = { - '/units': 'get the currently supported list of conversions', + '/units': 'get the currently supported list of units and unit their groups', + '/units/type/:unit': 'try to determine which unit group(s) :unit belongs to', '/units/:value/:unit_from/to/:unit_to': 'convert :value from :unit_from to :unit_to, if it is supported', }; diff --git a/lib/units/index.js b/lib/units/index.js index 8e0b96d..f63bd7f 100644 --- a/lib/units/index.js +++ b/lib/units/index.js @@ -1,143 +1,33 @@ -var conversions = { - // Tempurature - 'fahrenheit': { - 'celsius': function(value){ - value -= 32; - value *= 5 - value /= 9; - return value; - }, - }, - 'celsius': { - 'fahrenheit': function(value){ - value *= 9; - value /= 5; - value += 32; - return value; - }, - }, - - // Liquid Volume - 'gallon': { - 'cup': function(value){ - return value * 16; - }, - 'liter': function(value){ - return value * 3.78541178; - }, - 'pint': function(value){ - return value * 8; - }, - 'milliliter': function(value){ - return value * 3785.41178; - } - }, - 'cup': { - 'gallon': function(value){ - return value / 16; - }, - 'liter': function(value){ - return (value / 16) * 3.78541178; - }, - 'pint': function(value){ - return value / 2; - }, - 'milliliter': function(value){ - return value * 236.588237; - }, - }, - 'liter': { - 'gallon': function(value){ - return value / 3.78541178; - }, - 'cup': function(value){ - return (value / 3.78541178) * 16; - }, - 'pint': function(value){ - return (value / 3.78541178) * 8; - }, - 'milliliter': function(value){ - return value * 1000; - }, - }, - 'milliliter': { - 'gallon': function(value){ - return value / 3785.41178; - }, - 'cup': function(value){ - return value / 236.588; - }, - 'pint': function(value){ - return value / 473.176; - }, - 'liter': function(value){ - return value / 1000; - }, - }, - 'pint': { - 'gallon': function(value){ - return value * 0.125; - }, - 'cup': function(value){ - return value * 2; - }, - 'liter': function(value){ - return value * 0.473176; - }, - 'milliliter': function(value){ - return value * 473.176; - }, - }, - - // Weight - 'pound': { - 'ounce': function(value){ - return value * 16; - }, - 'gram': function(value){ - return value * 453.592; - }, - }, - 'ounce': { - 'pound': function(value){ - return value / 16; - }, - 'gram': function(value){ - return value * 28.3495; - }, - }, - 'gram': { - 'ounce': function(value){ - return value / 28.3495; - }, - 'pound': function(value){ - return value / 453.592; - }, - }, -}; - +var units = require('node-units'); +// might as well cache this so we dont have to compute every time +// the list of available units will only change once we restart +var available = {}; +var db = units.getDB(); +for(var group in db){ + available[group] = Object.keys(db[group]); +} return module.exports = function(server){ server.get('/units', function(req, res){ - var can_convert = {}; - for(var from in conversions){ - can_convert[from] = []; - for(var to in conversions[from]){ - can_convert[from].push(to); - } - } - res.writeHead(200, { 'Content-Type': 'application/javascript' }); - res.end(JSON.stringify(can_convert)); + return res.end(JSON.stringify(available)); + }); + + server.get(/units\/type\/([a-zA-Z]+)/i, function(req, res){ + var result = units.getUnitType(req.params[0]); + res.writeHead(200, { + 'Content-Type': 'application/javascript', + }); + return res.end(JSON.stringify(result)); }); - server.get(/\/units\/([0-9]+(\.[0-9]+)?)\/([a-zA-Z]+)\/to\/([a-zA-Z]+)/i, function(req, res){ - var value = parseFloat(req.params[0]); - var from = req.params[2].toLowerCase(); - var to = req.params[3].toLowerCase(); + server.get(/\/units\/(.*?)\/([a-zA-Z]+)\/to\/([a-zA-Z]+)/i, function(req, res){ + var value = decodeURIComponent(req.params[0]); + var from = req.params[1]; + var to = req.params[2]; var answer = { 'convert': value, @@ -146,15 +36,15 @@ return module.exports = function(server){ 'result': undefined, 'error': undefined }; - if(from in conversions && to in conversions[from]){ - answer['result'] = conversions[from][to](value); - } else{ - answer['error'] = 'There is no conversion from ' + from + ' to ' + to; + try{ + answer['result'] = units.convert(value + ' ' + from + ' to ' + to); + } catch(e){ + answer['error'] = e; } res.writeHead(200, { 'Content-Type': 'application/javascript' }); - res.end(JSON.stringify(answer)); + return res.end(JSON.stringify(answer)); }); }; diff --git a/package.json b/package.json index f3f2504..53af981 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "brett-is-api", - "version": "0.1.1", + "version": "0.1.2", "description": "", "main": "lib/index.js", "scripts": { @@ -10,10 +10,11 @@ "author": "Brett Langdon (www.brett.is)", "license": "MIT", "dependencies": { - "restify": "~1.4.4" + "restify": "~1.4.4", + "node-units": "~0.1.0" }, "bin": { - "brett-is-api": "bin/brett-is-api" + "brett-is-api": "bin/brett-is-api" }, "private": true }