Back to Parent

def get_nutrients body
  url = 'https://trackapi.nutritionix.com/v2/natural/nutrients'
  puts body
  res = HTTParty.post url, body: { query:body, timezone: "US/Eastern"}.to_json, headers: {'content-type' => 'application/json', 'x-app-id' => ENV['NUTRITIONIX_ID'], 'x-app-key' => ENV['NUTRITIONIX_KEY']}

  total_fat = 0
  total_protein = 0
  total_cal = 0
  if res['foods'].nil?
    msg = "I can't seem to recognize any food in what you said. Can you be more specific please?"
    session[:last_msg] = "further_log_meal"
    return msg
  else
    res['foods'].each do |food|
      total_fat += food['nf_total_fat']
      total_protein += food['nf_protein']
      total_cal += food['nf_calories']
    end
    #save changes to json file
    # update_log total_cal, total_protein, total_fat

    total_fat = total_fat.to_i
    total_protein = total_protein.to_i
    total_cal = total_cal.to_i

    msg = "You consumed approximately #{total_cal} calories."
    return msg, total_cal, total_protein, total_fat
  end
end
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0