twitch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/twitch.rb +223 -0
  2. metadata +75 -0
@@ -0,0 +1,223 @@
1
+ require "curb"
2
+ require "json"
3
+
4
+ class Twitch
5
+ def initialize(options = {})
6
+ @client_id = options[:client_id] || nil
7
+ @secret_key = options[:secret_key] || nil
8
+ @redirect_uri = options[:redirect_uri] || nil
9
+ @scope = options[:scope] || nil
10
+ @access_token = options[:access_token] || nil
11
+
12
+ @base_url = "https://api.twitch.tv/kraken"
13
+ end
14
+
15
+ public
16
+
17
+ def setTestingDefaults
18
+ @client_id = "k96gsxbp95dgpv9ck8wnzqcyhifqxv5"
19
+ @secret_key = "9is2azmi3iw5r29ay7d8gvl4u4feeyg"
20
+ @redirect_uri = "http://localhost:3000/auth"
21
+ @scope = ["user_red", "channel_read", "channel_editor", "channel_commercial", "channel_stream", "user_blocks_edit"]
22
+ @access_token = "1d6lcvunb152ccoxlzuxesh7u337m2a"
23
+ end
24
+
25
+ def getLink
26
+ scope = ""
27
+ @scope.each do |s|
28
+ scope += s + " "
29
+ end
30
+ link = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{scope}"
31
+ end
32
+
33
+ def auth(code)
34
+ path = "/oauth2/token"
35
+ url = @base_url + path
36
+ post(url, {
37
+ :client_id => @client_id,
38
+ :client_secret => @secret_key,
39
+ :grant_type => "authorization_code",
40
+ :redirect_uri => @redirect_uri,
41
+ :code => code
42
+ })
43
+ end
44
+
45
+ # User
46
+
47
+ def getUser(user)
48
+ path = "/users/"
49
+ url = @base_url + path + user;
50
+ get(url)
51
+ end
52
+
53
+ def getYourUser
54
+ return false if !@access_token
55
+ path = "/user?oauth_token=#{@access_token}"
56
+ url = @base_url + path
57
+ get(url)
58
+ end
59
+
60
+ # Teams
61
+
62
+ def getTeams
63
+ path = "/teams/"
64
+ url = @base_url + path;
65
+ get(url)
66
+ end
67
+
68
+
69
+ def getTeam(team_id)
70
+ path = "/teams/"
71
+ url = @base_url + path + team_id;
72
+ get(url)
73
+ end
74
+
75
+ # Channel
76
+
77
+ def getChannel(channel)
78
+ path = "/channels/"
79
+ url = @base_url + path + channel;
80
+ get(url)
81
+ end
82
+
83
+ def getYourChannel
84
+ return false if !@access_token
85
+ path = "/channel?oauth_token=#{@access_token}"
86
+ url = @base_url + path;
87
+ get(url)
88
+ end
89
+
90
+ def editChannel(status, game)
91
+ return false if !@access_token
92
+ path = "/channels/dustinlakin/?oauth_token=#{@access_token}"
93
+ url = @base_url + path
94
+ data = {
95
+ :channel =>{
96
+ :game => game,
97
+ :status => status
98
+ }
99
+ }
100
+ put(url, data)
101
+ end
102
+
103
+ def runCommercial(channel, length = 30)
104
+ return false if !@access_token
105
+ path = "/channels/#{channel}/commercial?oauth_token=#{@access_token}"
106
+ url = @base_url + path
107
+ post(url, {
108
+ :length => length
109
+ })
110
+ end
111
+
112
+ # Streams
113
+
114
+ def getStream(stream_name)
115
+ path = "/stream/#{stream_name}"
116
+ url = @base_url + path;
117
+ get(url)
118
+ end
119
+
120
+ def getStream(stream_name)
121
+ path = "/streams/#{stream_name}"
122
+ url = @base_url + path;
123
+ get(url)
124
+ end
125
+
126
+ def getStreams(options = {})
127
+ query = buildQueryString(options)
128
+ path = "/streams"
129
+ url = @base_url + path + query
130
+ get(url)
131
+ end
132
+
133
+ def getFeaturedStreams(options = {})
134
+ query = buildQueryString(options)
135
+ path = "/streams/featured"
136
+ url = @base_url + path + query
137
+ get(url)
138
+ end
139
+
140
+ def getSummeraizedStreams(options = {})
141
+ query = buildQueryString(options)
142
+ path = "/streams/summary"
143
+ url = @base_url + path + query
144
+ get(url)
145
+ end
146
+
147
+ def getYourFollowedStreams
148
+ path = "/streams/followed?oauth_token=#{@access_token}"
149
+ url = @base_url + path
150
+ get(url)
151
+ end
152
+
153
+ #Games
154
+
155
+ def getTopGames(options = {})
156
+ query = buildQueryString(options)
157
+ path = "/games/top"
158
+ url = @base_url + path + query
159
+ get(url)
160
+ end
161
+
162
+ #Search
163
+
164
+ def searchStreams(options = {})
165
+ query = buildQueryString(options)
166
+ path = "/search/streams"
167
+ url = @base_url + path + query
168
+ get(url)
169
+ end
170
+
171
+ def searchGames(options = {})
172
+ query = buildQueryString(options)
173
+ path = "/search/games"
174
+ url = @base_url + path + query
175
+ get(url)
176
+ end
177
+
178
+ # Videos
179
+
180
+ def getChannelVideos(channel, options = {})
181
+ query = buildQueryString(options)
182
+ path = "/channels/#{channel}/videos"
183
+ url = @base_url + path + query
184
+ get(url)
185
+ end
186
+
187
+ def getVideo(video_id)
188
+ path = "/videos/#{video_id}/"
189
+ url = @base_url + path
190
+ get(url)
191
+ end
192
+
193
+
194
+ private
195
+
196
+ def buildQueryString(options)
197
+ query = "?"
198
+ options.each do |key, value|
199
+ query += "#{key}=#{value.to_s.gsub(" ", "+")}&"
200
+ end
201
+ query = query[0...-1]
202
+ end
203
+
204
+ def post(url, data)
205
+ JSON.parse(Curl.post(url, data).body_str)
206
+ c = Curl.post(url, data)
207
+ {:body => JSON.parse(c.body_str), :response => c.response_code}
208
+ end
209
+
210
+ def get(url)
211
+ c = Curl.get(url)
212
+ {:body => JSON.parse(c.body_str), :response => c.response_code}
213
+ end
214
+
215
+ def put(url, data)
216
+ c = Curl.put(url,data.to_json) do |curl|
217
+ curl.headers['Accept'] = 'application/json'
218
+ curl.headers['Content-Type'] = 'application/json'
219
+ curl.headers['Api-Version'] = '2.2'
220
+ end
221
+ {:body => JSON.parse(c.body_str), :response => c.response_code}
222
+ end
223
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitch
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Dustin Lakin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-01-30 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: curb
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: Simplify Twitch's API for Ruby
38
+ email: dustin.lakin@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/twitch.rb
47
+ homepage: http://lak.in/
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Twitch API
74
+ test_files: []
75
+