From 24c1d47652879cbfbf06b7cecef8b76b65534ee1 Mon Sep 17 00:00:00 2001 From: Esma Date: Thu, 16 Nov 2023 21:33:06 +0100 Subject: [PATCH] made getFormattedJSON more generalizable --- src/data.cpp | 34 ++++------------------------------ src/include/data.h | 15 +++++++-------- 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/src/data.cpp b/src/data.cpp index 55af6cb..ea05e9a 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -510,7 +510,7 @@ std::string getFormattedPrices(std::vector indicesSymbols, std::vec return formattedString.str(); } -std::string getFormattedJSON(const std::string &pathToJson, bool markdown, bool description, const std::string ®ion) +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); @@ -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; diff --git a/src/include/data.h b/src/include/data.h index 3a01be4..6b85f6f 100644 --- a/src/include/data.h +++ b/src/include/data.h @@ -114,16 +114,15 @@ std::string getFormattedStockMetrics(const std::string &symbol, bool markdown = std::string getFormattedPrices(std::vector indicesSymbols, std::vector indicesNames = {}, std::vector 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 ®ion = ""); +std::string getFormattedJSON(const std::string &pathToJson, const std::string &key, bool markdown = false, bool description = false); #endif // DATA_H \ No newline at end of file