Class: Twitch::Client

Inherits:
Object
  • Object
show all
Includes:
Adapters, Request
Defined in:
lib/twitch/client.rb

Constant Summary

Constants included from Adapters

Adapters::DEFAULT_ADAPTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Adapters

#get_adapter

Methods included from Request

#build_query_string, #delete, #get, #post, #put

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/twitch/client.rb', line 9

def initialize(options = {})
  @client_id = options[:client_id] || nil
  @secret_key = options[:secret_key] || nil
  @redirect_uri = options[:redirect_uri] || nil
  @scope = options[:scope] || nil
  @access_token = options[:access_token] || nil

  @adapter = get_adapter(options[:adapter] || nil)

  @base_url = "https://api.twitch.tv/kraken"
  @alt_base_url = "https://api.twitch.tv/api"
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



23
24
25
# File 'lib/twitch/client.rb', line 23

def adapter
  @adapter
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



22
23
24
# File 'lib/twitch/client.rb', line 22

def base_url
  @base_url
end

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url.



22
23
24
# File 'lib/twitch/client.rb', line 22

def redirect_url
  @redirect_url
end

#scopeObject (readonly)

Returns the value of attribute scope.



22
23
24
# File 'lib/twitch/client.rb', line 22

def scope
  @scope
end

Instance Method Details

#auth(code) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/twitch/client.rb', line 37

def auth(code)
  path = "/oauth2/token"
  url = @base_url + path
  post(url, {
    :client_id => @client_id,
    :client_secret => @secret_key,
    :grant_type => "authorization_code",
    :redirect_uri => @redirect_uri,
    :code => code
  })
end

#badges(channel) ⇒ Object



345
346
347
348
349
350
# File 'lib/twitch/client.rb', line 345

def badges(channel)
  path = "/chat/#{channel}/badges"
  url = @base_url + path;

  get(url)
end

#block_user(username, target) ⇒ Object



320
321
322
323
324
325
326
# File 'lib/twitch/client.rb', line 320

def block_user(username, target)
  return false unless @access_token

  path = "/users/#{username}/blocks/#{target}?oauth_token=#{@access_token}"
  url = @base_url + path
  put(url)
end

#blocks(username, options = {}) ⇒ Object

Blocks



311
312
313
314
315
316
317
318
# File 'lib/twitch/client.rb', line 311

def blocks(username, options = {})
  options[:oauth_token] = @access_token
  query = build_query_string(options)
  path = "/users/#{username}/blocks"
  url = @base_url + path + query

  get(url)
end

#channel(channel = nil) ⇒ Object

Channel



88
89
90
91
92
93
94
95
# File 'lib/twitch/client.rb', line 88

def channel(channel = nil)
  return your_channel unless channel

  path = "/channels/"
  url = @base_url + path + channel;

  get(url)
end

#channel_panels(channel = nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/twitch/client.rb', line 97

def channel_panels(channel = nil)
  return nil if channel.nil?

  path = "/channels/#{channel}/panels"
  url = @alt_base_url + path;

  get(url)
end

#channel_teams(channel) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/twitch/client.rb', line 173

def channel_teams(channel)
  return false unless @access_token

  path = "/channels/#{channel}/teams?oauth_token=#{@access_token}"
  url = @base_url + path;

  get(url)
end

#channel_videos(channel, options = {}) ⇒ Object

Videos



265
266
267
268
269
270
271
# File 'lib/twitch/client.rb', line 265

def channel_videos(channel, options = {})
  query = build_query_string(options)
  path = "/channels/#{channel}/videos"
  url = @base_url + path + query

  get(url)
end

Chat



338
339
340
341
342
343
# File 'lib/twitch/client.rb', line 338

def chat_links(channel)
  path = "/chat/"
  url = @base_url + path + channel;

  get(url)
end

#edit_channel(channel, status, game) ⇒ Object

TODO: Add ability to set delay, which is only available for partered channels



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/twitch/client.rb', line 125

def edit_channel(channel, status, game)
  return false unless @access_token

  path = "/channels/#{channel}/?oauth_token=#{@access_token}"
  url = @base_url + path
  data = {
    :channel =>{
      :game => game,
      :status => status
    }
  }
  put(url, data)
end

#editors(channel) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/twitch/client.rb', line 115

