kappa 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,17 @@
1
- module Kappa
2
- # @private
3
- module IdEquality
4
- def hash
5
- @id.hash
6
- end
7
-
8
- def eql?(other)
9
- other && (self.class == other.class) && (self.id == other.id)
10
- end
11
-
12
- def ==(other)
13
- eql?(other)
14
- end
15
- end
16
- end
17
-
1
+ module Kappa
2
+ # @private
3
+ module IdEquality
4
+ def hash
5
+ @id.hash
6
+ end
7
+
8
+ def eql?(other)
9
+ other && (self.class == other.class) && (self.id == other.id)
10
+ end
11
+
12
+ def ==(other)
13
+ eql?(other)
14
+ end
15
+ end
16
+ end
17
+
@@ -1,36 +1,34 @@
1
- module Kappa::V2
2
- # A group of URLs pointing to variously-sized versions of the same image.
3
- class Images
4
- # @private
5
- def initialize(hash)
6
- @large_url = hash['large']
7
- @medium_url = hash['medium']
8
- @small_url = hash['small']
9
- @template_url = hash['template']
10
- end
11
-
12
- # Get a URL pointing to an image with a specific size.
13
- # @param width [Fixnum] Desired width of the image.
14
- # @param height [Fixnum] Desired height of the image.
15
- # @return [String] URL pointing to the image with the specified size.
16
- def url(width, height)
17
- @template_url.gsub('{width}', width.to_s).gsub('{height}', height.to_s)
18
- end
19
-
20
- # TODO: Add documentation notes about rough sizes for small, medium, large images.
21
-
22
- # @return [String] URL for the large-sized version of this image.
23
- attr_reader :large_url
24
-
25
- # @return [String] URL for the medium-sized version of this image.
26
- attr_reader :medium_url
27
-
28
- # @return [String] URL for the small-sized version of this image.
29
- attr_reader :small_url
30
-
31
- # @note You shouldn't need to use this property directly. See #url for getting a formatted URL instead.
32
- # @return [String] Template image URL with placeholders for width and height.
33
- # @see #url
34
- attr_reader :template_url
35
- end
36
- end
1
+ module Kappa::V2
2
+ # A group of URLs pointing to variously-sized versions of the same image.
3
+ class Images
4
+ # @private
5
+ def initialize(hash)
6
+ @large_url = hash['large']
7
+ @medium_url = hash['medium']
8
+ @small_url = hash['small']
9
+ @template_url = hash['template']
10
+ end
11
+
12
+ # Get a URL pointing to an image with a specific size.
13
+ # @param width [Fixnum] Desired width of the image.
14
+ # @param height [Fixnum] Desired height of the image.
15
+ # @return [String] URL pointing to the image with the specified size.
16
+ def url(width, height)
17
+ @template_url.gsub('{width}', width.to_s).gsub('{height}', height.to_s)
18
+ end
19
+
20
+ # @return [String] URL for the large-sized version of this image.
21
+ attr_reader :large_url
22
+
23
+ # @return [String] URL for the medium-sized version of this image.
24
+ attr_reader :medium_url
25
+
26
+ # @return [String] URL for the small-sized version of this image.
27
+ attr_reader :small_url
28
+
29
+ # @note You shouldn't need to use this property directly. See #url for getting a formatted URL instead.
30
+ # @return [String] Template image URL with placeholders for width and height.
31
+ # @see #url
32
+ attr_reader :template_url
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ # @private
2
+ module Proxy
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def proxy(&create_block)
9
+ self.class_variable_set(:@@creator, create_block)
10
+ end
11
+ end
12
+
13
+ def method_missing(*args)
14
+ real.send(*args)
15
+ end
16
+
17
+ def respond_to?(sym, include_private = false)
18
+ real.respond_to?(sym, include_private) || super
19
+ end
20
+
21
+ def respond_to_missing?(method_name, include_private = false)
22
+ real.respond_to?(method_name, include_private) || super
23
+ end
24
+
25
+ private
26
+ def real
27
+ @real ||= begin
28
+ creator = self.class.class_variable_get(:@@creator)
29
+ self.instance_eval(&creator)
30
+ end
31
+ end
32
+ end
@@ -1,175 +1,185 @@
1
- require 'cgi'
2
-
3
- module Kappa::V2
4
- # Streams are video broadcasts that are currently live. They belong to a user and are part of a channel.
5
- # @see .get Stream.get
6
- # @see Streams
7
- # @see Channel
8
- class Stream
9
- include Connection
10
- include Kappa::IdEquality
11
-
12
- # @private
13
- def initialize(hash)
14
- @id = hash['_id']
15
- @broadcaster = hash['broadcaster']
16
- @game_name = hash['game']
17
- @name = hash['name']
18
- @viewer_count = hash['viewers']
19
- @preview_url = hash['preview']
20
- @channel = Channel.new(hash['channel'])
21
- end
22
-
23
- # Get a live stream by name.
24
- # @example
25
- # s = Stream.get('lagtvmaximusblack')
26
- # s.nil? # => false (stream is live)
27
- # s.game_name # => "StarCraft II: Heart of the Swarm"
28
- # s.viewer_count # => 2403
29
- # @example
30
- # s = Strearm.get('destiny')
31
- # s.nil? # => true (stream is offline)
32
- # @param stream_name [String] The name of the stream to get. This is the same as the channel or user name.
33
- # @see Streams.find
34
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamschannel GET /streams/:channel
35
- # @return [Stream] A valid `Stream` object if the stream exists and is currently live, `nil` otherwise.
36
- def self.get(stream_name)
37
- encoded_name = CGI.escape(stream_name)
38
- json = connection.get("streams/#{encoded_name}")
39
- stream = json['stream']
40
- if json['status'] == 404 || !stream
41
- nil
42
- else
43
- new(stream)
44
- end
45
- end
46
-
47
- # @return [Fixnum] Unique Twitch ID.
48
- attr_reader :id
49
-
50
- # @example
51
- # "fme", "xsplit", "obs"
52
- # @return [String] The broadcasting software used for this stream.
53
- attr_reader :broadcaster
54
-
55
- # @return [String] The name of the game currently being streamed.
56
- attr_reader :game_name
57
-
58
- # @return [String] The unique Twitch name for this stream.
59
- attr_reader :name
60
-
61
- # @return [Fixnum] The number of viewers currently watching the stream.
62
- attr_reader :viewer_count
63
-
64
- # @return [String] URL of a preview screenshot taken from the video stream.
65
- attr_reader :preview_url
66
-
67
- # @note This does not incur any web requests.
68
- # @return [Channel] The `Channel` associated with this stream.
69
- attr_reader :channel
70
- end
71
-
72
- # Query class used for finding featured streams or streams meeting certain criteria.
73
- # @see Stream
74
- class Streams
75
- include Connection
76
-
77
- # @private
78
- # Private until implemented
79
- def self.all
80
- # TODO
81
- end
82
-
83
- # Get a list of streams for a specific game, for a set of channels, or by other criteria.
84
- # @example
85
- # Streams.find(:game => 'League of Legends', :limit => 50)
86
- # @example
87
- # Streams.find(:channel => ['fgtvlive', 'incontroltv', 'destiny'])
88
- # @example
89
- # Streams.find(:game => 'Diablo III', :channel => ['nl_kripp', 'protech'])
90
- # @param options [Hash] Search criteria.
91
- # @option options [String/Game] :game Only return streams currently streaming the specified game.
92
- # @option options [Array<String/Channel>] :channel Only return streams for these channels.
93
- # If a channel is not currently streaming, it is omitted. You must specify an array of channels
94
- # or channel names. If you want to find the stream for a single channel, see {Stream.get}.
95
- # @option options [Boolean] :embeddable TODO
96
- # @option options [Boolean] :hls TODO
97
- # @option options [Fixnum] :limit (none) Limit on the number of results returned.
98
- # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
99
- # @see Stream.get
100
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streams GET /streams
101
- # @raise [ArgumentError] If `options` does not specify a search criteria (`:game`, `:channel`, `:embeddable`, or `:hls`).
102
- # @return [Array<Stream>] List of streams matching the specified criteria.
103
- def self.find(options)
104
- check = options.dup
105
- check.delete(:limit)
106
- check.delete(:offset)
107
- raise ArgumentError if check.empty?
108
-
109
- # TODO: Support Kappa::Vx::Game object for the :game param.
110
- # TODO: Support Kappa::Vx::Channel object for the :channel param.
111
-
112
- params = {}
113
-
114
- if options[:channel]
115
- params[:channel] = options[:channel].join(',')
116
- end
117
-
118
- if options[:game]
119
- params[:game] = options[:game]
120
- end
121
-
122
- limit = options[:limit]
123
- if limit && (limit < 100)
124
- params[:limit] = limit
125
- else
126
- params[:limit] = 100
127
- limit = 0
128
- end
129
-
130
- return connection.accumulate(
131
- :path => 'streams',
132
- :params => params,
133
- :json => 'streams',
134
- :class => Stream,
135
- :limit => limit
136
- )
137
- end
138
-
139
- # Get the list of currently featured (promoted) streams. This includes the list of streams shown on the Twitch homepage.
140
- # @note There is no guarantee of how many streams are featured at any given time.
141
- # @example
142
- # Streams.featured
143
- # @example
144
- # Streams.featured(:limit => 5)
145
- # @param options [Hash] Filter criteria.
146
- # @option options [Boolean] :hls (false) TODO
147
- # @option options [Fixnum] :limit (none) Limit on the number of results returned.
148
- # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
149
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamsfeatured GET /streams/featured
150
- # @return [Array<Stream>] List of currently featured streams.
151
- def self.featured(options = {})
152
- params = {}
153
-
154
- # TODO: Support :offset param
155
- # TODO: Support :hls param
156
-
157
- limit = options[:limit]
158
- if limit && (limit < 100)
159
- params[:limit] = limit
160
- else
161
- params[:limit] = 100
162
- limit = 0
163
- end
164
-
165
- return connection.accumulate(
166
- :path => 'streams/featured',
167
- :params => params,
168
- :json => 'featured',
169
- :sub_json => 'stream',
170
- :class => Stream,
171
- :limit => limit
172
- )
173
- end
174
- end
175
- end
1
+ require 'cgi'
2
+
3
+ module Kappa::V2
4
+ # Streams are video broadcasts that are currently live. They belong to a user and are part of a channel.
5
+ # @see .get Stream.get
6
+ # @see Streams
7
+ # @see Channel
8
+ class Stream
9
+ include Connection
10
+ include Kappa::IdEquality
11
+
12
+ # @private
13
+ def initialize(hash)
14
+ @id = hash['_id']
15
+ @broadcaster = hash['broadcaster']
16
+ @game_name = hash['game']
17
+ @name = hash['name']
18
+ @viewer_count = hash['viewers']
19
+ @preview_url = hash['preview']
20
+ @channel = Channel.new(hash['channel'])
21
+ @url = @channel.url
22
+ end
23
+
24
+ # Get a live stream by name.
25
+ # @example
26
+ # s = Stream.get('lagtvmaximusblack')
27
+ # s.nil? # => false (stream is live)
28
+ # s.game_name # => "StarCraft II: Heart of the Swarm"
29
+ # s.viewer_count # => 2403
30
+ # @example
31
+ # s = Strearm.get('destiny')
32
+ # s.nil? # => true (stream is offline)
33
+ # @param stream_name [String] The name of the stream to get. This is the same as the channel or user name.
34
+ # @see Streams.find
35
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamschannel GET /streams/:channel
36
+ # @return [Stream] A valid `Stream` object if the stream exists and is currently live, `nil` otherwise.
37
+ def self.get(stream_name)
38
+ encoded_name = CGI.escape(stream_name)
39
+ json = connection.get("streams/#{encoded_name}")
40
+ stream = json['stream']
41
+ if json['status'] == 404 || !stream
42
+ nil
43
+ else
44
+ new(stream)
45
+ end
46
+ end
47
+
48
+ # Get the owner of this stream.
49
+ # @note This incurs an additional web request.
50
+ # @return [User] The user that owns this stream.
51
+ def user
52
+ User.get(@channel.name)
53
+ end
54
+
55
+ # @return [Fixnum] Unique Twitch ID.
56
+ attr_reader :id
57
+
58
+ # @example
59
+ # "fme", "xsplit", "obs", "rebroadcast", "delay", "unknown rtmp"
60
+ # @deprecated This attribute will not be present in the V3 API.
61
+ # @return [String] The broadcasting software used for this stream.
62
+ attr_reader :broadcaster
63
+
64
+ # @return [String] The name of the game currently being streamed.
65
+ attr_reader :game_name
66
+
67
+ # @return [String] The unique Twitch name for this stream.
68
+ attr_reader :name
69
+
70
+ # @return [Fixnum] The number of viewers currently watching the stream.
71
+ attr_reader :viewer_count
72
+
73
+ # @return [String] URL of a preview screenshot taken from the video stream.
74
+ attr_reader :preview_url
75
+
76
+ # @note This does not incur any web requests.
77
+ # @return [Channel] The `Channel` associated with this stream.
78
+ attr_reader :channel
79
+
80
+ # @return [String] The URL for this stream.
81
+ attr_reader :url
82
+ end
83
+
84
+ # Query class used for finding featured streams or streams meeting certain criteria.
85
+ # @see Stream
86
+ class Streams
87
+ include Connection
88
+
89
+ # Get a list of streams for a specific game, for a set of channels, or by other criteria.
90
+ # @example
91
+ # Streams.find(:game => 'League of Legends', :limit => 50)
92
+ # @example
93
+ # Streams.find(:channel => ['fgtvlive', 'incontroltv', 'destiny'])
94
+ # @example
95
+ # Streams.find(:game => 'Diablo III', :channel => ['nl_kripp', 'protech'])
96
+ # @param options [Hash] Search criteria.
97
+ # @option options [String/Game/#name] :game Only return streams currently streaming the specified game.
98
+ # @option options [Array<String/Channel/#name>] :channel Only return streams for these channels.
99
+ # If a channel is not currently streaming, it is omitted. You must specify an array of channels
100
+ # or channel names. If you want to find the stream for a single channel, see {Stream.get}.
101
+ # @option options [Boolean] :embeddable (nil) If `true`, limit the streams to those that can be embedded. If `false` or `nil`, do not limit.
102
+ # @option options [Boolean] :hls (nil) If `true`, limit the streams to those using HLS (HTTP Live Streaming). If `false` or `nil`, do not limit.
103
+ # @option options [Fixnum] :limit (none) Limit on the number of results returned.
104
+ # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
105
+ # @see Stream.get
106
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streams GET /streams
107
+ # @raise [ArgumentError] If `options` does not specify a search criteria (`:game`, `:channel`, `:embeddable`, or `:hls`).
108
+ # @return [Array<Stream>] List of streams matching the specified criteria.
109
+ def self.find(options)
110
+ check = options.dup
111
+ check.delete(:limit)
112
+ check.delete(:offset)
113
+ raise ArgumentError, 'options' if check.empty?
114
+
115
+ params = {}
116
+
117
+ channels = options[:channel]
118
+ if channels
119
+ params[:channel] = channels.map { |channel|
120
+ if channel.respond_to?(:name)
121
+ channel.name
122
+ else
123
+ channel.to_s
124
+ end
125
+ }.join(',')
126
+ end
127
+
128
+ game = options[:game]
129
+ if game
130
+ if game.respond_to?(:name)
131
+ params[:game] = game.name
132
+ else
133
+ params[:game] = game.to_s
134
+ end
135
+ end
136
+
137
+ if options[:hls]
138
+ params[:hls] = true
139
+ end
140
+
141
+ if options[:embeddable]
142
+ params[:embeddable] = true
143
+ end
144
+
145
+ return connection.accumulate(
146
+ :path => 'streams',
147
+ :params => params,
148
+ :json => 'streams',
149
+ :class => Stream,
150
+ :limit => options[:limit],
151
+ :offset => options[:offset]
152
+ )
153
+ end
154
+
155
+ # Get the list of currently featured (promoted) streams. This includes the list of streams shown on the Twitch homepage.
156
+ # @note There is no guarantee of how many streams are featured at any given time.
157
+ # @example
158
+ # Streams.featured
159
+ # @example
160
+ # Streams.featured(:limit => 5)
161
+ # @param options [Hash] Filter criteria.
162
+ # @option options [Boolean] :hls (nil) If `true`, limit the streams to those using HLS (HTTP Live Streaming). If `false` or `nil`, do not limit.
163
+ # @option options [Fixnum] :limit (none) Limit on the number of results returned.
164
+ # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
165
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamsfeatured GET /streams/featured
166
+ # @return [Array<Stream>] List of currently featured streams.
167
+ def self.featured(options = {})
168
+ params = {}
169
+
170
+ if options[:hls]
171
+ params[:hls] = true
172
+ end
173
+
174
+ return connection.accumulate(
175
+ :path => 'streams/featured',
176
+ :params => params,
177
+ :json => 'featured',
178
+ :sub_json => 'stream',
179
+ :class => Stream,
180
+ :limit => options[:limit],
181
+ :offset => options[:offset]
182
+ )
183
+ end
184
+ end
185
+ end