Skip to content

Commit 5852d8d

Browse files
committed
WIP: Add helper methods for accesssing expansions in individual tweet
1 parent 6cd9087 commit 5852d8d

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

lib/tweetkit/response/tweet.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def author_id
5252
data["author_id"]
5353
end
5454

55+
def author
56+
expansions&.users.find { |user| user.id == author_id }
57+
end
58+
5559
# Returns the origin / root Tweet ID of the conversation (which includes direct replies, replies of replies)
5660
#
5761
# @note The field +tweet.fields=conversation_id+ must be specified when fetching the tweet to access this data
@@ -61,6 +65,10 @@ def conversation_id
6165
data["conversation_id"]
6266
end
6367

68+
def conversation
69+
expansions&.tweets.find { |tweet| tweet.conversation_id == conversation_id }
70+
end
71+
6472
# If this Tweet is a reply, returns the user ID of the parent tweet's author
6573
#
6674
# @note The expansion +expansions=in_reply_to_user_id+ must be specified when fetching the tweet to access this data
@@ -70,6 +78,10 @@ def in_reply_to_user_id
7078
data["in_reply_to_user_id"]
7179
end
7280

81+
def reply_to
82+
expansions&.users.find { |user| user.id == in_reply_to_user_id }
83+
end
84+
7385
# A list of Tweets this Tweet refers to. For example, if the parent Tweet is a Retweet, a Retweet with comment (also known as Quoted Tweet) or a Reply, it will include the related Tweet referenced to by its parent
7486
#
7587
# @note The field +tweet.fields=referenced_tweets+ must be specified when fetching the tweet to access this data
@@ -87,6 +99,8 @@ def referenced_tweets
8799
#
88100
# @return [Attachments]
89101
def attachments
102+
return if data.dig("attachments", "media_keys").nil?
103+
90104
@attachments ||= Attachments.new(data["attachments"]["media_keys"])
91105
end
92106

@@ -96,6 +110,8 @@ def attachments
96110
#
97111
# @return [Polls]
98112
def polls
113+
return if data.dig("attachments", "poll_ids").nil?
114+
99115
@polls ||= Polls.new(data["attachments"]["poll_ids"])
100116
end
101117

@@ -105,6 +121,8 @@ def polls
105121
#
106122
# @return [Geo]
107123
def geo
124+
return if data["geo"].nil?
125+
108126
@geo ||= Geo.new(data["geo"])
109127
end
110128

@@ -116,6 +134,8 @@ def geo
116134
#
117135
# @return [ContextAnnotations]
118136
def context_annotations
137+
return if data["context_annotations"].nil?
138+
119139
@context_annotations ||= ContextAnnotations.new(data["context_annotations"])
120140
end
121141

@@ -128,6 +148,8 @@ def context_annotations
128148
#
129149
# @return [EntityAnnotations]
130150
def entity_annotations
151+
return if data["entities"].nil?
152+
131153
@entity_annotations ||= EntityAnnotations.new(data["entities"])
132154
end
133155

@@ -153,6 +175,8 @@ def withheld
153175
#
154176
# @return [Tweetkit::Response::Tweet::Metrics]
155177
def metrics
178+
return if data["public_metrics"].nil? && data["non_public_metrics"].nil? && data["organic_metrics"].nil? && data["promoted_metrics"].nil?
179+
156180
@metrics ||= Metrics.new(
157181
public_metrics: data["public_metrics"],
158182
private_metrics: data["non_public_metrics"],
@@ -250,7 +274,6 @@ def url
250274
alias_method :device, :source
251275
alias_method :date, :created_at
252276
alias_method :parent_tweet_id, :conversation_id
253-
alias_method :reply_to, :in_reply_to_user_id
254277
alias_method :nsfw?, :possibly_sensitive
255278
alias_method :sensitive?, :possibly_sensitive
256279
alias_method :non_public_metrics, :private_metrics

lib/tweetkit/response/tweets.rb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class Tweets
1919

2020
def initialize(response, **options)
2121
@response = response
22-
@tweets = extract_tweets(response["data"])
23-
assign_expansions(@tweets, expansions: response["includes"]) unless response["includes"].nil?
22+
@tweets = extract_tweets(response["data"], expansions: response["includes"])
2423
end
2524

2625
def each(*args, &block)
@@ -31,6 +30,14 @@ def last
3130
@tweets.last
3231
end
3332

33+
def [](key)
34+
if key.kind_of?(Integer)
35+
@tweets[key]
36+
else
37+
@tweets.send(:[], key)
38+
end
39+
end
40+
3441
# Returns the Tweets fetched from the given IDs
3542
#
3643
# @return [Tweetkit::Response::Tweets] Collection of Tweets fetched
@@ -47,28 +54,15 @@ def meta
4754
@meta ||= Meta.new(response["meta"])
4855
end
4956

50-
# Returns the expansions data based off the expansions passed in the initial query
51-
#
52-
# @return [Tweetkit::Response::Tweets::Expansions] Expansions data
53-
def expansions
54-
return if response["includes"].nil?
55-
56-
@expansions ||= Expansions.new(response["includes"])
57-
end
58-
5957
private
6058

61-
def extract_tweets(data)
59+
def extract_tweets(data, expansions:)
6260
if data.kind_of? Array
63-
data.collect { |data| Tweet.new(data) }
61+
data.collect { |data| Tweet.new(data, expansions:) }
6462
else
65-
[Tweet.new(data)]
63+
[Tweet.new(data, expansions:)]
6664
end
6765
end
68-
69-
def assign_expansions(tweets, expansions:)
70-
# TODO: Think of how to assign expansions when Tweet is in multiform
71-
end
7266
end
7367
end
7468
end

0 commit comments

Comments
 (0)