kappa 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +184 -148
- data/lib/kappa.rb +12 -11
- data/lib/kappa/channel.rb +143 -184
- data/lib/kappa/connection.rb +129 -142
- data/lib/kappa/game.rb +147 -161
- data/lib/kappa/id_equality.rb +17 -17
- data/lib/kappa/images.rb +34 -36
- data/lib/kappa/proxy.rb +32 -0
- data/lib/kappa/stream.rb +185 -175
- data/lib/kappa/team.rb +99 -99
- data/lib/kappa/twitch.rb +13 -11
- data/lib/kappa/user.rb +124 -132
- data/lib/kappa/version.rb +3 -1
- data/lib/kappa/video.rb +150 -100
- metadata +100 -38
data/lib/kappa/id_equality.rb
CHANGED
@@ -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
|
+
|
data/lib/kappa/images.rb
CHANGED
@@ -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
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# @
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
data/lib/kappa/proxy.rb
ADDED
@@ -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
|
data/lib/kappa/stream.rb
CHANGED
@@ -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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# s
|
27
|
-
# s.
|
28
|
-
# s.
|
29
|
-
#
|
30
|
-
#
|
31
|
-
# s
|
32
|
-
#
|
33
|
-
# @
|
34
|
-
# @see
|
35
|
-
# @
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
# @
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
# @return [
|
56
|
-
attr_reader :
|
57
|
-
|
58
|
-
# @
|
59
|
-
|
60
|
-
|
61
|
-
# @return [
|
62
|
-
attr_reader :
|
63
|
-
|
64
|
-
# @return [String]
|
65
|
-
attr_reader :
|
66
|
-
|
67
|
-
# @
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
# @
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
#
|
90
|
-
# @
|
91
|
-
#
|
92
|
-
# @
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# @
|
97
|
-
# @option options [
|
98
|
-
# @option options [
|
99
|
-
#
|
100
|
-
#
|
101
|
-
# @
|
102
|
-
# @
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
if
|
119
|
-
params[:
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
:
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
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
|