def editors(channel)
  return false unless @access_token

  path = "/channels/#{channel}/editors?oauth_token=#{@access_token}"
  url = @base_url + path;

  get(url)
end

#emoticonsObject



352
353
354
355
356
357
# File 'lib/twitch/client.rb', line 352

def emoticons()
  path = "/chat/emoticons"
  url = @base_url + path;

  get(url)
end


199
200
201
202
203
204
205
# File 'lib/twitch/client.rb', line 199

def featured_streams(options = {})
  query = build_query_string(options)
  path = "/streams/featured"
  url = @base_url + path + query

  get(url)
end

#follow_channel(username, channel) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/twitch/client.rb', line 147

def follow_channel(username, channel)
  return false unless @access_token

  path = "/users/#{username}/follows/channels/#{channel}?oauth_token=#{@access_token}"
  url = @base_url + path
  put(url)
end

#follow_status(username, channel) ⇒ Object



377
378
379
380
381
382
# File 'lib/twitch/client.rb', line 377

def follow_status(username, channel)
  path = "/users/#{username}/follows/channels/#{channel}/?oauth_token=#{@access_token}"
  url = @base_url + path;

  get(url)
end

#followed(username, options = {}) ⇒ Object



369
370
371
372
373
374
375
# File 'lib/twitch/client.rb', line 369

def followed(username, options = {})
  query = build_query_string(options)
  path = "/users/#{username}/follows/channels"
  url = @base_url + path + query

  get(url)
end

#followed_streams(options = {}) ⇒ Object Also known as: your_followed_streams



215
216
217
218
219
220
221
222
223
224
# File 'lib/twitch/client.rb', line 215

def followed_streams(options = {})
  return false unless @access_token

  options[:oauth_token] = @access_token
  query = build_query_string(options)
  path = "/streams/followed"
  url = @base_url + path + query

  get(url)
end

#followed_videos(options = {}) ⇒ Object Also known as: your_followed_videos



289
290
291
292
293
294
295
296
297
298
# File 'lib/twitch/client.rb', line 289

def followed_videos(options ={})
  return false unless @access_token

  options[:oauth_token] = @access_token
  query = build_query_string(options)
  path = "/videos/followed"
  url = @base_url + path + query

  get(url)
end

#following(channel, options = {}) ⇒ Object

Follows



361
362
363
364
365
366
367
# File 'lib/twitch/client.rb', line 361

def following(channel, options = {})
  query = build_query_string(options)
  path = "/channels/#{channel}/follows"
  url = @base_url + path + query;

  get(url)
end

#ingestsObject

Ingests



386
387
388
389
390
391
# File 'lib/twitch/client.rb', line 386

def ingests()
  path = "/ingests"
  url = @base_url + path

  get(url)
end


31
32
33
34
35
# File 'lib/twitch/client.rb', line 31

def link
  scope = ""
  @scope.each { |s| scope += s + '+' }
  "#{@base_url}/oauth2/authorize?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{scope}"
end

#reset_key(channel) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/twitch/client.rb', line 139

def reset_key(channel)
  return false unless @access_token

  path = "/channels/#{channel}/stream_key?oauth_token=#{@access_token}"
  url = @base_url + path
  delete(url)
end

#rootObject

Root



395
396
397
398
399
400
# File 'lib/twitch/client.rb', line 395

def root()
  path = "/?oauth_token=#{@access_token}"
  url = @base_url + path

  get(url)
end

#run_commercial(channel, length = 30) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/twitch/client.rb', line 163

def run_commercial(channel, length = 30)
  return false unless @access_token

  path = "/channels/#{channel}/commercial?oauth_token=#{@access_token}"
  url = @base_url + path
  post(url, {
    :length => length
  })
end

#search_channels(options = {}) ⇒ Object

Search



239
240
241
242
243
244
245
# File 'lib/twitch/client.rb', line 239

def search_channels(options = {})
  query = build_query_string(options)
  path = "/search/channels"
  url = @base_url + path + query

  get(url)
end

#search_games(options = {}) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/twitch/client.rb', line 255

def search_games(options = {})
  query = build_query_string(options)
  path = "/search/games"
  url = @base_url + path + query

  get(url)
end

#search_streams(options = {}) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/twitch/client.rb', line 247

