Browse Source

added units api endpoint

master
Brett Langdon 13 years ago
parent
commit
a2f40c0de3
4 changed files with 181 additions and 1 deletions
  1. +15
    -0
      README.md
  2. +5
    -0
      lib/index.js
  3. +160
    -0
      lib/units/index.js
  4. +1
    -1
      package.json

+ 15
- 0
README.md View File

@ -34,3 +34,18 @@ $ curl http://api.brett.is/bjcp?id=19A
$ curl http://api.brett.is/bjcp?flavor=malty
[{"name":"Lite American Lager","aroma":["Little to no malt aroma, although it can be grainy, sweet or corn-like if present","Hop aroma may range from none to a light, spicy or floral hop presence","Low levels of yeast character (green apples, DMS, or fruitiness) are optional but acceptable","No diacetyl."],"appearance":["Very pale straw to pale yellow color","White, frothy head seldom persists","Very clear."],"flavor":["Crisp and dry flavor with some low levels of grainy or corn-like sweetness","Hop flavor ranges from none to low levels","Hop bitterness at low level","Balance may vary from slightly malty to slightly bitter, but is relatively close to even","High levels of carbonation may provide a slight acidity or dry \"sting.\" No diacetyl","No fruitiness."] ...
```
### Units
The units API is used to convert between various measurements.
To get a list of the currently available conversions:
```bash
$ curl http://api.brett.is/units
{"fahrenheit":["celsius"],"celsius":["fahrenheit"],"gallon":["cup","liter","pint","milliliter"],"cup":["gallon","liter","pint","milliliter"],"liter":["gallon","cup","pint","milliliter"],"milliliter":["gallon","cup","pint","liter"],"pint":["gallon","cup","liter","milliliter"],"pound":["ounce","gram"],"ounce":["pound","gram"],"gram":["ounce","pound"]}V
```
To convert between supported measurements:
```bash
$ curl http://api.brett.is/units/5/gallon/to/pint
{"convert":5,"from":"gallon","to":"pint","result":40}
```

+ 5
- 0
lib/index.js View File

@ -11,6 +11,7 @@ server.use(restify.throttle(throttle_settings));
require('./bjcp')(server);
require('./time')(server);
require('./units')(server);
server.get('/', function(req, res){
var answer = {};
@ -23,6 +24,10 @@ server.get('/', function(req, res){
'/bjcp': 'get the current BJCP style guide as JSON object, to search style guide provide query string parameters' +
', example (/bjcp?id=28A, /bjcp?mouthfeel=tannin)',
};
answer.units = {
'/units': 'get the currently supported list of conversions',
'/units/:value/:unit_from/to/:unit_to': 'convert :value from :unit_from to :unit_to, if it is supported',
};
res.writeHead(200, {
'Content-Type': 'application/javascript'


+ 160
- 0
lib/units/index.js View File

@ -0,0 +1,160 @@
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;
},
},
};
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));
});
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();
var answer = {
'convert': value,
'from': from,
'to': to,
'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;
}
res.writeHead(200, {
'Content-Type': 'application/javascript'
});
res.end(JSON.stringify(answer));
});
};

+ 1
- 1
package.json View File

@ -1,6 +1,6 @@
{
"name": "brett-is-api",
"version": "0.1.0",
"version": "0.1.1",
"description": "",
"main": "lib/index.js",
"scripts": {


Loading…
Cancel
Save