Skip to content

Commit

Permalink
added new function: getFormattedCommodities
Browse files Browse the repository at this point in the history
  • Loading branch information
EtoileScintillante committed Nov 12, 2023
1 parent 42b9efe commit ebd36ea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,49 @@ std::string getFormattedMajorIndices(const std::string &pathToJson, const std::s
}

return getFormattedPrices(indicesSymbols, indicesNames, indicesDescriptions, markdown);
}

std::string getFormattedCommodities(const std::string &pathToJson, bool markdown)
{
// Load JSON data from a file
std::ifstream file(pathToJson);
if (!file.is_open()) {
return "Error: Unable to open JSON file.";
}

// Read JSON data from the file
std::string jsonData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();

rapidjson::Document document;
document.Parse(jsonData.c_str());

if (!document.IsObject())
{
return "Error: Invalid JSON data.";
}

rapidjson::Value::ConstMemberIterator commodities = document.FindMember("commodities");
if (commodities == document.MemberEnd() || !commodities->value.IsArray())
{
return "Error: commodities data not found.";
}

const rapidjson::Value &commoditiesData = commodities->value;

std::vector<std::string> symbols;
std::vector<std::string> names;

// Put data in vectors
for (rapidjson::SizeType i = 0; i < commoditiesData.Size(); ++i)
{
const rapidjson::Value &comData = commoditiesData[i];
if (comData.HasMember("symbol") && comData.HasMember("name"))
{
symbols.push_back(comData["symbol"].GetString());
names.push_back(comData["name"].GetString());
}
}

return getFormattedPrices(symbols, names, {}, markdown);
}
8 changes: 8 additions & 0 deletions src/include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,12 @@ std::string getFormattedPrices(std::vector<std::string> indicesSymbols, std::vec
/// @return A string with the formatted price data and descriptions.
std::string getFormattedMajorIndices(const std::string &pathToJson, const std::string &region, bool description, bool markdown = false);

/// This function reads JSON data containing names and symbols of different commodities
/// from a JSON file. It extracts the symbols and names and formats the data
/// using Markdown syntax if requested. If an error occurs, it will return an error message.
/// @param pathToJson Path to the JSON file.
/// @param markdown When set to true, the formatted string will contain Markdown syntax to make it more visually appealing.
/// @return A string with the formatted price data.
std::string getFormattedCommodities(const std::string &pathToJson, bool markdown = false);

#endif // DATA_H

0 comments on commit ebd36ea

Please sign in to comment.