在我的rails后端使用外部api时遇到问题



因此,对于初学者来说,这是我使用的api

https://fdc.nal.usda.gov/api-guide.html

我的目标是提取信息用于健康应用程序,这样用户就可以跟踪他们的食物消费。我是这方面的新手,在将数据拉入后端时遇到了问题。这是我的种子.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,

您应该使用HTTParty-gem进行外部api调用。这是宝石的链接https://github.com/jnunemaker/httparty

这是你需要的想法

class FetchAPI
include HTTParty
base_uri "https://api.nal.usda.gov"
def self.list
params = {
dataType: "Branded",
pageSize: 100,
sortBy: "asc"
}
response = self.class.get("/fdc/v1/foods/list", { headers: "Bearer #{ENV['API_KEY']}"}, query: params)
response.parse
end
end
FetchAPI.list["foods"].each do |food|
Food.get_foods_list(food)
end

最新更新