twitch 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1aaa6641eba687a3e3b10c7bf9f7966c4795fa5f
4
- data.tar.gz: 11d1d90915327fd4d367af1057b8f5593d2c74c4
3
+ metadata.gz: 95f3eeb91c1f1d1fe9e7617efc34e39e559f7faf
4
+ data.tar.gz: 24ef1ede7142906bdbc003e897a329a0bef8845f
5
5
  SHA512:
6
- metadata.gz: 46bc8ea7559f759f1f5f88adee2917ea9ce728c540d3e00dce7de4cb3eb195fe8a866cd809c2ff4e7e8a98180ea5ced175449d786e84b5a73bc50c12937f665d
7
- data.tar.gz: 018060e0542c44653e2eaad7d86c08c8b9704512ac57959e91f7b3653e2f813675625ad1889863d3b8c4214a2e5dafb28d0a6717ff91182d9b2cc01a2a3dc0d7
6
+ metadata.gz: 008d286a0df1ad05cdf3405d184c174cbd9c3424eaee6dec9ab84bfde59190da36a260cf7c164b6ca18c7a8e74e26bcd0d0836035613888f2a2b1660f44069da
7
+ data.tar.gz: 3e508a0606d788e2a35531b147f754022256a8206e30e6858a607380e6c45f9e4f695f5946258c9557fb19be0d6aec72abdbc04f8e969fbebdd7aba15dbd4451
@@ -1,226 +1,10 @@
1
- require "httparty"
2
-
3
- class Twitch
4
- def initialize(options = {})
5
- @client_id = options[:client_id] || nil
6
- @secret_key = options[:secret_key] || nil
7
- @redirect_uri = options[:redirect_uri] || nil
8
- @scope = options[:scope] || nil
9
- @access_token = options[:access_token] || nil
10
-
11
- @base_url = "https://api.twitch.tv/kraken"
12
- end
13
-
14
- public
15
-
16
- def getLink
17
- scope = ""
18
- @scope.each { |s| scope += s + '+' }
19
- link = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{scope}"
20
- end
21
-
22
- def auth(code)
23
- path = "/oauth2/token"
24
- url = @base_url + path
25
- post(url, {
26
- :client_id => @client_id,
27
- :client_secret => @secret_key,
28
- :grant_type => "authorization_code",
29
- :redirect_uri => @redirect_uri,
30
- :code => code
31
- })
32
- end
33
-
34
- # User
35
-
36
- def getUser(user)
37
- path = "/users/"
38
- url = @base_url + path + user;
39
- get(url)
40
- end
41
-
42
- def getYourUser
43
- return false if !@access_token
44
- path = "/user?oauth_token=#{@access_token}"
45
- url = @base_url + path
46
- get(url)
47
- end
48
-
49
- # Teams
50
-
51
- def getTeams
52
- path = "/teams/"
53
- url = @base_url + path;
54
- get(url)
55
- end
56
-
57
-
58
- def getTeam(team_id)
59
- path = "/teams/"
60
- url = @base_url + path + team_id;
61
- get(url)
62
- end
63
-
64
- # Channel
65
-
66
- def getChannel(channel)
67
- path = "/channels/"
68
- url = @base_url + path + channel;
69
- get(url)
70
- end
71
-
72
- def getYourChannel
73
- return false if !@access_token
74
- path = "/channel?oauth_token=#{@access_token}"
75
- url = @base_url + path;
76
- get(url)
77
- end
78
-
79
- def editChannel(status, game)
80
- return false if !@access_token
81
- path = "/channels/dustinlakin/?oauth_token=#{@access_token}"
82
- url = @base_url + path
83
- data = {
84
- :channel =>{
85
- :game => game,
86
- :status => status
87
- }
88
- }
89
- put(url, data)
90
- end
91
-
92
- def followChannel(username, channel)
93
- return false if !@access_token
94
- path = "/users/#{username}/follows/channels/#{channel}?oauth_token=#{@access_token}"
95
- url = @base_url + path
96
- put(url)
97
- end
98
-
99
- def runCommercial(channel, length = 30)
100
- return false if !@access_token
101
- path = "/channels/#{channel}/commercial?oauth_token=#{@access_token}"
102
- url = @base_url + path
103
- post(url, {
104
- :length => length
105
- })
106
- end
107
-
108
- # Streams
109
-
110
- def getStream(stream_name)
111
- path = "/stream/#{stream_name}"
112
- url = @base_url + path;
113
- get(url)
114
- end
115
-
116
- def getStream(stream_name)
117
- path = "/streams/#{stream_name}"
118
- url = @base_url + path;
119
- get(url)
120
- end
121
-
122
- def getStreams(options = {})
123
- query = buildQueryString(options)
124
- path = "/streams"
125
- url = @base_url + path + query
126
- get(url)
127
- end
128
-
129
- def getFeaturedStreams(options = {})
130
- query = buildQueryString(options)
131
- path = "/streams/featured"
132
- url = @base_url + path + query
133
- get(url)
134
- end
135
-
136
- def getSummeraizedStreams(options = {})
137
- query = buildQueryString(options)
138
- path = "/streams/summary"
139
- url = @base_url + path + query
140
- get(url)
141
- end
142
-
143
- def getYourFollowedStreams
144
- path = "/streams/followed?oauth_token=#{@access_token}"
145
- url = @base_url + path
146
- get(url)
147
- end
148
-
149
- #Games
150
-
151
- def getTopGames(options = {})
152
- query = buildQueryString(options)
153
- path = "/games/top"
154
- url = @base_url + path + query
155
- get(url)
156
- end
157
-
158
- #Search
159
-
160
- def searchStreams(options = {})
161
- query = buildQueryString(options)
162
- path = "/search/streams"
163
- url = @base_url + path + query
164
- get(url)
165
- end
166
-
167
- def searchGames(options = {})
168
- query = buildQueryString(options)
169
- path = "/search/games"
170
- url = @base_url + path + query
171
- get(url)
172
- end
173
-
174
- # Videos
175
-
176
- def getChannelVideos(channel, options = {})
177
- query = buildQueryString(options)
178
- path = "/channels/#{channel}/videos"
179
- url = @base_url + path + query
180
- get(url)
181
- end
182
-
183
- def getVideo(video_id)
184
- path = "/videos/#{video_id}/"
185
- url = @base_url + path
186
- get(url)
187
- end
188
-
189
- def isSubscribed(username, channel, options = {})
190
- query = buildQueryString(options)
191
- path = "/users/#{username}/subscriptions/#{channel}?oauth_token=#{@access_token}"
192
- url = @base_url + path + query
193
- get(url)
1
+ require 'twitch/adapters'
2
+ require 'twitch/client'
3
+ require 'twitch/request'
4
+ require 'twitch/version'
5
+
6
+ module Twitch
7
+ def self.new(options={})
8
+ Twitch::Client.new(options)
194
9
  end