def search_streams(options = {})
  query = build_query_string(options)
  path = "/search/streams"
  url = @base_url + path + query

  get(url)
end

#stream(stream_name) ⇒ Object

Streams



184
185
186
187
188
189
# File 'lib/twitch/client.rb', line 184

def stream(stream_name)
  path = "/streams/#{stream_name}"
  url = @base_url + path;

  get(url)
end

#streams(options = {}) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/twitch/client.rb', line 191

def streams(options = {})
  query = build_query_string(options)
  path = "/streams"
  url =  @base_url + path + query

  get(url)
end

#subscribed(channel, options = {}) ⇒ Object

Subscriptions



404
405
406
407
408
409
410
411
412
413
# File 'lib/twitch/client.rb', line 404

def subscribed(channel, options = {})
  return false unless @access_token
  options[:oauth_token] = @access_token

  query = build_query_string(options)
  path = "/channels/#{channel}/subscriptions"
  url = @base_url + path + query

  get(url)
end

#subscribed?(username, channel, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
283
284
285
286
287
# File 'lib/twitch/client.rb', line 280

def subscribed?(username, channel, options = {})
  options[:oauth_token] = @access_token
  query = build_query_string(options)
  path = "/users/#{username}/subscriptions/#{channel}"
  url = @base_url + path + query

  get(url)
end

#subscribed_to_channel(username, channel) ⇒ Object



415
416
417
418
419
420
421
422
# File 'lib/twitch/client.rb', line 415

def subscribed_to_channel(username, channel)
  return false unless @access_token

  path = "/channels/#{channel}/subscriptions/#{username}?oauth_token=#{@access_token}"
  url = @base_url + path

  get(url)
end

#summarized_streams(options = {}) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/twitch/client.rb', line 207

def summarized_streams(options = {})
  query = build_query_string(options)
  path = "/streams/summary"
  url = @base_url + path + query

  get(url)
end

#team(team_id) ⇒ Object



79
80
81
82
83
84
# File 'lib/twitch/client.rb', line 79

def team(team_id)
  path = "/teams/"
  url = @base_url + path + team_id;

  get(url)
end

#teamsObject

Teams



71
72
73
74
75
76
# File 'lib/twitch/client.rb', line 71

def teams
  path = "/teams/"
  url = @base_url + path;

  get(url)
end

#top_games(options = {}) ⇒ Object

Games



229
230
231
232
233
234
235
# File 'lib/twitch/client.rb', line 229

def top_games(options = {})
  query = build_query_string(options)
  path = "/games/top"
  url = @base_url + path + query

  get(url)
end

#top_videos(options = {}) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/twitch/client.rb', line 301

def top_videos(options = {})
  query = build_query_string(options)
  path = "/videos/top"
  url = @base_url + path + query

  get(url)
end

#unblock_user(username, target) ⇒ Object



328
329
330
331
332
333
334
# File 'lib/twitch/client.rb', line 328

def unblock_user(username, target)
  return false unless @access_token

  path = "/users/#{username}/blocks/#{target}?oauth_token=#{@access_token}"
  url = @base_url + path
  delete(url)
end

#unfollow_channel(username, channel) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/twitch/client.rb', line 155

def unfollow_channel(username, channel)
  return false unless @access_token

  path = "/users/#{username}/follows/channels/#{channel}?oauth_token=#{@access_token}"
  url = @base_url + path
  delete(url)
end

#user(user = nil) ⇒ Object

User



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

def user(user = nil)
  return your_user unless user

  path = "/users/"
  url = @base_url + path + user;

  get(url)
end

#video(video_id) ⇒ Object



273
274
275
276
277
278
# File 'lib/twitch/client.rb', line 273

def video(video_id)
  path = "/videos/#{video_id}/"
  url = @base_url + path

  get(url)
end

#your_channelObject



106
107
108
109
110
111
112
113
# File 'lib/twitch/client.rb', line 106

def your_channel
  return false unless @access_token

  path = "/channel?oauth_token=#{@access_token}"
  url = @base_url + path;

  get(url)
end

#your_userObject



60
61
62
63
64
65
66
67
# File 'lib/twitch/client.rb', line 60

def your_user
  return false unless @access_token

  path = "/user?oauth_token=#{@access_token}"
  url = @base_url + path

  get(url)
end