Class: Twitch::UsersResource

Inherits:
Resource show all
Defined in:
lib/twitch/resources/users.rb

Instance Attribute Summary

Attributes inherited from Resource

#client

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Twitch::Resource

Instance Method Details

#authorization(id: nil, ids: nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/twitch/resources/users.rb', line 95

def authorization(id: nil, ids: nil)
  raise "Either id or ids is required" unless !id.nil? || !ids.nil?

  if id
    response = get_request("authorization/users", params: { user_id: id })
  elsif ids
    response = get_request("authorization/users", params: { user_id: ids })
  end

  Collection.from_response(response, type: UserAuthorization)
end

#block_user(target_user_id:, **attributes) ⇒ Object

Required scope: user:manage:blocked_users



67
68
69
# File 'lib/twitch/resources/users.rb', line 67

def block_user(target_user_id:, **attributes)
  put_request("users/blocks?#{URI.encode_www_form(target_user_id: target_user_id)}", body: attributes)
end

#blocks(broadcaster_id:, **params) ⇒ Object

Required scope: user:read:blocked_users



61
62
63
64
# File 'lib/twitch/resources/users.rb', line 61

def blocks(broadcaster_id:, **params)
  response = get_request("users/blocks", params: params.merge(broadcaster_id: broadcaster_id))
  Collection.from_response(response, type: BlockedUser)
end

#emotes(user_id:, **params) ⇒ Object



89
90
91
92
93
# File 'lib/twitch/resources/users.rb', line 89

def emotes(user_id:, **params)
  attrs = { user_id: user_id }
  response = get_request("chat/emotes/user", params: attrs.merge(params))
  Collection.from_response(response, type: Emote)
end

#following?(from_id:, to_id:) ⇒ Boolean

A quick method to see if a user is following a channel

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/twitch/resources/users.rb', line 77

def following?(from_id:, to_id:)
  warn "`users.following?` is deprecated. Use `channels.followers` or `channels.following` instead."

  response = get_request("users/follows", params: { from_id: from_id, to_id: to_id })

  if response.body["data"].empty?
    false
  else
    true
  end
end

#follows(**params) ⇒ Object

Deprecated.



51
52
53
54
55
56
57
58
# File 'lib/twitch/resources/users.rb', line 51

def follows(**params)
  warn "`users.follows` is deprecated. Use `channels.followers` or `channels.following` instead."

  raise "from_id or to_id is required" unless !params[:from_id].nil? || !params[:to_id].nil?

  response = get_request("users/follows", params: params)
  Collection.from_response(response, type: FollowedUser)
end

#get_color(user_id: nil, user_ids: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twitch/resources/users.rb', line 31

def get_color(user_id: nil, user_ids: nil)
  if user_ids
    ids = user_ids.is_a?(Array) ? user_ids : user_ids.split(",")
    query = URI.encode_www_form(ids.map { |i| [ "user_id", i.to_s.strip ] })
    response = get_request("chat/color?#{query}")
    Collection.from_response(response, type: UserColor)
  else
    response = get_request("chat/color", params: { user_id: user_id })
    UserColor.new response.body.dig("data")[0]
  end
end

#retrieve(id: nil, ids: nil, username: nil, usernames: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twitch/resources/users.rb', line 3

def retrieve(id: nil, ids: nil, username: nil, usernames: nil)
  raise "Either id, ids, username or usernames is required" unless !id.nil? || !ids.nil? || !username.nil? || !usernames.nil?

  if id
    response = get_request("users", params: { id: id })
  elsif ids
    response = get_request("users", params: { id: ids })
  elsif usernames
    response = get_request("users", params: { login: usernames })
  else
    response = get_request("users", params: { login: username })
  end

  body = response.body.dig("data")
  if id || username
    body.empty? ? nil : User.new(body[0])
  else
    Collection.from_response(response, type: User)
  end
end

#unblock_user(target_user_id:) ⇒ Object

Required scope: user:manage:blocked_users



72
73
74
# File 'lib/twitch/resources/users.rb', line 72

def unblock_user(target_user_id:)
  delete_request("users/blocks", params: { target_user_id: target_user_id })
end

#update(description:) ⇒ Object

Updates the current users description Required scope: user:edit



26
27
28
29
# File 'lib/twitch/resources/users.rb', line 26

def update(description:)
  response = put_request("users", body: { description: description })
  User.new response.body.dig("data")[0]
end

#update_color(user_id:, color:) ⇒ Object

Update a user's color Required scope: user:manage:chat_color user_id must be the currently authenticated user



46
47
48
# File 'lib/twitch/resources/users.rb', line 46

def update_color(user_id:, color:)
  put_request("chat/color?#{URI.encode_www_form(user_id: user_id, color: color)}", body: {})
end