diff --git a/CMakeLists.txt b/CMakeLists.txt index 03d595e..6abd554 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.19 + VERSION 0.7.20 LANGUAGES CXX ) diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 30559bc..53c34e5 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -24,10 +24,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 17d14b0..eee9f28 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -19,7 +19,7 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP - #include "easy.hpp" + #include "easy/easy.hpp" #include "easy/account.hpp" #include "easy/application.hpp" #include "easy/attachment.hpp" @@ -36,7 +36,7 @@ #include "easy/status.hpp" #include "easy/tag.hpp" #else - #include + #include #include #include #include diff --git a/src/easy/application.hpp b/src/easy/application.hpp index 6350a6c..f3017c6 100644 --- a/src/easy/application.hpp +++ b/src/easy/application.hpp @@ -22,10 +22,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 50d2c48..415c1c3 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -53,10 +53,7 @@ const std::chrono::duration Attachment::duration() const { const double sec = get_double("meta.original.duration"); - if (sec > 0.0) - { - return std::chrono::duration(sec); - } + return std::chrono::duration(sec); } const std::array Attachment::focus() const diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp index ae546e4..299fb96 100644 --- a/src/easy/attachment.hpp +++ b/src/easy/attachment.hpp @@ -25,10 +25,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/card.hpp b/src/easy/card.hpp index 5b5036e..1b63dc1 100644 --- a/src/easy/card.hpp +++ b/src/easy/card.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/context.hpp b/src/easy/context.hpp index 37fa469..18fbad4 100644 --- a/src/easy/context.hpp +++ b/src/easy/context.hpp @@ -23,12 +23,12 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "status.hpp" + #include "easy/easy.hpp" + #include "easy/status.hpp" #else #include - #include - #include + #include + #include #endif using std::string; diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 247f9e4..f456c4f 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -23,8 +23,6 @@ using namespace Mastodon; using std::string; -using std::uint_fast64_t; -using std::chrono::system_clock; Easy::Easy(const string &instance, const string &access_token) : API(instance, access_token) @@ -51,41 +49,6 @@ const std::vector Easy::json_array_to_vector(const string &json) return {}; } -Easy::Entity::Entity(const string &json) -: _tree(Json::nullValue) -, _valid(false) -{ - from_string(json); -} - -const void Easy::Entity::from_string(const string &json) -{ - std::stringstream ss(json); - ss >> _tree; - - // If the JSON is a single object encapsulated in an array, - // transform it into an object. If the JSON string is [], transform to null - if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1) - { - _tree = _tree[0]; - } - - if (_tree.isNull()) - { - ttdebug << "ERROR: JSON string holds no object\n"; - ttdebug << "String was: " << json << '\n'; - } - else if (!_tree["error"].isNull()) - { - ttdebug << "ERROR: Server returned an error\n"; - ttdebug << "String was: " << json << '\n'; - } - else - { - _valid = true; - } -} - const std::vector Easy::parse_stream(const std::string &streamdata) { @@ -113,144 +76,3 @@ const std::vector return vec; } - -const Json::Value Easy::Entity::to_object() const -{ - return _tree; -} - -Easy::Entity::Entity() -: _valid(false) -{} - -const bool Easy::Entity::valid() const -{ - return _valid; -} - -const string Easy::Entity::error() const -{ - return get_string("error"); -} - -const Json::Value Easy::Entity::get(const string &key) const -{ - const Json::Value *node; - if (key.find('.') == std::string::npos) - { - node = &_tree[key]; - } - else - { - // If dots in key, we have to walk through the tree - std::size_t pos = 0; - string current_key = key; - node = &_tree; - while ((pos = current_key.find('.')) != std::string::npos) - { - try - { - node = &(*node)[current_key.substr(0, pos)]; - current_key = current_key.substr(pos + 1); - } - catch (const Json::LogicError &e) - { - ttdebug << e.what() << '\n'; - goto error; - } - } - node = &(*node)[current_key]; - } - - if (!node->isNull()) - { - return *node; - } - - error: - ttdebug << "Could not get data: " << key << '\n'; - return Json::Value(); -} - -const string Easy::Entity::get_string(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isString()) - { - return node.asString(); - } - - return ""; -} - -const uint_fast64_t Easy::Entity::get_uint64(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isUInt64()) - { - return node.asUInt64(); - } - - return 0; -} - -const double Easy::Entity::get_double(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isDouble()) - { - return node.asDouble(); - } - - return 0.0; -} - -const bool Easy::Entity::get_bool(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isBool()) - { - return node.asBool(); - } - - return false; -} - -const system_clock::time_point - Easy::Entity::get_time_point(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isString()) - { - std::stringstream sstime(node.asString()); - struct std::tm tm = {0}; - sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); - std::time_t time = timegm(&tm); - return system_clock::from_time_t(time); - } - - // Return clocks epoch - return system_clock::time_point(); -} - -const std::vector Easy::Entity::get_vector(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isArray()) - { - std::vector vec; - for (const Json::Value &value : node) - { - vec.push_back(value.asString()); - } - return vec; - } - - return {}; -} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index c1fbd87..360aa29 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -136,9 +136,6 @@ public: static const std::vector parse_stream(const std::string &streamdata); - /*! - * @brief Base class for entities. - */ class Entity { public: diff --git a/src/easy/emoji.hpp b/src/easy/emoji.hpp index afd0a68..45ab64f 100644 --- a/src/easy/emoji.hpp +++ b/src/easy/emoji.hpp @@ -22,10 +22,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/entity.cpp b/src/easy/entity.cpp new file mode 100644 index 0000000..f164225 --- /dev/null +++ b/src/easy/entity.cpp @@ -0,0 +1,203 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2018 tastytea + * + * This program 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, version 3. + * + * This program 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 this program. If not, see . + */ + +#include +#include // get_time +#include +#include +#include +#include "easy.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using std::string; +using std::chrono::system_clock; + +Easy::Entity::Entity(const string &json) +: _tree(Json::nullValue) +, _valid(false) +{ + from_string(json); +} + +const void Easy::Entity::from_string(const string &json) +{ + std::stringstream ss(json); + ss >> _tree; + + // If the JSON is a single object encapsulated in an array, + // transform it into an object. If the JSON string is [], transform to null + if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1) + { + _tree = _tree[0]; + } + + if (_tree.isNull()) + { + ttdebug << "ERROR: JSON string holds no object\n"; + ttdebug << "String was: " << json << '\n'; + } + else if (!_tree["error"].isNull()) + { + ttdebug << "ERROR: Server returned an error\n"; + ttdebug << "String was: " << json << '\n'; + } + else + { + _valid = true; + } +} + +const Json::Value Easy::Entity::to_object() const +{ + return _tree; +} + +Easy::Entity::Entity() +: _valid(false) +{} + +const bool Easy::Entity::valid() const +{ + return _valid; +} + +const string Easy::Entity::error() const +{ + return get_string("error"); +} + +const Json::Value Easy::Entity::get(const string &key) const +{ + const Json::Value *node; + if (key.find('.') == std::string::npos) + { + node = &_tree[key]; + } + else + { + // If dots in key, we have to walk through the tree + std::size_t pos = 0; + string current_key = key; + node = &_tree; + while ((pos = current_key.find('.')) != std::string::npos) + { + try + { + node = &(*node)[current_key.substr(0, pos)]; + current_key = current_key.substr(pos + 1); + } + catch (const Json::LogicError &e) + { + ttdebug << e.what() << '\n'; + goto error; + } + } + node = &(*node)[current_key]; + } + + if (!node->isNull()) + { + return *node; + } + + error: + ttdebug << "Could not get data: " << key << '\n'; + return Json::Value(); +} + +const string Easy::Entity::get_string(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isString()) + { + return node.asString(); + } + + return ""; +} + +const uint_fast64_t Easy::Entity::get_uint64(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isUInt64()) + { + return node.asUInt64(); + } + + return 0; +} + +const double Easy::Entity::get_double(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isDouble()) + { + return node.asDouble(); + } + + return 0.0; +} + +const bool Easy::Entity::get_bool(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isBool()) + { + return node.asBool(); + } + + return false; +} + +const system_clock::time_point + Easy::Entity::get_time_point(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isString()) + { + std::stringstream sstime(node.asString()); + struct std::tm tm = {0}; + sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); + std::time_t time = timegm(&tm); + return system_clock::from_time_t(time); + } + + // Return clocks epoch + return system_clock::time_point(); +} + +const std::vector Easy::Entity::get_vector(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(value.asString()); + } + return vec; + } + + return {}; +} diff --git a/src/easy/instance.hpp b/src/easy/instance.hpp index d80c418..0b26c69 100644 --- a/src/easy/instance.hpp +++ b/src/easy/instance.hpp @@ -23,11 +23,11 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" #else #include - #include + #include #include #endif diff --git a/src/easy/list.hpp b/src/easy/list.hpp index 654cbda..4e1bda8 100644 --- a/src/easy/list.hpp +++ b/src/easy/list.hpp @@ -24,10 +24,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/mention.hpp b/src/easy/mention.hpp index 9865b16..0789c24 100644 --- a/src/easy/mention.hpp +++ b/src/easy/mention.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/notification.hpp b/src/easy/notification.hpp index c9db8b7..d746eb7 100644 --- a/src/easy/notification.hpp +++ b/src/easy/notification.hpp @@ -24,12 +24,12 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" - #include "status.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" + #include "easy/status.hpp" #else #include - #include + #include #include #include #endif diff --git a/src/easy/relationship.hpp b/src/easy/relationship.hpp index 72402c7..b0ff9b0 100644 --- a/src/easy/relationship.hpp +++ b/src/easy/relationship.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/report.hpp b/src/easy/report.hpp index 93905cb..0d0abc7 100644 --- a/src/easy/report.hpp +++ b/src/easy/report.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/results.hpp b/src/easy/results.hpp index 81859e2..f9167d4 100644 --- a/src/easy/results.hpp +++ b/src/easy/results.hpp @@ -23,14 +23,14 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" - #include "status.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" + #include "easy/status.hpp" #else #include - #include - #include - #include + #include + #include + #include #endif using std::string; diff --git a/src/easy/status.hpp b/src/easy/status.hpp index e842a86..d31e78f 100644 --- a/src/easy/status.hpp +++ b/src/easy/status.hpp @@ -25,22 +25,22 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" - #include "emoji.hpp" - #include "attachment.hpp" - #include "mention.hpp" - #include "tag.hpp" - #include "application.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" + #include "easy/emoji.hpp" + #include "easy/attachment.hpp" + #include "easy/mention.hpp" + #include "easy/tag.hpp" + #include "easy/application.hpp" #else #include - #include - #include - #include - #include - #include - #include - #include + #include + #include + #include + #include + #include + #include + #include #endif using std::string; diff --git a/src/easy/tag.hpp b/src/easy/tag.hpp index 39447e5..032f928 100644 --- a/src/easy/tag.hpp +++ b/src/easy/tag.hpp @@ -22,10 +22,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string;