Class: NutritionFacts::Item
- Inherits:
-
Object
- Object
- NutritionFacts::Item
- Defined in:
- lib/nutrition_facts/item.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#hits ⇒ Object
Returns the value of attribute hits.
-
#item_name ⇒ Object
Returns the value of attribute item_name.
-
#max_score ⇒ Object
Returns the value of attribute max_score.
-
#nf_calcium_dv ⇒ Object
Returns the value of attribute nf_calcium_dv.
-
#nf_calories ⇒ Object
Returns the value of attribute nf_calories.
-
#nf_calories_from_fat ⇒ Object
Returns the value of attribute nf_calories_from_fat.
-
#nf_cholesterol ⇒ Object
Returns the value of attribute nf_cholesterol.
-
#nf_dietary_fiber ⇒ Object
Returns the value of attribute nf_dietary_fiber.
-
#nf_iron_dv ⇒ Object
Returns the value of attribute nf_iron_dv.
-
#nf_protein ⇒ Object
Returns the value of attribute nf_protein.
-
#nf_saturated_fat ⇒ Object
Returns the value of attribute nf_saturated_fat.
-
#nf_serving_size_qty ⇒ Object
Returns the value of attribute nf_serving_size_qty.
-
#nf_serving_size_unit ⇒ Object
Returns the value of attribute nf_serving_size_unit.
-
#nf_serving_weight_grams ⇒ Object
Returns the value of attribute nf_serving_weight_grams.
-
#nf_sodium ⇒ Object
Returns the value of attribute nf_sodium.
-
#nf_sugars ⇒ Object
Returns the value of attribute nf_sugars.
-
#nf_total_carbohydrate ⇒ Object
Returns the value of attribute nf_total_carbohydrate.
-
#nf_total_fat ⇒ Object
Returns the value of attribute nf_total_fat.
-
#nf_vitamin_a_dv ⇒ Object
Returns the value of attribute nf_vitamin_a_dv.
-
#nf_vitamin_c_dv ⇒ Object
Returns the value of attribute nf_vitamin_c_dv.
-
#total_hits ⇒ Object
Returns the value of attribute total_hits.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(item_list = {}) ⇒ Item
constructor
A new instance of Item.
Constructor Details
#initialize(item_list = {}) ⇒ Item
Returns a new instance of Item.
13 14 15 16 17 |
# File 'lib/nutrition_facts/item.rb', line 13 def initialize(item_list = {}) item_list.each do |key, value| send("#{key}=", value) end end |
Instance Attribute Details
#hits ⇒ Object
Returns the value of attribute hits.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def hits @hits end |
#item_name ⇒ Object
Returns the value of attribute item_name.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def item_name @item_name end |
#max_score ⇒ Object
Returns the value of attribute max_score.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def max_score @max_score end |
#nf_calcium_dv ⇒ Object
Returns the value of attribute nf_calcium_dv.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_calcium_dv @nf_calcium_dv end |
#nf_calories ⇒ Object
Returns the value of attribute nf_calories.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_calories @nf_calories end |
#nf_calories_from_fat ⇒ Object
Returns the value of attribute nf_calories_from_fat.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_calories_from_fat @nf_calories_from_fat end |
#nf_cholesterol ⇒ Object
Returns the value of attribute nf_cholesterol.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_cholesterol @nf_cholesterol end |
#nf_dietary_fiber ⇒ Object
Returns the value of attribute nf_dietary_fiber.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_dietary_fiber @nf_dietary_fiber end |
#nf_iron_dv ⇒ Object
Returns the value of attribute nf_iron_dv.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_iron_dv @nf_iron_dv end |
#nf_protein ⇒ Object
Returns the value of attribute nf_protein.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_protein @nf_protein end |
#nf_saturated_fat ⇒ Object
Returns the value of attribute nf_saturated_fat.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_saturated_fat @nf_saturated_fat end |
#nf_serving_size_qty ⇒ Object
Returns the value of attribute nf_serving_size_qty.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_serving_size_qty @nf_serving_size_qty end |
#nf_serving_size_unit ⇒ Object
Returns the value of attribute nf_serving_size_unit.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_serving_size_unit @nf_serving_size_unit end |
#nf_serving_weight_grams ⇒ Object
Returns the value of attribute nf_serving_weight_grams.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_serving_weight_grams @nf_serving_weight_grams end |
#nf_sodium ⇒ Object
Returns the value of attribute nf_sodium.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_sodium @nf_sodium end |
#nf_sugars ⇒ Object
Returns the value of attribute nf_sugars.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_sugars @nf_sugars end |
#nf_total_carbohydrate ⇒ Object
Returns the value of attribute nf_total_carbohydrate.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_total_carbohydrate @nf_total_carbohydrate end |
#nf_total_fat ⇒ Object
Returns the value of attribute nf_total_fat.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_total_fat @nf_total_fat end |
#nf_vitamin_a_dv ⇒ Object
Returns the value of attribute nf_vitamin_a_dv.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_vitamin_a_dv @nf_vitamin_a_dv end |
#nf_vitamin_c_dv ⇒ Object
Returns the value of attribute nf_vitamin_c_dv.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def nf_vitamin_c_dv @nf_vitamin_c_dv end |
#total_hits ⇒ Object
Returns the value of attribute total_hits.
2 3 4 |
# File 'lib/nutrition_facts/item.rb', line 2 def total_hits @total_hits end |
Class Method Details
.all ⇒ Object
39 40 41 |
# File 'lib/nutrition_facts/item.rb', line 39 def self.all @@all end |
.find_by_name(name) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/nutrition_facts/item.rb', line 19 def self.find_by_name(name) uri = URI.parse("https://api.nutritionix.com/v1_1/search/#{name}?results=0%3A9&cal_min=0&cal_max=50000&fields=item_name%2Cnf_calories%2Cnf_total_fat%2Cnf_total_carbohydrate%2Cnf_dietary_fiber%2Cnf_sugars%2Cnf_protein%2Cnf_serving_weight_grams%2Cnf_cholesterol%2Cnf_saturated_fat%2Cnf_calories_from_fat%2Cnf_sodium%2Cnf_calcium_dv%2Cnf_iron_dv%2Cnf_vitamin_a_dv%2Cnf_vitamin_c_dv%2Cnf_serving_size_qty%2Cnf_serving_size_unit&appId=da276553&appKey=f501b79647768ba684af20428a44ef59") response = Net::HTTP.get_response(uri) raw_data = JSON.parse(response.body) if raw_data['hits'].empty? puts '' else i = 0 num = raw_data['hits'].count while i < num @@all << NutritionFacts::Item.new(raw_data['hits'][i]['fields']) i += 1 end end @@all end |
.reset ⇒ Object
43 44 45 |
# File 'lib/nutrition_facts/item.rb', line 43 def self.reset @@all.clear end |