Skip to content

Commit 67e44eb

Browse files
committed
WIP: Add docs
1 parent 225c33c commit 67e44eb

20 files changed

Lines changed: 448 additions & 226 deletions

File tree

lib/tweetkit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "debug"
12
require "zeitwerk"
23

34
loader = Zeitwerk::Loader.for_gem

lib/tweetkit/connection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def parse_options(options)
177177
#
178178
# @return [String] A formatted error messages with data from the Twitter error
179179
def format_error_message(error)
180+
debugger
180181
error_obj = JSON.parse(error.response_body)
181182

182183
<<-ERR

lib/tweetkit/response.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
module Tweetkit
4+
# Class for creating a response object from the resources returned by the Twitter v2 API
45
class Response
56
RESOURCE_CLASS_MAP = {
67
tweet: "Tweetkit::Response::Tweet",

lib/tweetkit/response/tweet.rb

Lines changed: 187 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module Tweetkit
22
class Response
3+
# Class for individual Tweets
34
class Tweet
4-
attr_accessor :annotations, :attachments, :data
5+
attr_accessor :attachments, :data
56

67
def initialize(data)
78
if data["data"].nil?
@@ -14,92 +15,251 @@ def initialize(data)
1415
# @attachments = Attachments.new(data["attachments"])
1516
end
1617

18+
# Unique ID for this Tweet
19+
#
20+
# @return [String] Unique ID for this Tweet
1721
def id
1822
data["id"]
1923
end
2024

25+
# @see text
2126
def body
2227
text
2328
end
2429

30+
# @see text
2531
def content
2632
text
2733
end
2834

35+
# The content of the Tweet
36+
#
37+
# @return [String] The content of the Tweet
2938
def text
3039
data["text"]
3140
end
3241

42+
# @see created_at
43+
def date
44+
created_at
45+
end
46+
47+
# The time (ISO 8601) the Tweet was created
48+
#
49+
# @note The field +tweet.fields=created_at+ must be specified when fetching the tweet to access this data
50+
#
51+
# @return [String] The time (ISO 8601) the Tweet was created
52+
def created_at
53+
data["created_at"]
54+
end
55+
56+
# Unique identifier of this user
57+
#
58+
# @note The expansion +expansions=author_id+ must be specified when fetching the tweet to access this data
59+
#
60+
# @return [String] Unique identifier of this user
3361
def author_id
3462
data["author_id"]
3563
end
3664

37-
def conversation_id
38-
data["conversation_id"]
65+
# @see conversation_id
66+
def parent_tweet_id
67+
conversation_id
3968
end
4069

41-
def created_at
42-
data["created_at"]
70+
# Returns the origin / root Tweet ID of the conversation (which includes direct replies, replies of replies)
71+
#
72+
# @note The field +tweet.fields=conversation_id+ must be specified when fetching the tweet to access this data
73+
#
74+
# @return [String] Returns the origin / root Tweet ID of the conversation
75+
def conversation_id
76+
data["conversation_id"]
4377
end
4478

79+
# @see in_reply_to_user_id
4580
def reply_to
4681
in_reply_to_user_id
4782
end
4883

84+
# If this Tweet is a reply, returns the user ID of the parent tweet's author
85+
#
86+
# @note The expansion +expansions=in_reply_to_user_id+ must be specified when fetching the tweet to access this data
87+
#
88+
# @return [String] If this Tweet is a Reply, indicates the user ID of the parent Tweet's author.
4989
def in_reply_to_user_id
5090
data["in_reply_to_user_id"]
5191
end
5292

53-
def lang
54-
data["lang"]
93+
# 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
94+
#
95+
# @note The field +tweet.fields=referenced_tweets+ must be specified when fetching the tweet to access this data
96+
#
97+
# @return [Array] A list of Tweets this Tweet refers to
98+
def referenced_tweets
99+
data["referenced_tweets"]
100+
end
101+
102+
# Specifies the type of attachments (if any) present in this Tweet
103+
#
104+
# @note The field +tweet.fields=referenced_tweets+ must be specified when fetching the tweet to access this data
105+
#
106+
# @return TODO
107+
def attachments
108+
@attachments
109+
end
110+
111+
# Contains details about the location tagged by the user in this Tweet, if they specified one.
112+
#
113+
# @note The field +tweet.fields=referenced_tweets+ must be specified when fetching the tweet to access this data
114+
#
115+
# @return TODO
116+
def geo
117+
end
118+
119+
# Context annotations for the Tweet.
120+
#
121+
# @note The field +tweet.fields=context_annotations+ must be specified when fetching the tweet to access this data
122+
#
123+
# @see https://developer.twitter.com/en/docs/twitter-api/annotations/overview
124+
#
125+
# @return TODO
126+
def context_annotations
127+
@annotations.context_annotations || nil
128+
end
129+
130+
# @see entities
131+
def entity_annotations
132+
entities
133+
end
134+
135+
# Entity annotations for the Tweet.
136+
#
137+
# @note The field +tweet.fields=entities+ must be specified when fetching the tweet to access this data
138+
#
139+
# @see https://developer.twitter.com/en/docs/twitter-api/annotations/overview
140+
#
141+
# @return TODO
142+
def entities
143+
@annotations.entity_annotations || nil
144+
end
145+
146+
# Determines whether the Tweet is withheld or otherwise
147+
#
148+
# @see withheld
149+
#
150+
# @return [Boolean]
151+
def withheld?
152+
withheld && !withheld.empty?
153+
end
154+
155+
# Returns withholding details for withheld content
156+
#
157+
# @see https://help.twitter.com/en/rules-and-policies/tweet-withheld-by-country
158+
#
159+
# @note The field +tweet.fields=withheld+ must be specified when fetching the tweet to access this data
160+
#
161+
# @return [Hash] Returns withholding details for withheld content
162+
def withheld
163+
data["withheld"]
164+
end
165+
166+
# Engagement metrics for the Tweet at the time of the request.
167+
#
168+
# @note The field +tweet.fields=public_metrics+ must be specified when fetching the tweet to access this data
169+
#
170+
# @return [Hash] Engagement metrics for the Tweet at the time of the request.
171+
def public_metrics
172+
data["public_metrics"]
173+
end
174+
175+
# Non-public engagement metrics for the Tweet at the time of the request.
176+
#
177+
# @note The field +tweet.fields=non_public_metrics+ must be specified when fetching the tweet to access this data
178+
# @note This is a private metric, and requires the use of OAuth 2.0 User Context authentication
179+
#
180+
# @return [Hash] Non-public engagement metrics for the Tweet at the time of the request.
181+
def non_public_metrics
182+
data["non_public_metrics"]
55183
end
56184

185+
# Organic engagement metrics for the Tweet at the time of the request.
186+
#
187+
# @note The field +tweet.fields=organic_metrics+ must be specified when fetching the tweet to access this data
188+
# @note This is a private metric, and requires the use of OAuth 2.0 User Context authentication
189+
#
190+
# @return [Hash] Organic engagement metrics for the Tweet at the time of the request.
191+
def organic_metrics
192+
data["organic_metrics"]
193+
end
194+
195+
# Engagement metrics for the Tweet at the time of the request in a promoted context.
196+
#
197+
# @note The field +tweet.fields=promoted_metrics+ must be specified when fetching the tweet to access this data
198+
# @note This is a private metric, and requires the use of OAuth 2.0 User Context authentication
199+
#
200+
# @return [Hash] Engagement metrics for the Tweet at the time of the request in a promoted context.
201+
def promoted_metrics
202+
data["promoted_metrics"]
203+
end
204+
205+
# @see possibility_sensitive
57206
def nsfw?
58207
possibly_sensitive
59208
end
60209

210+
# @see possibility_sensitive
61211
def sensitive?
62212
possibly_sensitive
63213
end
64214

215+
# Indicates if this Tweet contains URLs marked as sensitive, for example content suitable for mature audiences.
216+
#
217+
# @note The field +tweet.fields=possibly_sensitve+ must be specified when fetching the tweet to access this data
218+
#
219+
# @return [Boolean] Indicates if this Tweet contains URLs marked as sensitive
65220
def possibly_sensitive
66221
data["possibly_sensitive"]
67222
end
68223

69-
def permission
70-
reply_settings
224+
# Language of the Tweet, if detected by Twitter. Returned as a BCP47 language tag.
225+
#
226+
# @note The field +tweet.fields=lang+ must be specified when fetching the tweet to access this data
227+
#
228+
# @return [String] Language of the Tweet, if detected by Twitter. Returned as a BCP47 language tag.
229+
def lang
230+
data["lang"]
71231
end
72232

233+
# Shows who can reply to this Tweet.
234+
#
235+
# @note The field +tweet.fields=reply_settings+ must be specified when fetching the tweet to access this data
236+
#
237+
# @return [String] Returns one of +everyone+, +mentionedUsers+, and +following+
73238
def reply_settings
74239
data["reply_settings"]
75240
end
76241

242+
# @see source
77243
def device
78244
source
79245
end
80246

247+
# The name of the app the user Tweeted from.
248+
#
249+
# @note The field +tweet.fields=source+ must be specified when fetching the tweet to access this data
250+
#
251+
# @return [String] The name of the app the user Tweeted from.
81252
def source
82253
data["source"]
83254
end
84255

85-
def withheld?
86-
withheld && !withheld.empty?
87-
end
88-
89-
def withheld
90-
data["withheld"]
91-
end
92-
93-
def context_annotations
94-
@annotations.context_annotations || nil
95-
end
96-
97-
def entity_annotations
98-
entities
99-
end
100-
101-
def entities
102-
@annotations.entity_annotations || nil
256+
# The URL to the Tweet.
257+
#
258+
# @note The expansion +expansions=author_id+ must be specified when fetching the tweet to access this data
259+
#
260+
# @return [String] The URL to the tweet.
261+
def url
262+
"https://twitter.com/#{author_id}/status/#{id}"
103263
end
104264
end
105265
end
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
module Tweetkit::Response::Tweet
2-
class Annotations
3-
attr_accessor :context_annotations, :entity_annotations
1+
module Tweetkit
2+
class Response
3+
class Tweet
4+
class Annotations
5+
attr_accessor :context_annotations, :entity_annotations
46

5-
def initialize(context_annotations, entity_annotations)
6-
return unless context_annotations || entity_annotations
7+
def initialize(context_annotations, entity_annotations)
8+
return unless context_annotations || entity_annotations
79

8-
@context_annotations = Context.new(context_annotations)
9-
@entity_annotations = Entity.new(entity_annotations)
10+
@context_annotations = Context.new(context_annotations)
11+
@entity_annotations = Entity.new(entity_annotations)
12+
end
13+
end
1014
end
1115
end
1216
end

lib/tweetkit/response/tweet/annotations/context.rb

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
module Tweetkit::Response::Tweet::Annotations
2-
class Context
3-
include Enumerable
1+
module Tweetkit
2+
class Response
3+
class Tweet
4+
class Annotations
5+
class Context
6+
include Enumerable
47

5-
attr_accessor :annotations
8+
attr_accessor :annotations
69

7-
def initialize(annotations)
8-
return unless annotations
10+
def initialize(annotations)
11+
return unless annotations
912

10-
@annotations = annotations.collect { |annotation| Annotation.new(annotation) }
11-
end
13+
@annotations = annotations.collect { |annotation| Annotation.new(annotation) }
14+
end
1215

13-
def each(*args, &block)
14-
annotations.each(*args, &block)
15-
end
16+
def each(*args, &block)
17+
annotations.each(*args, &block)
18+
end
1619

17-
class Annotation
18-
attr_accessor :domain, :entity
20+
class Annotation
21+
attr_accessor :domain, :entity
1922

20-
def initialize(annotation)
21-
@domain = annotation['domain']
22-
@entity = annotation['entity']
23+
def initialize(annotation)
24+
@domain = annotation["domain"]
25+
@entity = annotation["entity"]
26+
end
27+
end
28+
end
2329
end
2430
end
2531
end

0 commit comments

Comments
 (0)