195
-
196
- private
197
- def buildQueryString(options)
198
- query = "?"
199
- options.each do |key, value|
200
- query += "#{key}=#{value.to_s.gsub(" ", "+")}&"
201
- end
202
- query = query[0...-1]
203
- end
204
-
205
- def post(url, data)
206
- response_data = HTTParty.post(url, :body => data)
207
- {
208
- :body => response_data,
209
- :response => response_data.code
210
- }
211
- end
212
-
213
- def get(url)
214
- c = HTTParty.get(url)
215
- {:body => c, :response => c.code}
216
- end
217
-
218
- def put(url, data)
219
- c = HTTParty.put(url, :body => data, :headers => {
220
- 'Accept' => 'application/json',
221
- 'Content-Type' => 'application/json',
222
- 'Api-Version' => '2.2'
223
- })
224
- {:body => c, :response => c.code}
225
- end
226
10
  end
@@ -0,0 +1,18 @@
1
+ require 'twitch/adapters/base_adapter'
2
+ require 'twitch/adapters/httparty_adapter'
3
+
4
+ module Twitch
5
+ module Adapters
6
+ DEFAULT_ADAPTER = Twitch::Adapters::HTTPartyAdapter
7
+
8
+ def get_adapter(adapter, default_adapter = DEFAULT_ADAPTER)
9
+ begin
10
+ Twitch::Adapters.const_defined?(adapter.to_s)
11
+ rescue
12
+ default_adapter
13
+ else
14
+ adapter
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module Twitch
2
+ module Adapters
3
+ class BaseAdapter
4
+ def self.get(url)
5
+ request(:get, url)
6
+ end
7
+
8
+ def self.post(url, options)
9
+ request(:post, url, options)
10
+ end
11
+
12
+ def self.put(url, options)
13
+ request(:put, url, options)
14
+ end
15
+
16
+ def self.delete(url)
17
+ request(:delete, url)
18
+ end
19
+
20
+ def self.request(method, url, options)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'httparty'
2
+
3
+ module Twitch
4
+ module Adapters
5
+ class HTTPartyAdapter < BaseAdapter
6
+
7
+ def self.request(method, url, options={})
8
+ res = HTTParty.send(method, url, options)
9
+ {:body => res, :response => res.code}
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,358 @@
1
+ require 'twitch/request'
2
+ require 'twitch/adapters'
3
+
4
+ module Twitch
5
+ class Client
6
+ include Twitch::Request
7
+ include Twitch::Adapters
8
+
9
+ def initialize(options = {})
10
+ @client_id = options[:client_id] || nil
11
+ @secret_key = options[:secret_key] || nil
12
+ @redirect_uri = options[:redirect_uri] || nil
13
+ @scope = options[:scope] || nil
14
+ @access_token = options[:access_token] || nil
15
+
16
+ @adapter = get_adapter(options[:adapter] || nil)
17
+
18
+ @base_url = "https://api.twitch.tv/kraken"
19
+ end
20
+
21
+ attr_reader :base_url, :redirect_url, :scope
22
+ attr_accessor :adapter
23
+
24
+ public
25
+
26
+ def adapter=(adapter)
27
+ get_adapter(adapter)
28
+ end
29
+
30
+ def getLink
31
+ scope = ""
32
+ @scope.each { |s| scope += s + '+' }
33
+ link = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{scope}"
34
+ end
35
+
36
+ def auth(code)
37
+ path = "/oauth2/token"
38
+ url = @base_url + path
39
+ post(url, {
40
+ :client_id => @client_id,
41
+ :client_secret => @secret_key,
42
+ :grant_type => "authorization_code",
43
+ :redirect_uri => @redirect_uri,
44
+ :code => code
45
+ })
46
+ end
47
+
48
+ # User
49
+
50
+ def getUser(user)
51
+ path = "/users/"
52
+ url = @base_url + path + user;
53
+ get(url)
54
+ end
55
+
56
+ def getYourUser
57
+ return false if !@access_token
58
+ path = "/user?oauth_token=#{@access_token}"
59
+ url = @base_url + path
60
+ get(url)
61
+ end
62
+
63
+ # Teams
64
+
65
+ def getTeams
66
+ path = "/teams/"
67
+ url = @base_url + path;
68
+ get(url)
69
+ end
70
+
71
+
72
+ def getTeam(team_id)
73
+ path = "/teams/"
74
+ url = @base_url + path + team_id;
75
+ get(url)
76
+ end
77
+
78
+ # Channel
79
+
80
+ def getChannel(channel)
81
+ path = "/channels/"
82
+ url = @base_url + path + channel;
83
+ get(url)
84
+ end
85
+
86
+ def getYourChannel
87
+ return false if !@access_token
88
+ path = "/channel?oauth_token=#{@access_token}"
89
+ url = @base_url + path;
90
+ get(url)
91
+ end
92
+
93
+ def getEditors(channel)
94
+ return false if !@access_token
95
+ path = "/channels/#{channel}/editors?oauth_token=#{@access_token}"
96
+ url = @base_url + path;
97
+ get(url)
98
+ end
99
+
100
+ # TODO: Add ability to set delay, which is only available for partered channels
101
+ def editChannel(channel, status, game)
102
+ return false if !@access_token
103
+ path = "/channels/#{channel}/?oauth_token=#{@access_token}"
104
+ url = @base_url + path
105
+ data = {
106
+ :channel =>{
107
+ :game => game,
108
+ :status => status
109
+ }
110
+ }
111
+ put(url, data)
112
+ end
113
+
114
+ def resetKey(channel)
115
+ return false if !@access_token
116
+ path = "/channels/#{channel}/stream_key?oauth_token=#{@access_token}"
117
+ url = @base_url + path
118
+ delete(url)
119
+ end
120
+
121
+ def followChannel(username, channel)
122
+ return false if !@access_token
123
+ path = "/users/#{username}/follows/channels/#{channel}?oauth_token=#{@access_token}"
124
+ url = @base_url + path
125
+ put(url)
126
+ end
127
+
128
+ def followChannel(username, channel)
129
+ return false if !@access_token
130
+ path = "/users/#{username}/follows/channels/#{channel}?oauth_token=#{@access_token}"
131
+ url = @base_url + path
132
+ delete(url)
133
+ end
134
+
135
+ def runCommercial(channel, length = 30)
136
+ return false if !@access_token
137
+ path = "/channels/#{channel}/commercial?oauth_token=#{@access_token}"
138
+ url = @base_url + path
139
+ post(url, {
140
+ :length => length
141
+ })
142
+ end
143
+
144
+ def getChannelTeams(channel)
145
+ return false if !@access_token
146
+ path = "/channels/#{channel}/teams?oauth_token=#{@access_token}"
147
+ url = @base_url + path;
148
+ get(url)
149
+ end
150
+
151
+ # Streams
152
+
153
+ def getStream(stream_name)
154
+ path = "/stream/#{stream_name}"
155
+ url = @base_url + path;
156
+ get(url)
157
+ end
158
+
159
+ def getStream(stream_name)
160
+ path = "/streams/#{stream_name}"
161
+ url = @base_url + path;
162
+ get(url)
163
+ end
164
+
165
+ def getStreams(options = {})
166
+ query = buildQueryString(options)
167
+ path = "/streams"
168
+ url = @base_url + path + query
169
+ get(url)
170
+ end
171
+
172
+ def getFeaturedStreams(options = {})
173
+ query = buildQueryString(options)
174
+ path = "/streams/featured"
175
+ url = @base_url + path + query
176
+ get(url)
177
+ end
178
+
179
+ def getSummeraizedStreams(options = {})
180
+ query = buildQueryString(options)
181
+ path = "/streams/summary"
182
+ url = @base_url + path + query
183
+ get(url)
184
+ end
185
+
186
+ def getYourFollowedStreams(options = {})
187
+ return false if !@access_token
188
+ query = buildQueryString(options)
189
+ path = "/streams/followed?oauth_token=#{@access_token}"
190
+ url = @base_url + path + query
191
+ get(url)
192
+ end
193
+
194
+ #Games
195
+
196
+ def getTopGames(options = {})
197
+ query = buildQueryString(options)
198
+ path = "/games/top"
199
+ url = @base_url + path + query
200
+ get(url)
201
+ end
202
+
203
+ #Search
204
+
205
+ def searchChannels(options = {})
206
+ query = buildQueryString(options)
207
+ path = "/search/channels"
208
+ url = @base_url + path + query
209
+ get(url)
210
+ end
211
+
212
+ def searchStreams(options = {})
213
+ query = buildQueryString(options)
214
+ path = "/search/streams"
215
+ url = @base_url + path + query
216
+ get(url)
217
+ end
218
+
219
+ def searchGames(options = {})
220
+ query = buildQueryString(options)
221
+ path = "/search/games"
222
+ url = @base_url + path + query
223
+ get(url)
224
+ end
225
+
226
+ # Videos
227
+
228
+ def getChannelVideos(channel, options = {})
229
+ query = buildQueryString(options)
230
+ path = "/channels/#{channel}/videos"
231
+ url = @base_url + path + query
232
+ get(url)
233
+ end
234
+
235
+ def getVideo(video_id)
236
+ path = "/videos/#{video_id}/"
237
+ url = @base_url + path
238
+ get(url)
239
+ end
240
+
241
+ def isSubscribed(username, channel, options = {})
242
+ query = buildQueryString(options)
243
+ path = "/users/#{username}/subscriptions/#{channel}?oauth_token=#{@access_token}"
244
+ url = @base_url + path + query
245
+ get(url)
246
+ end
247
+
248
+ def getYourFollowedVideos(options ={})
249
+ return false if !@access_token
250
+ query = buildQueryString(options)
251
+ path = "/videos/followed?oauth_token=#{@access_token}"
252
+ url = @base_url + path + query
253
+ get(url)
254
+ end
255
+
256
+ def getTopVideos(options = {})
257
+ query = buildQueryString(options)
258
+ path = "/videos/top"
259
+ url = @base_url + path + query
260
+ get(url)
261
+ end
262
+
263
+ # Blocks
264
+
265
+ def getBlocks(username, options = {})
266
+ query = buildQueryString(options)
267
+ path = "/users/#{username}/blocks?oauth_token=#{@access_token}"
268
+ url = @base_url + path + query
269
+ get(url)
270
+ end
271
+
272
+ def blockUser(username, target)
273
+ return false if !@access_token
274
+ path = "/users/#{username}/blocks/#{target}?oauth_token=#{@access_token}"
275
+ url = @base_url + path
276
+ put(url)
277
+ end
278
+
279
+ def unblockUser(username, target)
280
+ return false if !@access_token
281
+ path = "/users/#{username}/blocks/#{target}?oauth_token=#{@access_token}"
282
+ url = @base_url + path
283
+ delete(url)
284
+ end
285
+
286
+ # Chat
287
+
288
+ def getChatLinks(channel)
289
+ path = "/chat/"
290
+ url = @base_url + path + channel;
291
+ get(url)
292
+ end
293
+
294
+ def getBadges(channel)
295
+ path = "/chat/#{channel}/badges"
296
+ url = @base_url + path;
297
+ get(url)
298
+ end
299
+
300
+ def getEmoticons()
301
+ path = "/chat/emoticons"
302
+ url = @base_url + path;
303
+ get(url)
304
+ end
305
+
306
+ # Follows
307
+
308
+ def getFollowing(channel)
309
+ path = "/channels/#{channel}/follows"
310
+ url = @base_url + path;
311
+ get(url)
312
+ end
313
+
314
+ def getFollowed(username)
315
+ path = "/users/#{username}/follows/channels"
316
+ url = @base_url + path;
317
+ get(url)
318
+ end
319
+
320
+ def getFollowStatus(username, channel)
321
+ path = "/users/#{username}/follows/channels/#{channel}/?oauth_token=#{@access_token}"
322
+ url = @base_url + path;
323
+ get(url)
324
+ end
325
+
326
+ # Ingests
327
+
328
+ def getIngests()
329
+ path = "/ingests"
330
+ url = @base_url + path
331
+ get(url)
332
+ end
333
+
334
+ # Root
335
+
336
+ def getRoot()
337
+ path = "/?oauth_token=#{@access_token}"
338
+ url = @base_url + path
339
+ get(url)
340
+ end
341
+
342
+ # Subscriptions
343
+
344
+ def getSubscribed(channel)
345
+ return false if !@access_token
346
+ path = "/channels/#{channel}/subscriptions?oauth_token=#{@access_token}"
347
+ url = @base_url + path
348
+ get(url)
349
+ end
350
+
351
+ def isSubscribedToChannel(username, channel)
352
+ return false if !@access_token
353
+ path = "/channels/#{channel}/subscriptions/#{username}?oauth_token=#{@access_token}"
354
+ url = @base_url + path
355
+ get(url)
356
+ end
357
+ end
358
+ end
@@ -0,0 +1,31 @@
1
+ module Twitch
2
+ module Request
3
+ def buildQueryString(options)
4
+ query = "?"
5
+ options.each do |key, value|
6
+ query += "#{key}=#{value.to_s.gsub(" ", "+")}&"
7
+ end
8
+ query = query[0...-1]
9
+ end
10
+
11
+ def get(url)
12
+ @adapter.get(url)
13
+ end
14
+
15
+ def post(url, data)
16
+ @adapter.post(url, :body => data)
17
+ end
18
+
19
+ def put(url, data={})
20
+ @adapter.put(url, :body => data, :headers => {
21
+ 'Accept' => 'application/json',
22
+ 'Content-Type' => 'application/json',
23
+ 'Api-Version' => '2.2'
24
+ })
25
+ end
26
+
27
+ def delete(url)
28
+ @adapter.delete(url)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module Twitch
3
+ module VERSION
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ TINY = 5
7
+
8
+ STRING = [MAJOR, MINOR, TINY].compact.join(".")
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Lakin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2015-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -58,6 +58,12 @@ executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
+ - lib/twitch/adapters/base_adapter.rb
62
+ - lib/twitch/adapters/httparty_adapter.rb
63
+ - lib/twitch/adapters.rb
64
+ - lib/twitch/client.rb
65
+ - lib/twitch/request.rb
66
+ - lib/twitch/version.rb
61
67
  - lib/twitch.rb
62
68
  homepage: https://github.com/dustinlakin/twitch-rb
63
69
  licenses: []