Skip to content
API for researching nutritional information of various foods
Latest commit 0ccb9cc Dec 5, 2013 Kali Donovan Misc bugfixes
Failed to load latest commit information.
lib Misc bugfixes Dec 5, 2013
food_info.gemspec Misc bugfixes Dec 5, 2013



FoodInfo is a ruby gem that retrieves food nutrition information from various online data sources.


FoodInfo is available as a gem, so installation is as simple as:

gem install food_info

Supported Data Sources

There's currently only one adapter implemented, which pulls data from FatSecret's REST API. The code's modular and adding additional data sources should be fairly straightforward, but since DailyBurn discontinued their API access I don't know of any other solid sources (if you do, though, please let me know and/or add an adapter!).



To use the FatSecret API (currently your only option), you'll first need to sign up for a free developer account and retrieve the "REST API Consumer Key" and "REST API Consumer Secret" from your "My Account" tab.

Once that's done, the first step is to tell FoodInfo which adapter you want to use and what authorization to send.

FoodInfo.establish_connection(:fat_secret, :key => 'YOUR-KEY', :secret => 'YOUR-KEY')


To cache results, FoodInfo supports passing in an instance of a memcache-API-compatible (i.e. responds to get and set) caching object. I recommend using the Dalli gem.

require 'dalli'
client ='localhost:11211')
FoodInfo.establish_connection(:fat_secret, :key => 'YOUR-KEY', :secret => 'YOUR-KEY', :cache => client)

With that in place repeated search or details requests will pull from the cache, and not the API endpoint.


Now we can search for foods.

cheese ='cheese')
cheese.total_results    # => 2469
cheese.per_page         # => 20             # => 1
cheese.results          # => ... big array ...
cheese.results.first    # => 
# {
#     "description" => "Per 100g - Calories: 403kcal | Fat: 33.14g | Carbs: 1.28g | Protein: 24.90g",
#              "id" => "33689",
#            "kind" => "Generic",
#            "name" => "Cheddar Cheese",
#             "url" => ""
# }

(As an aside, I get that pretty, nicely-lined-up console formatting from the remarkably awesome AwesomePrint Gem).


Search supports pagination via the page and per_page (max 50) parameters:'cheese', :page => 2, :per_page => 50)

Access to search results

You can access the results of the search explicitly:'cheese').results # => ... lots of results ...

But the SearchResults class includes Enumerable, so you can also just call enumerable methods on it directly:'cheese').map(&:name) # => array of names of matching foods

Nutritional Details

Once you have a specific food item in mind from the search results, you can retrieve a whole lot of additional information.

cheddar ='cheese').first
info = FoodInfo.details( ) # => ... a whole lotta data ...

General metadata about the cheese includes id, name, kind, and url, which are identical to what you'd get from the search method. It also has one or more servings, however, and this is where we finally get our nutrition info.

serving = info.servings.first # =>
# {
#                     "calcium" => 95,
#                    "calories" => 532.0,
#                "carbohydrate" => 1.69,
#                 "cholesterol" => 139.0,
#                         "fat" => 43.74,
#                       "fiber" => 0.0,
#                          "id" => "29131",
#                        "iron" => 5,
#     "measurement_description" => "cup, diced",
#       "metric_serving_amount" => 132.0,
#         "metric_serving_unit" => "g",
#         "monounsaturated_fat" => 12.396,
#             "number_of_units" => 1.0,
#         "polyunsaturated_fat" => 1.243,
#                   "potassium" => 129.0,
#                     "protein" => 32.87,
#               "saturated_fat" => 27.841,
#         "serving_description" => "1 cup diced",
#                      "sodium" => 820.0,
#                       "sugar" => 0.69,
#                   "trans_fat" => 0.0,
#                         "url" => "",
#                   "vitamin_a" => 26,
#                   "vitamin_c" => 0
# }

For full details on what each of those fields contains, check the FatSecret documentation.

Legal Note

The FatSecret TOS requires you not to store, well, pretty much anything aside from food or serving IDs for more than 24 hours. This is annoying, but I figured I'd give you a heads up.

Note on Patches/Pull Requests

Contributions are welcome, particularly adding adapters for additional data sources.

As always, the process is to fork this project on Github, make your changes (preferably in a topic branch, and without changing the gem version), send a pull request, and then receive much appreciation!


Copyright © 2011 Deviantech, Inc. and released under the MIT license.

Something went wrong with that request. Please try again.