First implementation
It seems to work but I think the ticker value returned by the remote call is wrong. Maybe I'm calling the wrong endpoint ¯\_(ツ)_/¯ I don't know trying to work it out
This commit is contained in:
commit
1d61cc7f2f
21 changed files with 1474 additions and 0 deletions
135
src/main.cpp
Normal file
135
src/main.cpp
Normal file
|
@ -0,0 +1,135 @@
|
|||
/* Copyright 2022, Michele Santullo
|
||||
* This file is part of duckticker.
|
||||
*
|
||||
* Wrenpp is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Wrenpp is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "nap/quick_rest.hpp"
|
||||
#include <simdjson.h>
|
||||
#include <wrenpp/vm_fun.hpp>
|
||||
#include <wrenpp/def_configuration.hpp>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
constexpr char g_script[] =
|
||||
R"script(
|
||||
foreign class TickerPrice {
|
||||
construct new() { }
|
||||
|
||||
foreign price
|
||||
foreign currency
|
||||
}
|
||||
|
||||
class Bitcoinity {
|
||||
foreign static ticker(currency)
|
||||
}
|
||||
|
||||
class App {
|
||||
construct new(currency) {
|
||||
_currency = currency
|
||||
}
|
||||
|
||||
start() {
|
||||
var price = Bitcoinity.ticker(_currency)
|
||||
System.print("Ticker: %(price.price) %(price.currency)")
|
||||
}
|
||||
}
|
||||
|
||||
var the_app = App.new("USD")
|
||||
)script";
|
||||
|
||||
class TickerPrice {
|
||||
public:
|
||||
TickerPrice() = default;
|
||||
TickerPrice (double price, std::string&& currency) :
|
||||
m_price(price),
|
||||
m_currency(std::move(currency))
|
||||
{}
|
||||
|
||||
void set_price (double price) { m_price = price; }
|
||||
void set_currency (std::string&& currency) { m_currency = std::move(currency); }
|
||||
double price() const noexcept { return m_price; }
|
||||
const char* currency() const noexcept { return m_currency.c_str(); }
|
||||
|
||||
private:
|
||||
double m_price{};
|
||||
std::string m_currency;
|
||||
};
|
||||
|
||||
TickerPrice ticker_price (std::string_view currency) {
|
||||
nap::QuickRest qrest{simdjson::SIMDJSON_PADDING};
|
||||
qrest.set_user_agent("duckticker");
|
||||
qrest.add_post("currency", currency);
|
||||
|
||||
auto response = qrest.fetch("https://bitcoinity.org/simple_switch_currency");
|
||||
//std::cout << response.body << '\n';
|
||||
|
||||
simdjson::ondemand::parser parser;
|
||||
simdjson::padded_string_view body{response.body, response.body.size() + simdjson::SIMDJSON_PADDING};
|
||||
simdjson::ondemand::document doc = parser.iterate(body);
|
||||
const double price_out = doc["price"];
|
||||
const std::string_view currency_out = doc["currency"];
|
||||
return {price_out, std::string{currency_out}};
|
||||
}
|
||||
|
||||
void ticker_price_wren (wren::VM& vm) {
|
||||
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "main");
|
||||
*tp = ticker_price(wren::get<const char*>(vm, 1));
|
||||
}
|
||||
|
||||
class MyWrenConfiguration : public wren::DefConfiguration {
|
||||
public:
|
||||
wren::foreign_method_t foreign_method_fn(
|
||||
wren::VM* vm,
|
||||
std::string_view module,
|
||||
std::string_view class_name,
|
||||
bool is_static,
|
||||
std::string_view signature
|
||||
) {
|
||||
if ("main" == module) {
|
||||
if (is_static and "Bitcoinity" == class_name and "ticker(_)" == signature) {
|
||||
return &ticker_price_wren;
|
||||
}
|
||||
else if ("TickerPrice" == class_name) {
|
||||
if (not is_static and "price" == signature)
|
||||
return wren::make_method_bindable<&TickerPrice::price>();
|
||||
else if (not is_static and "currency" == signature)
|
||||
return wren::make_method_bindable<&TickerPrice::currency>();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
wren::foreign_class_t foreign_class_fn(
|
||||
wren::VM* vm,
|
||||
std::string_view module,
|
||||
std::string_view class_name
|
||||
) {
|
||||
if (module == "main" and class_name == "TickerPrice")
|
||||
return wren::make_foreign_class<TickerPrice>();
|
||||
else
|
||||
return {nullptr, nullptr};
|
||||
}
|
||||
};
|
||||
} //unnamed namespace
|
||||
|
||||
int main() {
|
||||
MyWrenConfiguration config;
|
||||
wren::VM vm(&config, nullptr);
|
||||
vm.interpret("main", g_script);
|
||||
|
||||
wren::call<void>(vm, {"main", "the_app"}, "start");
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue