Module: T::RequestableAPI::UserEndpoints

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

Instance Method Summary collapse

Instance Method Details

#x_follower_ids(user = nil) ⇒ Object



65
66
67
68
# File 'lib/t/requestable_api/user_endpoints.rb', line 65

def x_follower_ids(user = nil)
  user_id = user.nil? ? current_user_id : resolve_user_id(user)
  fetch_relationship_ids(user_id, "followers")
end

#x_friend_ids(user = nil) ⇒ Object



60
61
62
63
# File 'lib/t/requestable_api/user_endpoints.rb', line 60

def x_friend_ids(user = nil)
  user_id = user.nil? ? current_user_id : resolve_user_id(user)
  fetch_relationship_ids(user_id, "following")
end

#x_friendship?(user1, user2) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/t/requestable_api/user_endpoints.rb', line 54

def x_friendship?(user1, user2)
  user1_id = resolve_user_id(user1)
  user2_id = resolve_user_id(user2)
  fetch_relationship_ids(user1_id, "following").include?(user2_id.to_s)
end

#x_user(user = nil, _opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/t/requestable_api/user_endpoints.rb', line 10

def x_user(user = nil, _opts = {}, &)
  if block_given? && user.nil?
    @requestable_api_before_request&.call
    x_home_timeline(count: 100).each(&)
    return
  end

  fetch_single_user(user)
rescue X::ServiceUnavailable
  t_get_v1("users/show.json", screen_name: strip_at(user.to_s))
end

#x_user_search(query, page:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/t/requestable_api/user_endpoints.rb', line 37

def x_user_search(query, page:)
  page = page.to_i
  return [] if page > 1 && @requestable_api_user_search_tokens[[query, page - 1]].to_s.empty?

  params = {
    query: query.to_s,
    max_results: "100",
    "user.fields": V2_USER_FIELDS,
    expansions: V2_USER_EXPANSIONS,
    "tweet.fields": V2_TWEET_FIELDS,
  }
  params[:next_token] = @requestable_api_user_search_tokens[[query, page - 1]] if page > 1
  response = t_get_v2("users/search", params)
  @requestable_api_user_search_tokens[[query, page]] = response.dig("meta", "next_token")
  extract_users(response)
end

#x_users(users) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/t/requestable_api/user_endpoints.rb', line 22

def x_users(users)
  users = Array(users).flatten.compact
  return [] if users.empty?

  ids, names = users.partition { |entry| numeric_identifier?(entry) }
  results = []
  ids.each_slice(100) do |chunk|
    results.concat(extract_users(t_get_v2("users", user_lookup_params.merge(ids: chunk.join(",")))))
  end
  names.each_slice(100) do |chunk|
    results.concat(extract_users(t_get_v2("users/by", user_lookup_params.merge(usernames: chunk.map { |name| strip_at(name) }.join(",")))))
  end
  results
end

#x_verify_credentialsObject



4
5
6
7
8
# File 'lib/t/requestable_api/user_endpoints.rb', line 4

def x_verify_credentials
  extract_users(t_get_v2("users/me", user_lookup_params)).first || {}
rescue X::Error
  t_get_v1("account/verify_credentials.json")
end