diff options
Diffstat (limited to 'src/user.cpp')
| -rw-r--r-- | src/user.cpp | 48 |
1 files changed, 20 insertions, 28 deletions
| 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 @@ | |||
| 1 | #include "user.h" | 1 | #include "user.h" |
| 2 | #include <json.hpp> | 2 | #include <json.hpp> |
| 3 | 3 | #include "codes.h" | |
| 4 | using nlohmann::json; | 4 | #include "client.h" |
| 5 | 5 | ||
| 6 | namespace twitter { | 6 | namespace twitter { |
| 7 | 7 | ||
| 8 | user::user() : _valid(false) | 8 | user::user(const client& tclient, std::string data) try |
| 9 | { | 9 | : _client(tclient) |
| 10 | |||
| 11 | } | ||
| 12 | |||
| 13 | user::user(std::string data) : _valid(true) | ||
| 14 | { | 10 | { |
| 15 | auto _data = json::parse(data); | 11 | auto json = nlohmann::json::parse(data); |
| 16 | _id = _data.at("id"); | 12 | _id = json["id"].get<user_id>(); |
| 17 | _screen_name = _data.at("screen_name"); | 13 | _screen_name = json["screen_name"].get<std::string>(); |
| 18 | _name = _data.at("name"); | 14 | _name = json["name"].get<std::string>(); |
| 19 | } | 15 | } catch (const std::invalid_argument& error) |
| 20 | |||
| 21 | user_id user::getID() const | ||
| 22 | { | 16 | { |
| 23 | return _id; | 17 | std::throw_with_nested(malformed_object("user", data)); |
| 24 | } | 18 | } catch (const std::domain_error& error) |
| 25 | |||
| 26 | std::string user::getScreenName() const | ||
| 27 | { | 19 | { |
| 28 | return _screen_name; | 20 | std::throw_with_nested(malformed_object("user", data)); |
| 29 | } | 21 | } |
| 30 | 22 | ||
| 31 | std::string user::getName() const | 23 | std::set<user_id> user::getFriends() const |
| 32 | { | 24 | { |
| 33 | return _name; | 25 | return _client.getFriends(_id); |
| 34 | } | 26 | } |
| 35 | 27 | ||
| 36 | user::operator bool() const | 28 | std::set<user_id> user::getFollowers() const |
| 37 | { | 29 | { |
| 38 | return _valid; | 30 | return _client.getFollowers(_id); |
| 39 | } | 31 | } |
| 40 | 32 | ||
| 41 | bool user::operator==(const user& other) const | 33 | void user::follow() const |
| 42 | { | 34 | { |
| 43 | return _id == other._id; | 35 | _client.follow(_id); |
| 44 | } | 36 | } |
| 45 | 37 | ||
| 46 | bool user::operator!=(const user& other) const | 38 | void user::unfollow() const |
| 47 | { | 39 | { |
| 48 | return _id != other._id; | 40 | _client.unfollow(_id); |
| 49 | } | 41 | } |
| 50 | 42 | ||
| 51 | }; | 43 | }; |
