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

#buildQueryString, #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
# 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"
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url.



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

def redirect_url
  @redirect_url
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#auth(code) ⇒ Object



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

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

#blockUser(username, target) ⇒ Object



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

def blockUser(username, target)
  return false if !@access_token
  path = "/users/#{username}/blocks/#{target}?oauth_token=#{@access_token}"
  url = @base_url + path
  put(url)
end

#editChannel(channel, status, game) ⇒ Object

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



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/twitch/client.rb', line 101

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

#followChannel(username, channel) ⇒ Object



121
122
123
124
125
126
# File 'lib/twitch/client.rb', line 121

def followChannel(username, channel)
  return false if !@access_token
  path = "/users/#{username}/follows/channels/#{channel}?oauth_token=#{@access_token}"
  url = @base_url + path
  put(url)
end

#getBadges(channel) ⇒ Object



294
295
296
297
298
# File 'lib/twitch/client.rb', line 294

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

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

Blocks



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

def getBlocks(username, options = {})
  query = buildQueryString(options)
  path = "/users/#{username}/blocks?oauth_token=#{@access_token}"
  url = @base_url + path + query
  get(url)
end

#getChannel(channel) ⇒ Object

Channel



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

def getChannel(channel)
  path = "/channels/"
  url = @base_url + path + channel;
  get(url)
end

#getChannelTeams(channel) ⇒ Object



144
145
146
147
148
149
# File 'lib/twitch/client.rb', line 144

def getChannelTeams(channel)
  return false if !@access_token
  path = "/channels/#{channel}/teams?oauth_token=#{@access_token}"
  url = @base_url + path;
  get(url)
end

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

Videos



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

def getChannelVideos(channel, options = {})
  query = buildQueryString(options)
  path = "/channels/#{channel}/videos"
  url = @base_url + path + query
  get(url)
end

Chat



288
289
290
291
292
# File 'lib/twitch/client.rb', line 288

def getChatLinks(channel)
  path = "/chat/"
  url = @base_url + path + channel;
  get(url)
end

#getEditors(channel) ⇒ Object



93
94
95
96
97
98
# File 'lib/twitch/client.rb', line 93

def getEditors(channel)
  return false if !@access_token
  path = "/channels/#{channel}/editors?oauth_token=#{@access_token}"
  url = @base_url + path;
  get(url)
end

#getEmoticonsObject



300
301
302
303
304
# File 'lib/twitch/client.rb', line 300

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

#getFeaturedStreams(options = {}) ⇒ Object



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

def getFeaturedStreams(options = {})
  query = buildQueryString(options)
  path = "/streams/featured"
  url = @base_url + path + query
  get(url)
end

#getFollowed(username) ⇒ Object



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

def getFollowed(username)
  path = "/users/#{username}/follows/channels"
  url = @base_url + path;
  get(url)
end

#getFollowing(channel) ⇒ Object

Follows



308
309
310
311
312
# File 'lib/twitch/client.rb', line 308

def getFollowing(channel)
  path = "/channels/#{channel}/follows"
  url = @base_url + path;
  get(url)
end

#getFollowStatus(username, channel) ⇒ Object



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

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

#getIngestsObject

Ingests



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

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


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

def getLink
  scope = ""
  @scope.each { |s| scope += s + '+' }
  link = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{scope}"
end

#getRootObject

Root



336
337
338
339
340
# File 'lib/twitch/client.rb', line 336

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

#getStream(stream_name) ⇒ Object

Streams



153
154
155
156
157
# File 'lib/twitch/client.rb', line 153

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

#getStreams(options = {}) ⇒ Object



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

def getStreams(options = {})
  query = buildQueryString(options)
  path = "/streams"
  url =  @base_url + path + query
  get(url)
end

#getSubscribed(channel) ⇒ Object

Subscriptions



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

def getSubscribed(channel)
  return false if !@access_token
  path = "/channels/#{channel}/subscriptions?oauth_token=#{@access_token}"
  url = @base_url + path
  get(url)
end

#getSummeraizedStreams(options = {}) ⇒ Object



179
180
181
182
183
184
# File 'lib/twitch/client.rb', line 179

def getSummeraizedStreams(options = {})
  query = buildQueryString(options)
  path = "/streams/summary"
  url = @base_url + path + query
  get(url)
end

#getTeam(team_id) ⇒ Object



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

