From 69fc8d805396b889b5e8c1c88e8129d93db77d29 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 20 Aug 2016 13:56:23 -0400 Subject: Updated API to use exceptions and make tweet/user objects more helpful --- src/tweet.cpp | 133 +++++++++++++++------------------------------------------- 1 file changed, 34 insertions(+), 99 deletions(-) (limited to 'src/tweet.cpp') diff --git a/src/tweet.cpp b/src/tweet.cpp index 2927018..4849fb7 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp @@ -1,134 +1,69 @@ #include "tweet.h" #include -#include - -using nlohmann::json; +#include "util.h" +#include "codes.h" +#include "client.h" namespace twitter { - tweet::tweet() : _valid(false) - { - - } - - tweet::tweet(std::string data) : _valid(true) + tweet::tweet(const client& tclient, std::string data) try + : _client(tclient) { - auto _data = json::parse(data); - _id = _data.at("id"); - _text = _data.at("text"); - _author = user(_data.at("user").dump()); + auto json = nlohmann::json::parse(data); + _id = json["id"].get(); + _text = json["text"].get(); + _author = make_unique(_client, json["user"].dump()); - if (_data.find("retweeted_status") != _data.end()) + if (!json["retweeted_status"].is_null()) { _is_retweet = true; - std::string retweet = _data.at("retweeted_status").dump(); - _retweeted_status = new tweet(retweet); + _retweeted_status = make_unique(_client, json["retweeted_status"].dump()); } - if (_data.find("entities") != _data.end()) + if (!json["entities"].is_null()) { - auto _entities = _data.at("entities"); - if (_entities.find("user_mentions") != _entities.end()) + auto entities = json["entities"]; + if (!entities["user_mentions"].is_null()) { - for (auto _mention : _entities.at("user_mentions")) + for (auto mention : entities["user_mentions"]) { - _mentions.push_back(std::make_pair(_mention.at("id"), _mention.at("screen_name").get())); + _mentions.push_back(std::make_pair(mention["id"].get(), mention["screen_name"].get())); } } } - } - - tweet::tweet(const tweet& other) + } catch (const std::invalid_argument& error) { - _valid = other._valid; - _id = other._id; - _text = other._text; - _author = other._author; - _is_retweet = other._is_retweet; - - if (_is_retweet) - { - _retweeted_status = new tweet(*other._retweeted_status); - } - - _mentions = other._mentions; - } - - tweet::tweet(tweet&& other) : tweet() + std::throw_with_nested(malformed_object("tweet", data)); + } catch (const std::domain_error& error) { - swap(*this, other); + std::throw_with_nested(malformed_object("tweet", data)); } - tweet::~tweet() + std::string tweet::generateReplyPrefill() const { - if (_is_retweet) + std::ostringstream output; + output << "@" << _author->getScreenName() << " "; + + for (auto mention : _mentions) { - delete _retweeted_status; + if ((mention.first != _author->getID()) && (mention.first != _client.getUser().getID())) + { + output << "@" << mention.second << " "; + } } - } - - tweet& tweet::operator=(tweet other) - { - swap(*this, other); - - return *this; - } - - void swap(tweet& first, tweet& second) - { - std::swap(first._valid, second._valid); - std::swap(first._id, second._id); - std::swap(first._text, second._text); - std::swap(first._author, second._author); - std::swap(first._is_retweet, second._is_retweet); - std::swap(first._retweeted_status, second._retweeted_status); - std::swap(first._mentions, second._mentions); - } - - tweet_id tweet::getID() const - { - assert(_valid); - - return _id; - } - - std::string tweet::getText() const - { - assert(_valid); - - return _text; - } - - const user& tweet::getAuthor() const - { - assert(_valid); - - return _author; - } - - bool tweet::isRetweet() const - { - assert(_valid); - - return _is_retweet; - } - - tweet tweet::getRetweet() const - { - assert(_valid && _is_retweet); - return *_retweeted_status; + return output.str(); } - std::vector> tweet::getMentions() const + tweet tweet::reply(std::string message, std::list media_ids) const { - return _mentions; + return _client.replyToTweet(message, _id, media_ids); } - tweet::operator bool() const + bool tweet::isMyTweet() const { - return _valid; + return *_author == _client.getUser(); } }; -- cgit 1.4.1