Giter Club home page Giter Club logo

active_nutrition's Introduction

active_nutrition

ActiveNutrition is an ActiveRecord-backed collection of models for storing and retrieving nutritional information from the USDA's Nutrient Database.

Installation

gem install active_nutrition

Usage

ActiveNutrition is meant to be used within a Rails application, but it should work within any Ruby project that uses ActiveRecord 3.0 or greater.

require 'active_nutrition'

In a (possibly Rails) gemfile:

gem 'active_nutrition', '~> 0.5.0'

Getting the Data

ActiveNutrition has the ability to fetch the current Nutrient Database files from the USDA's website and load them into your ActiveRecord-supported database. As long as you've established a connection via ActiveRecord, you should be able to import the nutrient data.

With Rails:

bundle exec rake active_nutrition:migrate active_nutrition:rebuild

Without Rails:

require 'active_nutrition'

ActiveNutrition.migrate
ActiveNutrition.rebuild

Note: The full data import could take anywhere from 30 minutes to a few hours depending on how powerful your computer is. Note: You can use an instead of active_nutrition for all rake tasks, eg. rake an:rebuild.

Searching

ActiveNutrition provides a simple #search method that executes a SQL LIKE statement and returns an array of Food objects:

ActiveNutrition.search("olive oil")

Food Objects

Each Food object offers these methods

  1. #nutrition_facts: Returns an array of NutritionFact objects for each kind of nutrient associated with this food. Examples include the amount of protein, fat, and sodium. Each NutritionFact object knows its unit (eg. miligrams, grams, etc) and amount per 1 gram of the food.
  2. #weights: Returns an array of common unit weights for this food (eg. cups, tablespoons, etc) and their gram equivalents.
  3. #food_group: Returns an object that specifies the name and code of the food's food group (eg. Breads, Vegetables, etc).

NutritionFact Objects

Each NutritionFact object has these important methods:

  1. #value: How much of this nutrient is present in 1 gram of the food.
  2. #units: The unit associated with #value.
  3. #description: A textual description of the nutrient.
ActiveNutrition.search("olive oil").first.nutrition_facts

Calling #nutrition_facts on a Food object actually returns an instance of NutritionFacts, which provides a handy #to_hash convenience method to hash nutrient amounts by description:

# { "Protein" => 0.0, "Total lipid (fat)" => 100.0, ... }
ActiveNutrition.search("olive oil").first.nutrition_facts.to_hash

The #to_hash method is capable of hashing nutrient amounts by any method supported by NutritionFact via the :by option:

# { 203 => 0.0, 204 => 100.0, 205 => 0.0, 207 => 0.0, ... }
ActiveNutrition.search("olive oil").first.nutrition_facts.to_hash(:by => :nutrition_number)

Weight Objects

Each Weight object offers these methods:

  1. #amount: The amount associated with #measurement, eg. 1, 0.5, etc.
  2. #measurement: The unit associated with #amount, eg. cup, tablespoon, gallon, dash, etc.
  3. #grams: Mass in grams.

Calling #weights on a Food object actually returns an instance of Weights, which provides a handy #to_hash convenience method to hash weights (grams) by measurement:

# { "tablespoon" => 13.5, "cup" => 216.0, "tsp" => 4.5 }
ActiveNutrition.search("olive oil").first.weights.to_hash

Requirements

No external requirements.

Running Tests

No test suite exists for this gem yet - coming soon!

Authors

Cameron C. Dutro: http://github.com/camertron

Links

USDA Nutrition Database: http://www.ars.usda.gov/Services/docs.htm?docid=8964

License

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

Future Plans

  • Ability to update your local database with rake active_nutrition:update.
  • Test suite.

active_nutrition's People

Contributors

camertron avatar beghbali avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.