class HealthyHungerApi::Meals

Attributes

day[RW]
id[RW]
readyInMinutes[RW]
servings[RW]
slot[RW]
sumnary[RW]
title[RW]
value[RW]

Public Class Methods

all() click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 8
def self.all #class method that is used as a getter for the class array @@all

    @@all ||= load_meals #loads the meal objects into the @@all array if they don't already exist

end
create_meal_from_specifics(meals) click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 34
def self.create_meal_from_specifics(meals) #class method that is used to create new instances by iterating over the meal hash
    meals.map do |meal_hash|
        self.new(meal_hash)
    end

end
find_by_number(choice) click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 14
def self.find_by_number(choice) #class method that takes in the choice of the user and then finds
    #the meal object associated with it within the all array
    self.all[choice.to_i - 1]
end
load_meals(time, cal, diet, allergy) click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 19
def self.load_meals(time, cal, diet, allergy) #class method that passes input from the user into the class methods
    #from the API class based on a conditional of whether the user has chosen day or week for time. It then assigns
    #the return value of those class methods to an instance variable @meals which is passed to a class method within
    #the meals class to create a new instance for each meal and assign the instances to the @@all array

    if time == 'day'
        @meals = API.get_meals(time, cal, diet, allergy)["meals"] #gets the meals from the API class using a the hash symbol
    elsif time == 'week'                                           #necessary to get the appropriate data for daily meals
        @meals = API.get_meals(time, cal, diet, allergy)["items"]
    end
    
    @@all = self.create_meal_from_specifics(@meals) #passing the return values to create new instances of meals
end
new(attributes = {}) click to toggle source

hash key received from the api meal hash

# File lib/Healthy_hunger_api/meals.rb, line 44
def initialize(attributes = {}) #initializes new instances of each meal
    attributes.each do |attribute_name, attribute_value| #iterates over each has assigning each key as an attribute name
        if self.respond_to?("#{attribute_name}=")       #and each key value as the attribute value using the send method
            self.send("#{attribute_name}=", attribute_value)
        end
    end
end

Public Instance Methods

daily_summaries() click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 60
def daily_summaries #instance method that uses the id key from the return value of the class method
                    #get_summary of the API class to get the summary of each meal for a daily meal plan
    
    @summaries ||= API.get_summary(self.id) 
    
end
display_meals_by_day() click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 74
def display_meals_by_day #instance method that uses the "summary" string to access the value from that hash symbol
                        #and replace each of the symbols found through the regex passed into the .gsub method
                        #with an empty space

    puts daily_summaries["summary"].gsub(/<\/?[a-z]+>/, "").gsub(/<a href="[a-zA-Z0-9\/:\.\-\+]+">/, "")   

end
display_meals_by_week() click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 82
def display_meals_by_week#instance method that uses the "summary" string to access the value from that hash symbol
    #and replace each of the symbols found through the regex passed into the .gsub method
    #with an empty space

    puts weekly_summaries["summary"].gsub(/<\/?[a-z]+>/, "").gsub(/<a href="[a-zA-Z0-9\/:\.\-\+]+">/, "")

end
value_parser() click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 53
def value_parser #instance method used to parse through the class method get_summary from the API classes
                #value key and grab the values that are within the "id" symbol
    API.get_summary(JSON.parse(self.value)["id"])

end
weekly_summaries() click to toggle source
# File lib/Healthy_hunger_api/meals.rb, line 67
def weekly_summaries#instance method that calls the value_parser method to get the summary of each meal for a weekly meal plan

        @summaries ||= value_parser

end