Class: FoodNutritionix::Food

Inherits:
Object
  • Object
show all
Defined in:
lib/foodnutritionix/food.rb

Constant Summary collapse

ATTRIBUTES =
[:name,
:serving_quantity,
:serving_unit,
:serving_weight,
:calories,
:fat,
:saturated_fat,
:cholesterol,
:sodium,
:carbohydrate,
:dietary_fiber,
:sugars,
:protein,
:potassium,
:photo].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Food

Returns a new instance of Food.



21
22
23
24
25
26
# File 'lib/foodnutritionix/food.rb', line 21

def initialize(data)
  @name = data['food_name']
  populate_serving_information(data)
  populate_nutrition_facts(data)
  @photo = data['photo']
end

Class Method Details

.search(*names) ⇒ Object



33
34
35
36
# File 'lib/foodnutritionix/food.rb', line 33

def self.search(*names)
  FoodNutrixClient.search_foods(names)
                 &.map { |food| new(food) }
end

Instance Method Details

#to_hashObject Also known as: to_h



28
29
30
# File 'lib/foodnutritionix/food.rb', line 28

def to_hash
  ATTRIBUTES.map { |attribute| [attribute, send(attribute)] }.to_h
end