Итак, для начала, это API, который я использую
https://fdc.nal.usda.gov/api-guide.html
Моя цель - получить информацию для использования в приложении для здоровья, чтобы пользователи могут отслеживать их потребление пищи. Я новичок в этом, и у меня проблемы с загрузкой данных в мой сервер. это мой файл seed.rb
API_HOST = "https://api.nal.usda.gov/"
LIST_PATH = "fdc/v1/foods/list"
def self.list
url = "#{API_HOST}#{LIST_PATH}"
params = {
dataType: "Branded",
pageSize: 100,
sortBy: "asc"
}
response = HTTP.auth("Bearer #{ENV['API_KEY']}").get(url, params: params)
response.parse
end
self.list["foods"].each do |food|
Food.get_foods_list(food)
end
В моей модели для еды:
class Food < ApplicationRecord
def self.get_foods_list(food)
self.create(
name: food["description"] ,
protein: food["foodNutrients"]["nutrientName"]["protein"]["value"],
carbohydrates: food["foodNutrients"]["nutrientName"]["carbohydrate, by difference"]["value"],
fats: food["foodNutrients"]["nutrientName"]["Total lipid (fat)"]["value"]
)
end
end
Я получаю эту ошибку:
NameError: uninitialized constant HTTP
при попытке засеять мою базу данных данными API.
Я чувствую, что упускаю что-то маленькое, но я не знаю, что это такое.
Это пример данных, которые я пытаюсь извлечь.
foods: [
{
fdcId: 470794,
description: "CHEDDAR CHEESE",
dataType: "Branded",
gtinUpc: "025555300085",
publishedDate: "2019-04-01",
brandOwner: "Coblentz Distributing, Inc.",
ingredients: "PASTEURIZED MILK, CHEESE CULTURE, SALT, ENZYMES AND ANNATTO (VEGETABLE COLOR).",
foodNutrients: [
{
nutrientId: 1005,
nutrientName: "Carbohydrate, by difference",
nutrientNumber: "205",
unitName: "G",
derivationCode: "LCCD",
derivationDescription: "Calculated from a daily value percentage per serving size measure",
value: 0,
},
{
nutrientId: 1079,
nutrientName: "Fiber, total dietary",
nutrientNumber: "291",
unitName: "G",
derivationCode: "LCCD",
derivationDescription: "Calculated from a daily value percentage per serving size measure",
value: 0,
},
{
nutrientId: 1087,
nutrientName: "Calcium, Ca",
nutrientNumber: "301",
unitName: "MG",
derivationCode: "LCCD",
derivationDescription: "Calculated from a daily value percentage per serving size measure",
value: 735,
},
{
nutrientId: 1089,
nutrientName: "Iron, Fe",
nutrientNumber: "303",
unitName: "MG",
derivationCode: "LCCD",
derivationDescription: "Calculated from a daily value percentage per serving size measure",
value: 0,
},
{
nutrientId: 1104,
nutrientName: "Vitamin A, IU",
nutrientNumber: "318",
unitName: "IU",
derivationCode: "LCCD",
derivationDescription: "Calculated from a daily value percentage per serving size measure",
value: 882,
},
{
nutrientId: 1162,
nutrientName: "Vitamin C, total ascorbic acid",
nutrientNumber: "401",
unitName: "MG",
derivationCode: "LCCD",
derivationDescription: "Calculated from a daily value percentage per serving size measure",
value: 0,
},
{
nutrientId: 1003,
nutrientName: "Protein",
nutrientNumber: "203",
unitName: "G",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 23.53,
},
{
nutrientId: 1004,
nutrientName: "Total lipid (fat)",
nutrientNumber: "204",
unitName: "G",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 32.35,
},
{
nutrientId: 1008,
nutrientName: "Energy",
nutrientNumber: "208",
unitName: "KCAL",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 412,
},
{
nutrientId: 2000,
nutrientName: "Sugars, total including NLEA",
nutrientNumber: "269",
unitName: "G",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 0,
},
{
nutrientId: 1093,
nutrientName: "Sodium, Na",
nutrientNumber: "307",
unitName: "MG",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 618,
},
{
nutrientId: 1253,
nutrientName: "Cholesterol",
nutrientNumber: "601",
unitName: "MG",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 103,
},
{
nutrientId: 1257,
nutrientName: "Fatty acids, total trans",
nutrientNumber: "605",
unitName: "G",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 0,
},
{
nutrientId: 1258,
nutrientName: "Fatty acids, total saturated",
nutrientNumber: "606",
unitName: "G",
derivationCode: "LCCS",
derivationDescription: "Calculated from value per serving size measure",
value: 20.59,
},
],
allHighlightFields: "<b>Ingredients</b>: PASTEURIZED MILK, <em>CHEESE</em> CULTURE, SALT, ENZYMES AND ANNATTO (VEGETABLE COLOR).",
score: 542.84247,