def getTeam(team_id)
  path = "/teams/"
  url = @base_url + path + team_id;
  get(url)
end

#getTeamsObject

Teams



65
66
67
68
69
# File 'lib/twitch/client.rb', line 65

def getTeams
  path = "/teams/"
  url = @base_url + path;
  get(url)
end

#getTopGames(options = {}) ⇒ Object

Games



196
197
198
199
200
201
# File 'lib/twitch/client.rb', line 196

def getTopGames(options = {})
  query = buildQueryString(options)
  path = "/games/top"
  url = @base_url + path + query
  get(url)
end

#getTopVideos(options = {}) ⇒ Object



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

def getTopVideos(options = {})
  query = buildQueryString(options)
  path = "/videos/top"
  url = @base_url + path + query
  get(url)
end

#getUser(user) ⇒ Object

User



50
51
52
53
54
# File 'lib/twitch/client.rb', line 50

def getUser(user)
  path = "/users/"
  url = @base_url + path + user;
  get(url)
end

#getVideo(video_id) ⇒ Object



235
236
237
238
239
# File 'lib/twitch/client.rb', line 235

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

#getYourChannelObject



86
87
88
89
90
91
# File 'lib/twitch/client.rb', line 86

def getYourChannel
  return false if !@access_token
  path = "/channel?oauth_token=#{@access_token}"
  url = @base_url + path;
  get(url)
end

#getYourFollowedStreams(options = {}) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/twitch/client.rb', line 186

def getYourFollowedStreams(options = {})
  return false if !@access_token
  query = buildQueryString(options)
  path = "/streams/followed?oauth_token=#{@access_token}"
  url = @base_url + path + query
  get(url)
end

#getYourFollowedVideos(options = {}) ⇒ Object



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

def getYourFollowedVideos(options ={})
  return false if !@access_token
  query = buildQueryString(options)
  path = "/videos/followed?oauth_token=#{@access_token}"
  url = @base_url + path + query
  get(url)
end

#getYourUserObject



56
57
58
59
60
61
# File 'lib/twitch/client.rb', line 56

def getYourUser
  return false if !@access_token
  path = "/user?oauth_token=#{@access_token}"
  url = @base_url + path
  get(url)
end

#isSubscribed(username, channel, options = {}) ⇒ Object



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

def isSubscribed(username, channel, options = {})
  query = buildQueryString(options)
  path = "/users/#{username}/subscriptions/#{channel}?oauth_token=#{@access_token}"
  url = @base_url + path + query
  get(url)
end

#isSubscribedToChannel(username, channel) ⇒ Object



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

def isSubscribedToChannel(username, channel)
  return false if !@access_token
  path = "/channels/#{channel}/subscriptions/#{username}?oauth_token=#{@access_token}"
  url = @base_url + path
  get(url)
end

#resetKey(channel) ⇒ Object



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

def resetKey(channel)
  return false if !@access_token
  path = "/channels/#{channel}/stream_key?oauth_token=#{@access_token}"
  url = @base_url + path
  delete(url)
end

#runCommercial(channel, length = 30) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/twitch/client.rb', line 135

def runCommercial(channel, length = 30)
  return false if !@access_token
  path = "/channels/#{channel}/commercial?oauth_token=#{@access_token}"
  url = @base_url + path
  post(url, {
    :length => length
  })
end

#searchChannels(options = {}) ⇒ Object

Search



205
206
207
208
209
210
# File 'lib/twitch/client.rb', line 205

def searchChannels(options = {})
  query = buildQueryString(options)
  path = "/search/channels"
  url = @base_url + path + query
  get(url)
end

#searchGames(options = {}) ⇒ Object



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

def searchGames(options = {})
  query = buildQueryString(options)
  path = "/search/games"
  url = @base_url + path + query
  get(url)
end

#searchStreams(options = {}) ⇒ Object



212
213
214
215
216
217
# File 'lib/twitch/client.rb', line 212

def searchStreams(options = {})
  query = buildQueryString(options)
  path = "/search/streams"
  url = @base_url + path + query
  get(url)
end

#unblockUser(username, target) ⇒ Object



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

def unblockUser(username, target)
  return false if !@access_token
  path = "/users/#{username}/blocks/#{target}?oauth_token=#{@access_token}"
  url = @base_url + path
  delete(url)
end