From fe7c20fe9ea2cea6b904c66a78e252ed8eb2f6c3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 3 Apr 2018 00:04:47 +0200 Subject: [PATCH] Added Easy::get_link() --- CMakeLists.txt | 2 +- src/easy/easy.cpp | 33 +++++++++++++++++++++++++++++++++ src/easy/easy.hpp | 30 ++++++++++++++++++++++++++++++ src/mastodon-cpp.hpp | 19 ++++++++++--------- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93b076a..52366d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.8.0 + VERSION 0.8.1 LANGUAGES CXX ) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index f456c4f..86faf3d 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -76,3 +76,36 @@ const std::vector return vec; } + +Easy::Link::Link(const string &link_header) +: _next(0) +, _prev(0) +{ + std::regex renext("max_id=([[:digit:]]*)"); + std::regex reprev("since_id=([[:digit:]]*)"); + std::smatch match; + + if (std::regex_search(link_header, match, renext)) + { + _next = std::stoull(match[1].str()); + } + if (std::regex_search(link_header, match, reprev)) + { + _prev = std::stoull(match[1].str()); + } +} + +const uint_fast64_t Easy::Link::next() const +{ + return _next; +} + +const uint_fast64_t Easy::Link::prev() const +{ + return _prev; +} + +const Easy::Link Easy::get_link() const +{ + return Link(get_header("Link")); +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index e9d75ba..2a54403 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -104,6 +104,31 @@ public: typedef std::pair stream_event; + /*! + * @brief Class to hold the `Link`-header. + * + * Extracts max_id and since_id from the `Link:`-header + */ + class Link + { + public: + explicit Link(const string &link_header); + + /*! + * @brief Returns max_id + */ + const uint_fast64_t next() const; + + /*! + * @brief Returns since_id + */ + const uint_fast64_t prev() const; + + private: + uint_fast64_t _next; + uint_fast64_t _prev; + }; + /*! * @brief Constructs a new Easy object. * @@ -134,6 +159,11 @@ public: static const std::vector parse_stream(const std::string &streamdata); + /*! + * @brief Gets the links from the last answer + */ + const Link get_link() const; + /*! * @brief Base class for all entities. */ diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 534b317..f4b64ad 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -332,6 +333,15 @@ public: const string &code, string &access_token); + /*! + * @brief Gets the header from the last answer. + * + * @param header The header to get + * + * @return The header, or "" on error. + */ + const string get_header(const string &header) const; + /*! * @brief Make a GET request which doesn't require parameters. * @@ -638,15 +648,6 @@ public: const parametermap ¶meters, string &answer); - /*! - * @brief Gets the header from the last answer. - * - * @param header The header to get - * - * @return The header, or "" on error. - */ - const string get_header(const string &header) const; - private: const string _instance; string _access_token;