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



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

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



69
70
71
# File 'lib/twitch/resources/users.rb', line 69

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

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

Required scope: user:read:blocked_users



63
64
65
66
# File 'lib/twitch/resources/users.rb', line 63

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

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



91
92
93
94
95
# File 'lib/twitch/resources/users.rb', line 91

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)


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

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.



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

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



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

def get_color(user_id: nil, user_ids: nil)
  if user_ids != nil
    users = user_ids.split(",").map { |i| "user_id=#{i.strip}" }.join("&")
    puts "chat/color?#{users}"
    response = get_request("chat/color?#{users}")
    Collection.from_response(response, type: UserColor)
  else
    response = get_request("chat/color?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
23
24
# 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.count == 1
    User.new body[0]
  elsif ids || usernames && body.count > 1
    Collection.from_response(response, type: User)
  else
    nil
  end
end

#unblock_user(target_user_id:) ⇒ Object

Required scope: user:manage:blocked_users



74
75
76
# File 'lib/twitch/resources/users.rb', line 74

def unblock_user(target_user_id:)
  delete_request("users/blocks?target_user_id=#{target_user_id}")
end

#update(description:) ⇒ Object

Updates the current users description Required scope: user:edit



28
29
30
31
# File 'lib/twitch/resources/users.rb', line 28

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



48
49
50
# File 'lib/twitch/resources/users.rb', line 48

def update_color(user_id:, color:)
  put_request("chat/color?user_id=#{user_id}&color=#{color}", body: {})
end