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/user.cpp | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) (limited to 'src/user.cpp') diff --git a/src/user.cpp b/src/user.cpp index 0fa1e39..0b6e93a 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -1,51 +1,43 @@ #include "user.h" #include - -using nlohmann::json; +#include "codes.h" +#include "client.h" namespace twitter { - user::user() : _valid(false) - { - - } - - user::user(std::string data) : _valid(true) + user::user(const client& tclient, std::string data) try + : _client(tclient) { - auto _data = json::parse(data); - _id = _data.at("id"); - _screen_name = _data.at("screen_name"); - _name = _data.at("name"); - } - - user_id user::getID() const + auto json = nlohmann::json::parse(data); + _id = json["id"].get(); + _screen_name = json["screen_name"].get(); + _name = json["name"].get(); + } catch (const std::invalid_argument& error) { - return _id; - } - - std::string user::getScreenName() const + std::throw_with_nested(malformed_object("user", data)); + } catch (const std::domain_error& error) { - return _screen_name; + std::throw_with_nested(malformed_object("user", data)); } - std::string user::getName() const + std::set user::getFriends() const { - return _name; + return _client.getFriends(_id); } - user::operator bool() const + std::set user::getFollowers() const { - return _valid; + return _client.getFollowers(_id); } - bool user::operator==(const user& other) const + void user::follow() const { - return _id == other._id; + _client.follow(_id); } - bool user::operator!=(const user& other) const + void user::unfollow() const { - return _id != other._id; + _client.unfollow(_id); } }; -- cgit 1.4.1