Added Easy::parse_stream()
This commit is contained in:
parent
7f9e8aca0b
commit
4baef545db
|
@ -17,6 +17,7 @@
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iomanip> // get_time
|
#include <iomanip> // get_time
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <regex>
|
||||||
#include "easy.hpp"
|
#include "easy.hpp"
|
||||||
#include "macros.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
|
const Json::Value Easy::Entity::to_object() const
|
||||||
{
|
{
|
||||||
return _tree;
|
return _tree;
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
|
|
||||||
// If we are compiling mastodon-cpp, use another include path
|
// If we are compiling mastodon-cpp, use another include path
|
||||||
|
@ -42,6 +43,17 @@ namespace Mastodon
|
||||||
class Easy : public API
|
class Easy : public API
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/*!
|
||||||
|
* @brief Describes the event type
|
||||||
|
*/
|
||||||
|
enum class event_type
|
||||||
|
{
|
||||||
|
Update,
|
||||||
|
Notification,
|
||||||
|
Delete,
|
||||||
|
Undefined
|
||||||
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Describes visibility of toots.
|
* @brief Describes visibility of toots.
|
||||||
*
|
*
|
||||||
|
@ -92,6 +104,8 @@ public:
|
||||||
unknown
|
unknown
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef std::pair<event_type, string> stream_event;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Constructs a new Easy object.
|
* @brief Constructs a new Easy object.
|
||||||
*
|
*
|
||||||
|
@ -112,6 +126,16 @@ public:
|
||||||
*/
|
*/
|
||||||
static const std::vector<string> json_array_to_vector(const string &json);
|
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.
|
* @brief Base class for entities.
|
||||||
*/
|
*/
|
||||||
|
|
Reference in New Issue
Block a user