kappa 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
- module Twitch
2
- VERSION = '1.0.1'
3
- end
1
+ module Twitch
2
+ VERSION = '1.0.2'
3
+ end
@@ -1,229 +1,229 @@
1
- require 'cgi'
2
- require 'time'
3
-
4
- module Twitch::V2
5
- # @private
6
- class ChannelProxy
7
- def initialize(name, display_name, query)
8
- @name = name
9
- @display_name = display_name
10
- @query = query
11
- end
12
-
13
- attr_reader :name
14
- attr_reader :display_name
15
-
16
- include Proxy
17
-
18
- proxy {
19
- @query.channels.get(@name)
20
- }
21
- end
22
-
23
- # Videos are broadcasts or highlights owned by a channel. Broadcasts are unedited
24
- # videos that are saved after a streaming session. Highlights are videos edited from
25
- # broadcasts by the channel's owner.
26
- # @see Videos#get Videos#get
27
- # @see Videos#top Videos#top
28
- # @see Videos
29
- # @see Channel
30
- class Video
31
- include Twitch::IdEquality
32
-
33
- # @private
34
- def initialize(hash, query)
35
- @id = hash['_id']
36
- @title = hash['title']
37
- @recorded_at = Time.parse(hash['recorded_at']).utc
38
- @url = hash['url']
39
- @view_count = hash['views']
40
- @description = hash['description']
41
- @length = hash['length']
42
- @game_name = hash['game']
43
- @preview_url = hash['preview']
44
- @embed_html = hash['embed']
45
-
46
- @channel = ChannelProxy.new(
47
- hash['channel']['name'],
48
- hash['channel']['display_name'],
49
- query
50
- )
51
- end
52
-
53
- # @note This is a `String`, not a `Fixnum` like most other object IDs.
54
- # @example
55
- # "a396294648"
56
- # @return [String] Unique Twitch ID for this video.
57
- attr_reader :id
58
-
59
- # @example
60
- # "DreamHack Open Stockholm 26-27 April"
61
- # @return [String] Title of this video. This is seen on the video's page.
62
- attr_reader :title
63
-
64
- # @example
65
- # 2013-04-27 09:37:30 UTC
66
- # @return [Time] When this video was recorded (UTC).
67
- attr_reader :recorded_at
68
-
69
- # @example
70
- # "http://www.twitch.tv/dreamhacktv/b/396294648"
71
- # @return [String] URL of this video on Twitch.
72
- attr_reader :url
73
-
74
- # @example
75
- # 81754
76
- # @return [Fixnum] The number of views this video has received all-time.
77
- attr_reader :view_count
78
-
79
- # @return [String] Description of this video.
80
- attr_reader :description
81
-
82
- # @example
83
- # 4205 # (1 hour, 10 minutes, 5 seconds)
84
- # @return [Fixnum] The length of this video (seconds).
85
- attr_reader :length
86
-
87
- # @example
88
- # "StarCraft II: Heart of the Swarm"
89
- # @return [String] The name of the game played in this video.
90
- attr_reader :game_name
91
-
92
- # @example
93
- # "http://static-cdn.jtvnw.net/jtv.thumbs/archive-396294648-320x240.jpg"
94
- # @return [String] URL of a preview screenshot taken from the video stream.
95
- attr_reader :preview_url
96
-
97
- # @return [Channel] The channel on which this video was originally streamed.
98
- attr_reader :channel
99
-
100
- # @example
101
- # "<object data='http://www.twitch.tv/widgets/archive_embed_player.swf'>...</object>"
102
- # @return [String] HTML code for embedding this video on a web page.
103
- attr_reader :embed_html
104
- end
105
-
106
- # Query class for finding videos.
107
- # @see Video
108
- class Videos
109
- # @private
110
- def initialize(query)
111
- @query = query
112
- end
113
-
114
- # Get a video by ID.
115
- # @example
116
- # Twitch.videos.get('a396294648')
117
- # @param id [String] The ID of the video to get.
118
- # @raise [ArgumentError] If `id` is `nil` or empty.
119
- # @return [Video] A valid `Video` object if the video exists, `nil` otherwise.
120
- def get(id)
121
- raise ArgumentError, 'id' if !id || id.strip.empty?
122
-
123
- id = CGI.escape(id)
124
- Twitch::Status.map(404 => nil) do
125
- json = @query.connection.get("videos/#{id}")
126
- Video.new(json, @query)
127
- end
128
- end
129
-
130
- # Get the list of most popular videos based on view count.
131
- # @note The number of videos returned is potentially very large, so it's recommended that you specify a `:limit`.
132
- # @example
133
- # Twitch.videos.top
134
- # @example
135
- # Twitch.videos.top(:period => :month, :game => 'Super Meat Boy')
136
- # @example
137
- # Twitch.videos.top(:period => :all, :limit => 10)
138
- # @example
139
- # Twitch.videos.top(:period => :all) do |video|
140
- # next if video.view_count < 10000
141
- # puts video.url
142
- # end
143
- # @param options [Hash] Filter criteria.
144
- # @option options [Symbol] :period (:week) Return videos only in this time period. Valid values are `:week`, `:month`, `:all`.
145
- # @option options [String] :game (nil) Return videos only for this game.
146
- # @option options [Fixnum] :limit (nil) Limit on the number of results returned.
147
- # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
148
- # @yield Optional. If a block is given, each top video is yielded.
149
- # @yieldparam [Video] video Current video.
150
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-videostop GET /videos/top
151
- # @raise [ArgumentError] If `:period` is not one of `:week`, `:month`, or `:all`.
152
- # @return [Array<Video>] Top videos, if no block is given.
153
- # @return [nil] If a block is given.
154
- def top(options = {}, &block)
155
- params = {}
156
-
157
- if options[:game]
158
- params[:game] = options[:game]
159
- end
160
-
161
- period = options[:period] || :week
162
- if ![:week, :month, :all].include?(period)
163
- raise ArgumentError, 'period'
164
- end
165
-
166
- params[:period] = period.to_s
167
-
168
- return @query.connection.accumulate(
169
- :path => 'videos/top',
170
- :params => params,
171
- :json => 'videos',
172
- :create => -> hash { Video.new(hash, @query) },
173
- :limit => options[:limit],
174
- :offset => options[:offset],
175
- &block
176
- )
177
- end
178
-
179
- # Get the videos for a channel, most recently created first.
180
- # @example
181
- # v = Twitch.videos.for_channel('dreamhacktv')
182
- # @example
183
- # v = Twitch.videos.for_channel('dreamhacktv', :type => :highlights, :limit => 10)
184
- # @example
185
- # Twitch.videos.for_channel('dreamhacktv') do |video|
186
- # next if video.view_count < 10000
187
- # puts video.url
188
- # end
189
- # @param options [Hash] Filter criteria.
190
- # @option options [Symbol] :type (:highlights) The type of videos to return. Valid values are `:broadcasts`, `:highlights`.
191
- # @option options [Fixnum] :limit (nil) Limit on the number of results returned.
192
- # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
193
- # @yield Optional. If a block is given, each video is yielded.
194
- # @yieldparam [Video] video Current video.
195
- # @see Channel#videos Channel#videos
196
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-channelschannelvideos GET /channels/:channel/videos
197
- # @raise [ArgumentError] If `:type` is not one of `:broadcasts` or `:highlights`.
198
- # @return [Array<Video>] Videos for the channel, if no block is given.
199
- # @return [nil] If a block is given.
200
- def for_channel(channel, options = {})
201
- if channel.respond_to?(:name)
202
- channel_name = channel.name
203
- else
204
- channel_name = channel.to_s
205
- end
206
-
207
- params = {}
208
-
209
- type = options[:type] || :highlights
210
- if !type.nil?
211
- if ![:broadcasts, :highlights].include?(type)
212
- raise ArgumentError, 'type'
213
- end
214
-
215
- params[:broadcasts] = (type == :broadcasts)
216
- end
217
-
218
- name = CGI.escape(channel_name)
219
- return @query.connection.accumulate(
220
- :path => "channels/#{name}/videos",
221
- :params => params,
222
- :json => 'videos',
223
- :create => -> hash { Video.new(hash, @query) },
224
- :limit => options[:limit],
225
- :offset => options[:offset]
226
- )
227
- end
228
- end
229
- end
1
+ require 'cgi'
2
+ require 'time'
3
+
4
+ module Twitch::V2
5
+ # @private
6
+ class ChannelProxy
7
+ def initialize(name, display_name, query)
8
+ @name = name
9
+ @display_name = display_name
10
+ @query = query
11
+ end
12
+
13
+ attr_reader :name
14
+ attr_reader :display_name
15
+
16
+ include Proxy
17
+
18
+ proxy {
19
+ @query.channels.get(@name)
20
+ }
21
+ end
22
+
23
+ # Videos are broadcasts or highlights owned by a channel. Broadcasts are unedited
24
+ # videos that are saved after a streaming session. Highlights are videos edited from
25
+ # broadcasts by the channel's owner.
26
+ # @see Videos#get Videos#get
27
+ # @see Videos#top Videos#top
28
+ # @see Videos
29
+ # @see Channel
30
+ class Video
31
+ include Twitch::IdEquality
32
+
33
+ # @private
34
+ def initialize(hash, query)
35
+ @id = hash['_id']
36
+ @title = hash['title']
37
+ @recorded_at = Time.parse(hash['recorded_at']).utc
38
+ @url = hash['url']
39
+ @view_count = hash['views']
40
+ @description = hash['description']
41
+ @length = hash['length']
42
+ @game_name = hash['game']
43
+ @preview_url = hash['preview']
44
+ @embed_html = hash['embed']
45
+
46
+ @channel = ChannelProxy.new(
47
+ hash['channel']['name'],
48
+ hash['channel']['display_name'],
49
+ query
50
+ )
51
+ end
52
+
53
+ # @note This is a `String`, not a `Fixnum` like most other object IDs.
54
+ # @example
55
+ # "a396294648"
56
+ # @return [String] Unique Twitch ID for this video.
57
+ attr_reader :id
58
+
59
+ # @example
60
+ # "DreamHack Open Stockholm 26-27 April"
61
+ # @return [String] Title of this video. This is seen on the video's page.
62
+ attr_reader :title
63
+
64
+ # @example
65
+ # 2013-04-27 09:37:30 UTC
66
+ # @return [Time] When this video was recorded (UTC).
67
+ attr_reader :recorded_at
68
+
69
+ # @example
70
+ # "http://www.twitch.tv/dreamhacktv/b/396294648"
71
+ # @return [String] URL of this video on Twitch.
72
+ attr_reader :url
73
+
74
+ # @example
75
+ # 81754
76
+ # @return [Fixnum] The number of views this video has received all-time.
77
+ attr_reader :view_count
78
+
79
+ # @return [String] Description of this video.
80
+ attr_reader :description
81
+
82
+ # @example
83
+ # 4205 # (1 hour, 10 minutes, 5 seconds)
84
+ # @return [Fixnum] The length of this video (seconds).
85
+ attr_reader :length
86
+
87
+ # @example
88
+ # "StarCraft II: Heart of the Swarm"
89
+ # @return [String] The name of the game played in this video.
90
+ attr_reader :game_name
91
+
92
+ # @example
93
+ # "http://static-cdn.jtvnw.net/jtv.thumbs/archive-396294648-320x240.jpg"
94
+ # @return [String] URL of a preview screenshot taken from the video stream.
95
+ attr_reader :preview_url
96
+
97
+ # @return [Channel] The channel on which this video was originally streamed.
98
+ attr_reader :channel
99
+
100
+ # @example
101
+ # "<object data='http://www.twitch.tv/widgets/archive_embed_player.swf'>...</object>"
102
+ # @return [String] HTML code for embedding this video on a web page.
103
+ attr_reader :embed_html
104
+ end
105
+
106
+ # Query class for finding videos.
107
+ # @see Video
108
+ class Videos
109
+ # @private
110
+ def initialize(query)
111
+ @query = query
112
+ end
113
+
114
+ # Get a video by ID.
115
+ # @example
116
+ # Twitch.videos.get('a396294648')
117
+ # @param id [String] The ID of the video to get.
118
+ # @raise [ArgumentError] If `id` is `nil` or empty.
119
+ # @return [Video] A valid `Video` object if the video exists, `nil` otherwise.
120
+ def get(id)
121
+ raise ArgumentError, 'id' if !id || id.strip.empty?
122
+
123
+ id = CGI.escape(id)
124
+ Twitch::Status.map(404 => nil) do
125
+ json = @query.connection.get("videos/#{id}")
126
+ Video.new(json, @query)
127
+ end
128
+ end
129
+
130
+ # Get the list of most popular videos based on view count.
131
+ # @note The number of videos returned is potentially very large, so it's recommended that you specify a `:limit`.
132
+ # @example
133
+ # Twitch.videos.top
134
+ # @example
135
+ # Twitch.videos.top(:period => :month, :game => 'Super Meat Boy')
136
+ # @example
137
+ # Twitch.videos.top(:period => :all, :limit => 10)
138
+ # @example
139
+ # Twitch.videos.top(:period => :all) do |video|
140
+ # next if video.view_count < 10000
141
+ # puts video.url
142
+ # end
143
+ # @param options [Hash] Filter criteria.
144
+ # @option options [Symbol] :period (:week) Return videos only in this time period. Valid values are `:week`, `:month`, `:all`.
145
+ # @option options [String] :game (nil) Return videos only for this game.
146
+ # @option options [Fixnum] :limit (nil) Limit on the number of results returned.
147
+ # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
148
+ # @yield Optional. If a block is given, each top video is yielded.
149
+ # @yieldparam [Video] video Current video.
150
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-videostop GET /videos/top
151
+ # @raise [ArgumentError] If `:period` is not one of `:week`, `:month`, or `:all`.
152
+ # @return [Array<Video>] Top videos, if no block is given.
153
+ # @return [nil] If a block is given.
154
+ def top(options = {}, &block)
155
+ params = {}
156
+
157
+ if options[:game]
158
+ params[:game] = options[:game]
159
+ end
160
+
161
+ period = options[:period] || :week
162
+ if ![:week, :month, :all].include?(period)
163
+ raise ArgumentError, 'period'
164
+ end
165
+
166
+ params[:period] = period.to_s
167
+
168
+ return @query.connection.accumulate(
169
+ :path => 'videos/top',
170
+ :params => params,
171
+ :json => 'videos',
172
+ :create => -> hash { Video.new(hash, @query) },
173
+ :limit => options[:limit],
174
+ :offset => options[:offset],
175
+ &block
176
+ )
177
+ end
178
+
179
+ # Get the videos for a channel, most recently created first.
180
+ # @example
181
+ # v = Twitch.videos.for_channel('dreamhacktv')
182
+ # @example
183
+ # v = Twitch.videos.for_channel('dreamhacktv', :type => :highlights, :limit => 10)
184
+ # @example
185
+ # Twitch.videos.for_channel('dreamhacktv') do |video|
186
+ # next if video.view_count < 10000
187
+ # puts video.url
188
+ # end
189
+ # @param options [Hash] Filter criteria.
190
+ # @option options [Symbol] :type (:highlights) The type of videos to return. Valid values are `:broadcasts`, `:highlights`.
191
+ # @option options [Fixnum] :limit (nil) Limit on the number of results returned.
192
+ # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
193
+ # @yield Optional. If a block is given, each video is yielded.
194
+ # @yieldparam [Video] video Current video.
195
+ # @see Channel#videos Channel#videos
196
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-channelschannelvideos GET /channels/:channel/videos
197
+ # @raise [ArgumentError] If `:type` is not one of `:broadcasts` or `:highlights`.
198
+ # @return [Array<Video>] Videos for the channel, if no block is given.
199
+ # @return [nil] If a block is given.
200
+ def for_channel(channel, options = {})
201
+ if channel.respond_to?(:name)
202
+ channel_name = channel.name
203
+ else
204
+ channel_name = channel.to_s
205
+ end
206
+
207
+ params = {}
208
+
209
+ type = options[:type] || :highlights
210
+ if !type.nil?
211
+ if ![:broadcasts, :highlights].include?(type)
212
+ raise ArgumentError, 'type'
213
+ end
214
+
215
+ params[:broadcasts] = (type == :broadcasts)
216
+ end
217
+
218
+ name = CGI.escape(channel_name)
219
+ return @query.connection.accumulate(
220
+ :path => "channels/#{name}/videos",
221
+ :params => params,
222
+ :json => 'videos',
223
+ :create => -> hash { Video.new(hash, @query) },
224
+ :limit => options[:limit],
225
+ :offset => options[:offset]
226
+ )
227
+ end
228
+ end
229
+ end