Added Easy::parse_stream()

This commit is contained in:
tastytea 2018-04-01 03:08:22 +02:00
parent 7f9e8aca0b
commit 4baef545db
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
2 changed files with 53 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include <ctime>
#include <iomanip> // get_time
#include <sstream>
#include <regex>
#include "easy.hpp"
#include "macros.hpp"
@ -85,6 +86,34 @@ const void Easy::Entity::from_string(const string &json)
}
}
const std::vector<Easy::stream_event>
Easy::parse_stream(const std::string &streamdata)
{
string stream = streamdata;
std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n");
std::smatch match;
std::vector<stream_event> vec = {};
while (std::regex_search(stream, match, reevent))
{
const string &event = match[1].str();
const string &data = match[2].str();
event_type type = event_type::Undefined;
if (event.compare("update") == 0)
type = event_type::Update;
else if (event.compare("notification") == 0)
type = event_type::Notification;
else if (event.compare("delete") == 0)
type = event_type::Delete;
vec.push_back(stream_event(type, data));
stream = match.suffix().str();
}
return vec;
}
const Json::Value Easy::Entity::to_object() const
{
return _tree;

View File

@ -21,6 +21,7 @@
#include <cstdint>
#include <chrono>
#include <vector>
#include <utility>
#include <jsoncpp/json/json.h>
// If we are compiling mastodon-cpp, use another include path
@ -42,6 +43,17 @@ namespace Mastodon
class Easy : public API
{
public:
/*!
* @brief Describes the event type
*/
enum class event_type
{
Update,
Notification,
Delete,
Undefined
};
/*!
* @brief Describes visibility of toots.
*
@ -92,6 +104,8 @@ public:
unknown
};
typedef std::pair<event_type, string> stream_event;
/*!
* @brief Constructs a new Easy object.
*
@ -112,6 +126,16 @@ public:
*/
static const std::vector<string> json_array_to_vector(const string &json);
/*!
* @brief Split stream into a vector of events
*
* @param streamdata Data from get_stream()
*
* @return vector of stream events
*/
static const std::vector<stream_event>
parse_stream(const std::string &streamdata);
/*!
* @brief Base class for entities.
*/