Module: T::RequestableAPI::TweetEndpoints

Included in:
T::RequestableAPI
Defined in:
lib/t/requestable_api/tweet_endpoints.rb

Instance Method Summary collapse

Instance Method Details

#x_favorites(user = nil, opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 49

def x_favorites(user = nil, opts = {})
  if user.is_a?(Hash) && opts.empty?
    opts = user
    user = nil
  end
  user_id = user.nil? ? current_user_id : resolve_user_id(user)
  extract_tweets(t_get_v2("users/#{user_id}/liked_tweets", timeline_v2_params(opts)))
end

#x_home_timeline(opts = {}) ⇒ Object



34
35
36
37
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 34

def x_home_timeline(opts = {})
  me_id = current_user_id
  extract_tweets(t_get_v2("users/#{me_id}/timelines/reverse_chronological", timeline_v2_params(opts)))
end

#x_mentions(opts = {}) ⇒ Object



44
45
46
47
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 44

def x_mentions(opts = {})
  me_id = current_user_id
  extract_tweets(t_get_v2("users/#{me_id}/mentions", timeline_v2_params(opts)))
end

#x_retweeted_by_me(opts = {}) ⇒ Object



8
9
10
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 8

def x_retweeted_by_me(opts = {})
  x_retweets_of_me(opts)
end

#x_retweeted_by_user(user, opts = {}) ⇒ Object



12
13
14
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 12

def x_retweeted_by_user(user, opts = {})
  x_user_timeline(user, opts).select { |tweet| tweet["full_text"].to_s.start_with?("RT @") }
end

#x_retweeters_ids(tweet_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 16

def x_retweeters_ids(tweet_id)
  ids = []
  params = {"user.fields": "id,username", max_results: "100"}
  MAX_PAGE.times do
    response = t_get_v2("tweets/#{tweet_id}/retweeted_by", params)
    ids.concat(extract_ids(response))
    token = response.dig("meta", "next_token")
    break if token.nil?

    params = params.merge(pagination_token: token)
  end
  ids
end

#x_retweets_of_me(opts = {}) ⇒ Object



4
5
6
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 4

def x_retweets_of_me(opts = {})
  extract_tweets(t_get_v2("users/reposts_of_me", timeline_v2_params(opts)))
end

#x_search(query, opts = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 58

def x_search(query, opts = {})
  count = [opts.fetch(:count, MAX_SEARCH_RESULTS).to_i, MAX_SEARCH_RESULTS].min
  params = {
    query: query.to_s,
    max_results: count.to_s,
  }.merge(v2_tweet_params)
  params[:until_id] = opts[:max_id].to_s if opts[:max_id]
  params[:since_id] = opts[:since_id].to_s if opts[:since_id]
  extract_tweets(t_get_v2("tweets/search/recent", params))
end

#x_status(status_id, _opts = {}) ⇒ Object



30
31
32
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 30

def x_status(status_id, _opts = {})
  extract_tweets(t_get_v2("tweets/#{status_id}", v2_tweet_params)).first || {}
end

#x_update(status, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 69

def x_update(status, opts = {})
  body = {text: status.to_s}
  body[:reply] = {in_reply_to_tweet_id: opts[:in_reply_to_status_id].to_s} if opts[:in_reply_to_status_id]
  body[:media] = {media_ids: Array(opts[:media_ids]).map(&:to_s)} if opts[:media_ids]
  response = t_post_v2_json("tweets", body)
  id = value_id(response) || value_id(response["data"])
  {"id" => id.to_i, "id_str" => id.to_s, "text" => status.to_s,
   "full_text" => status.to_s, "user" => current_user}
end

#x_update_with_media(status, file, opts = {}) ⇒ Object



79
80
81
82
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 79

def x_update_with_media(status, file, opts = {})
  media_id = upload_media(file)
  x_update(status, opts.merge(media_ids: [media_id]))
end

#x_user_timeline(user, opts = {}) ⇒ Object



39
40
41
42
# File 'lib/t/requestable_api/tweet_endpoints.rb', line 39

def x_user_timeline(user, opts = {})
  user_id = resolve_user_id(user)
  extract_tweets(t_get_v2("users/#{user_id}/tweets", timeline_v2_params(opts)))
end