about summary refs log tree commit diff stats
path: root/src/tweet.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-08-30 20:43:23 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-08-30 20:43:23 -0400
commit8584857578c3a2946a03de98ff3803431143297a (patch)
treee3cb3d9487a7b72a22e5dd50d91011d966a4ee15 /src/tweet.cpp
parenta9d33aa08df10102539e33c0c6d4334e9e99a470 (diff)
downloadlibtwittercpp-8584857578c3a2946a03de98ff3803431143297a.tar.gz
libtwittercpp-8584857578c3a2946a03de98ff3803431143297a.tar.bz2
libtwittercpp-8584857578c3a2946a03de98ff3803431143297a.zip
Refactored tweet and user classes
The only API-visible change is that these classes are no longer default constructible. Other than that, tweet now uses hatkirby::recptr to wrap its retweeted_status (tweet) and author (user) member objects, removing the need to implement the Rule of Five.
Diffstat (limited to 'src/tweet.cpp')
-rw-r--r--src/tweet.cpp101
1 files changed, 32 insertions, 69 deletions
diff --git a/src/tweet.cpp b/src/tweet.cpp index 1003647..7f10a5c 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp
@@ -7,83 +7,48 @@
7 7
8namespace twitter { 8namespace twitter {
9 9
10 tweet::tweet(std::string data) try 10 tweet::tweet(std::string data)
11 : _valid(true)
12 { 11 {
13 auto json = nlohmann::json::parse(data); 12 try
14 _id = json["id"].get<tweet_id>();
15 _text = json["text"].get<std::string>();
16 _author = std::make_unique<user>(json["user"].dump());
17
18 std::tm ctt = { 0 };
19 std::stringstream createdAtStream;
20 createdAtStream << json["created_at"].get<std::string>();
21 createdAtStream >> std::get_time(&ctt, "%a %b %d %H:%M:%S +0000 %Y");
22 _created_at = twitter::timegm(&ctt);
23
24 if (!json["retweeted_status"].is_null())
25 { 13 {
26 _is_retweet = true; 14 auto json = nlohmann::json::parse(data);
15 _id = json["id"].get<tweet_id>();
16 _text = json["text"].get<std::string>();
17 _author = new user(json["user"].dump());
18
19 std::tm ctt = { 0 };
20 std::stringstream createdAtStream;
21 createdAtStream << json["created_at"].get<std::string>();
22 createdAtStream >> std::get_time(&ctt, "%a %b %d %H:%M:%S +0000 %Y");
23 _created_at = twitter::timegm(&ctt);
24
25 if (!json["retweeted_status"].is_null())
26 {
27 _is_retweet = true;
27 28
28 _retweeted_status = std::make_unique<tweet>(json["retweeted_status"].dump()); 29 _retweeted_status = new tweet(json["retweeted_status"].dump());
29 } 30 }
30 31
31 if (!json["entities"].is_null()) 32 if (!json["entities"].is_null())
32 {
33 auto entities = json["entities"];
34 if (!entities["user_mentions"].is_null())
35 { 33 {
36 for (auto mention : entities["user_mentions"]) 34 auto entities = json["entities"];
35 if (!entities["user_mentions"].is_null())
37 { 36 {
38 _mentions.push_back(std::make_pair(mention["id"].get<user_id>(), mention["screen_name"].get<std::string>())); 37 for (auto mention : entities["user_mentions"])
38 {
39 _mentions.emplace_back(
40 mention["id"].get<user_id>(),
41 mention["screen_name"].get<std::string>());
42 }
39 } 43 }
40 } 44 }
41 } 45 } catch (const std::invalid_argument& error)
42 } catch (const std::invalid_argument& error)
43 {
44 std::throw_with_nested(malformed_object("tweet", data));
45 } catch (const std::domain_error& error)
46 {
47 std::throw_with_nested(malformed_object("tweet", data));
48 }
49
50 tweet::tweet(const tweet& other)
51 {
52 _valid = other._valid;
53 _id = other._id;
54 _text = other._text;
55 _author = std::make_unique<user>(*other._author);
56 _is_retweet = other._is_retweet;
57
58 if (_is_retweet)
59 { 46 {
60 _retweeted_status = std::make_unique<tweet>(*other._retweeted_status); 47 std::throw_with_nested(malformed_object("tweet", data));
48 } catch (const std::domain_error& error)
49 {
50 std::throw_with_nested(malformed_object("tweet", data));
61 } 51 }
62
63 _mentions = other._mentions;
64 }
65
66 tweet::tweet(tweet&& other) : tweet()
67 {
68 swap(*this, other);
69 }
70
71 tweet& tweet::operator=(tweet other)
72 {
73 swap(*this, other);
74
75 return *this;
76 }
77
78 void swap(tweet& first, tweet& second)
79 {
80 std::swap(first._valid, second._valid);
81 std::swap(first._id, second._id);
82 std::swap(first._text, second._text);
83 std::swap(first._author, second._author);
84 std::swap(first._is_retweet, second._is_retweet);
85 std::swap(first._retweeted_status, second._retweeted_status);
86 std::swap(first._mentions, second._mentions);
87 } 52 }
88 53
89 std::string tweet::generateReplyPrefill(const user& me) const 54 std::string tweet::generateReplyPrefill(const user& me) const
@@ -104,8 +69,6 @@ namespace twitter {
104 69
105 std::string tweet::getURL() const 70 std::string tweet::getURL() const
106 { 71 {
107 assert(_valid);
108
109 std::ostringstream urlstr; 72 std::ostringstream urlstr;
110 urlstr << "https://twitter.com/"; 73 urlstr << "https://twitter.com/";
111 urlstr << _author->getScreenName(); 74 urlstr << _author->getScreenName();