Skip to content

Commit

Permalink
params ok
Browse files Browse the repository at this point in the history
  • Loading branch information
SermetPekin committed Nov 5, 2024
1 parent a0042ed commit 20808c5
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 58 deletions.
4 changes: 2 additions & 2 deletions include/get.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::enable_if_t<is_cstring<T>::value, const char *> get_proxy_for_url(T url, T
return "";
}

std::string get_request_real(const GetParams &params , bool test );
std::string get_request_real(const GetParams &params, bool test);

std::string get_request(const GetParams &params, const Config &config)
{
Expand Down Expand Up @@ -124,7 +124,7 @@ std::string get_request_real(const GetParams &params, bool test)
{
// if (config.test)

if (!test & !evds::confirm("Request?"))
if (!test & !evds::confirm("Request?", params.url))
{
std::cout << "Not requesting ...";
throw std::runtime_error("Request was cancelled ");
Expand Down
8 changes: 6 additions & 2 deletions include/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ namespace evds
std::cout << std::endl;
}

// struct GetParams;
// Utility function for printing a single value
template <typename T>
void prints(const T &x)
{
std::cout << x;
}

bool confirm(const std::string &message)
{
bool confirm(const std::string &message, const std::string &url)
{
std::string input;

std::cout << "[" << url << "]";
std::cout << message << " (y/n): ";

std::cin >> input;

return input == "y" || input == "Y";
Expand Down
48 changes: 45 additions & 3 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include <iostream>
#include <vector>
#include <string>
#include <string>
#include <vector>
#include <sstream>
#include <string>
#include <vector>
#include <regex>
#include <iterator>

#pragma once

Expand All @@ -13,6 +20,39 @@ namespace evds

static const std::string domain("https://evds2.tcmb.gov.tr/");

std::vector<std::string> splitString(const std::string &str, const std::string &delimiters)
{
// a regex pattern for the delimiters (e.g., "[,; ]" to split on comma, semicolon, or space)
std::regex re("[" + delimiters + "]");

// Use regex_token_iterator to split based on the pattern
std::vector<std::string> result{
std::sregex_token_iterator(str.begin(), str.end(), re, -1),
std::sregex_token_iterator()};

// Remove any empty strings resulting from consecutive delimiters
result.erase(std::remove_if(result.begin(), result.end(),
[](const std::string &s)
{ return s.empty(); }),
result.end());

return result;
}

std::vector<std::string> splitString(const std::string &str, char delimiter)
{
std::vector<std::string> result;
std::stringstream ss(str);
std::string item;

while (std::getline(ss, item, delimiter))
{
result.push_back(item);
}

return result;
}

// .................................................................................... join

std::string join(const std::vector<std::string> &elements, const std::string &delimiter)
Expand All @@ -37,12 +77,14 @@ namespace evds
// .................................................................................... Config
struct Config
{

std::vector<std::string> indexes;
std::string start_date = "01-01-2000";
std::string end_date = "31-12-2100";
std::string frequency = "daily";
std::string aggregation = "none";
// std::string frequency = "daily";
// std::string aggregation = "none";
bool test = false;
std::string frequency = "default"; // | monthly | weekly | annually | semimonthly | semiannually | business
std::string frequency = "default"; // daily | monthly | weekly | annually | semimonthly | semiannually | business
std::string formulas = "default"; // | level | percentage_change | difference | year_to_year_percent_change | year_to_year_differences |
std::string aggregation = "default"; // | avg |min | max | first | last | sum
bool cache = true;
Expand Down
127 changes: 124 additions & 3 deletions include/url_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <iterator>
#include <set>
#include <cassert>
#include <iostream>
#include <unordered_map>
#include <string>
#include "index.h"

namespace evds
Expand Down Expand Up @@ -44,14 +47,133 @@ namespace evds
return *this;
}

std::string convert_series() const
{

auto s = evds::splitString(series_, ",-");
return evds::join(s, "-");
}
std::string multiply(const std::string &val) const
{

std::vector<std::string> v;

for (const auto &a : evds::splitString(series_, ",-")) // config_.indexes
{
v.push_back(val);
}

std::string str = evds::join(v, "-");

return str;
}

std::string aggregation_url() const
{
if (config_.frequency == "default")
{
return "";
}

if (config_.aggregation == "default")
{
auto m_ag = multiply("avg");
m_ag = "&aggregationTypes=" + m_ag;
return m_ag;
}
std::string res("&aggregationTypes=");
res = res + config_.aggregation;

return res;
}

std::string formulas_url() const
{

static const std::unordered_map<std::string, std::string> formulasDict = {
{"level", "0"},

{"percentage_change", "1"},
{"pc", "1"},

{"difference", "2"},
{"d", "2"},

{"yoy", "3"},
{"yoy_p", "3"},
{"yoy_pc", "3"},
{"yoy_percent", "3"},

{"yoy_diff", "4"},
{"yoy_d", "4"},

{"pc_end", "5"},

{"dif_end", "6"},
{"mov_ave", "7"},
{"ma", "7"},
{"mov_sum", "8"},
{"ms", "8"},
};

auto it = formulasDict.find(config_.formulas);
if (it != formulasDict.end())
{

std::string m_ag = multiply(it->second);
m_ag = "&formulas=" + m_ag;
return m_ag;

// return "&formulas=" + it->second;
}
else
{
return "";
}
}

std::string
frequency_url() const
{
// frequency=5&aggregationTypes=avg-avg-avg-avg-avg-avg-avg-avg&formulas=0-0-0-0-0-0-0-0

static const std::unordered_map<std::string, std::string> freqDict = {
{"daily", "1"},
{"business", "2"},
{"weekly", "3"},
{"semimonthly", "4"},
{"monthly", "5"},
{"quarterly", "6"},
{"semiannually", "7"},
{"annual", "8"},
{"annually", "8"}};

auto it = freqDict.find(config_.frequency);
if (it != freqDict.end())
{

return "&frequency=" + it->second;
}
else
{
return "";
}
}

// final URL
std::string build() const
std::string
build() const
{
std::ostringstream oss;
oss << domain << "service/evds/series=" << series_;
oss << domain << "service/evds/series=" << convert_series();
oss << "&startDate=" << config_.start_date;
oss << "&endDate=" << config_.end_date;
oss << frequency_url();
oss << aggregation_url();
oss << formulas_url();

oss << "&type=json";

return oss.str();
}

Expand All @@ -73,5 +195,4 @@ namespace evds
std::string series_;
Index index_v;
};

}
73 changes: 25 additions & 48 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,37 @@ std::unordered_map<std::string, std::string> parseArgs(ParseArgsOptions &options
return args;
}


#include <unordered_map>
#include <string>
#include <functional>

void setConfigOptions(const std::unordered_map<std::string, std::string> &args, Config &config) {
std::unordered_map<std::string, std::function<void(const std::string&)>> configSetters = {
{"cache", [&](const std::string &val) { config.cache = (val == "true"); }},
{"test", [&](const std::string &val) { config.test = (val == "true"); }},
{"start_date", [&](const std::string &val) { config.start_date = val; }},
{"end_date", [&](const std::string &val) { config.end_date = val; }},
{"frequency", [&](const std::string &val) { config.frequency = val; }},
{"formulas", [&](const std::string &val) { config.formulas = val; }},
{"aggregation", [&](const std::string &val) { config.aggregation = val; }}
};


for (const auto &arg : args) {
if (configSetters.count(arg.first)) {
void setConfigOptions(const std::unordered_map<std::string, std::string> &args, Config &config)
{
std::unordered_map<std::string, std::function<void(const std::string &)>> configSetters = {
{"cache", [&](const std::string &val)
{ config.cache = (val == "true"); }},
{"test", [&](const std::string &val)
{ config.test = (val == "true"); }},
{"start_date", [&](const std::string &val)
{ config.start_date = val; }},
{"end_date", [&](const std::string &val)
{ config.end_date = val; }},
{"frequency", [&](const std::string &val)
{ config.frequency = val; }},
{"formulas", [&](const std::string &val)
{ config.formulas = val; }},
{"aggregation", [&](const std::string &val)
{ config.aggregation = val; }}};

for (const auto &arg : args)
{
if (configSetters.count(arg.first))
{
configSetters[arg.first](arg.second);
}
}
}


int main(int argc, char *argv[])
{
// Initialize configuration with defaults
Expand All @@ -102,43 +108,14 @@ int main(int argc, char *argv[])
ParseArgsOptions poptions{argc, argv, indexes}; // Pass indexes by reference

auto args = parseArgs(poptions);
config.indexes = poptions.indexes;

/*
./evdscpp --cache true --test false --start_date 01-12-2020
*/

// Set config options if provided

if (args.count("cache"))
{
config.cache = args["cache"] == "true";
}
if (args.count("test"))
{
config.test = args["test"] == "true";
}

if (args.count("start_date"))
{
config.start_date = args["start_date"];
}
if (args.count("end_date"))
{
config.end_date = args["end_date"];
}

if (args.count("frequency"))
{
config.frequency = args["frequency"];
}
if (args.count("formulas"))
{
config.formulas = args["formulas"];
}
if (args.count("aggregation"))
{
config.aggregation = args["aggregation"];
}
setConfigOptions(args, config);

if (poptions.indexes.empty())
{
Expand Down

0 comments on commit 20808c5

Please sign in to comment.