Skip to content

Commit

Permalink
made getFormattedJSON more generalizable
Browse files Browse the repository at this point in the history
  • Loading branch information
EtoileScintillante committed Nov 16, 2023
1 parent 4920706 commit 24c1d47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 38 deletions.
34 changes: 4 additions & 30 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ std::string getFormattedPrices(std::vector<std::string> indicesSymbols, std::vec
return formattedString.str();
}

std::string getFormattedJSON(const std::string &pathToJson, bool markdown, bool description, const std::string &region)
std::string getFormattedJSON(const std::string &pathToJson, const std::string &key, bool markdown, bool description)
{
// Load JSON data from a file
std::ifstream file(pathToJson);
Expand All @@ -531,36 +531,10 @@ std::string getFormattedJSON(const std::string &pathToJson, bool markdown, bool
return "Error: Invalid JSON data.";
}

rapidjson::Value::ConstMemberIterator dataIt;

// Indices data
if (region != "")
rapidjson::Value::ConstMemberIterator dataIt = document.FindMember(key.c_str());
if (dataIt == document.MemberEnd() || !dataIt->value.IsArray())
{
dataIt = document.FindMember(region.c_str());
if (dataIt == document.MemberEnd() || !dataIt->value.IsArray())
{
return "Error: Invalid region or region data.";
}
}

// Commodities data
if (document.HasMember("commodities"))
{
dataIt = document.FindMember("commodities");
if (dataIt == document.MemberEnd() || !dataIt->value.IsArray())
{
return "Error: Invalid commodities data.";
}
}

// Currency data
if (document.HasMember("currencies"))
{
dataIt = document.FindMember("currencies");
if (dataIt == document.MemberEnd() || !dataIt->value.IsArray())
{
return "Error: Invalid currency data.";
}
return "Error: Invalid key.";
}

const rapidjson::Value &data = dataIt->value;
Expand Down
15 changes: 7 additions & 8 deletions src/include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,15 @@ std::string getFormattedStockMetrics(const std::string &symbol, bool markdown =
std::string getFormattedPrices(std::vector<std::string> indicesSymbols, std::vector<std::string> indicesNames = {},
std::vector<std::string> indicesDescriptions = {}, bool markdown = false);

/// This function reads JSON data containing symbols, names and optionally descriptions of market related
/// elements. It currently supports data about major indices (note: a region must be provided), commodities and currencies.
/// It extracts the symbols and names (and descriptions) and formats the data using Markdown syntax if requested.
/// See the folder "data" for examples of JSON files that work with this function.
/// This function reads JSON data containing symbols, names and optionally descriptions of things related to
/// financial markets. It extracts the symbols and names (and descriptions) and formats the data using
/// Markdown syntax if requested. See the folder "data" for examples of JSON files that work with this function.
/// If an error occurs, it will return an error message.
/// @param pathToJson Path to the JSON file. Must contain one of the following members: "indices", "commodities", "currencies".
/// @param pathToJson Path to the JSON file.
/// @param key Key of data in JSON file that needs to extracted (e.g. "commodities", "currencies", "Automotive").
/// @param markdown When set to true, the formatted string will contain Markdown syntax to make it more visually appealing.
/// @param description When set to true, the descriptions provided in the JSON file will also be added to the formatted string.
/// @param region The region for which to retrieve and format major indices data. Allowed values: "US", "EU", "Asia".
/// @param description When set to true, the descriptions provided in the JSON file will be added to the formatted string.
/// @return A string with the formatted price data (and optionally descriptions).
std::string getFormattedJSON(const std::string &pathToJson, bool markdown = false, bool description = false, const std::string &region = "");
std::string getFormattedJSON(const std::string &pathToJson, const std::string &key, bool markdown = false, bool description = false);

#endif // DATA_H

0 comments on commit 24c1d47

Please sign in to comment.