kappa 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,11 @@
1
- module Kappa::V2
1
+ module Twitch::V2
2
2
  # Games are categories (e.g. League of Legends, Diablo 3) used by streams and channels.
3
3
  # Games can be searched for by query.
4
+ # @see Games#top Games#top
5
+ # @see Games#find Games#find
4
6
  # @see Games
5
7
  class Game
6
- include Kappa::IdEquality
8
+ include Twitch::IdEquality
7
9
 
8
10
  # @private
9
11
  def initialize(hash)
@@ -18,12 +20,18 @@ module Kappa::V2
18
20
  @logo_images = Images.new(game['logo'])
19
21
  end
20
22
 
23
+ # @example
24
+ # 21799
21
25
  # @return [Fixnum] Unique Twitch ID.
22
26
  attr_reader :id
23
27
 
28
+ # @example
29
+ # "League of Legends"
24
30
  # @return [String] User-friendly game name.
25
31
  attr_reader :name
26
32
 
33
+ # @example
34
+ # 24024
27
35
  # @return [Fixnum] Unique game ID for GiantBomb.com.
28
36
  attr_reader :giantbomb_id
29
37
 
@@ -33,17 +41,21 @@ module Kappa::V2
33
41
  # @return [Images] Set of images for the game's logo.
34
42
  attr_reader :logo_images
35
43
 
44
+ # @example
45
+ # 802
36
46
  # @return [Fixnum] Total number of channels currently streaming this game on Twitch.
37
47
  attr_reader :channel_count
38
48
 
49
+ # @example
50
+ # 68592
39
51
  # @return [Fixnum] Total number of viewers across all channels currently watching this game on Twitch.
40
52
  attr_reader :viewer_count
41
53
  end
42
54
 
43
- # A game suggestion returned by Twitch when searching for games via `Games.find`.
44
- # @see Games.find
55
+ # A game suggestion returned by Twitch when searching for games via `Twitch.games.find`.
56
+ # @see Games#find Games#find
45
57
  class GameSuggestion
46
- include Kappa::IdEquality
58
+ include Twitch::IdEquality
47
59
 
48
60
  # @private
49
61
  def initialize(hash)
@@ -55,16 +67,24 @@ module Kappa::V2
55
67
  @logo_images = Images.new(hash['logo'])
56
68
  end
57
69
 
70
+ # @example
71
+ # 155075940
58
72
  # @return [Fixnum] Unique Twitch ID.
59
73
  attr_reader :id
60
74
 
75
+ # @example
76
+ # "Dark Souls"
61
77
  # @return [String] Game name.
62
78
  attr_reader :name
63
79
 
80
+ # @example
81
+ # 32697
64
82
  # @return [Fixnum] Unique game ID for GiantBomb.com.
65
83
  attr_reader :giantbomb_id
66
84
 
67
- # @return [Fixnum] Relative popularity metric. Higher number means more popular.
85
+ # @example
86
+ # 67
87
+ # @return [Fixnum] Relative popularity metric. Higher number means more popular. This value only has meaning relative to other popularity values.
68
88
  attr_reader :popularity
69
89
 
70
90
  # @return [Images] Set of images for the game's box art.
@@ -74,36 +94,39 @@ module Kappa::V2
74
94
  attr_reader :logo_images
75
95
  end
76
96
 
77
- # Query class used for finding top games or finding games by name.
97
+ # Query class for finding top games or finding games by name.
78
98
  # @see Game
79
99
  # @see GameSuggestion
80
100
  class Games
81
- include Connection
101
+ # @private
102
+ def initialize(query)
103
+ @query = query
104
+ end
82
105
 
83
106
  # Get a list of games with the highest number of current viewers on Twitch.
84
107
  # @example
85
- # Games.top
108
+ # Twitch.games.top
86
109
  # @example
87
- # Games.top(:limit => 10)
110
+ # Twitch.games.top(:limit => 10)
88
111
  # @param options [Hash] Filter criteria.
89
112
  # @option options [Boolean] :hls (nil) If `true`, limit the games to those that have any streams using HLS (HTTP Live Streaming). If `false` or `nil`, do not limit.
90
113
  # @option options [Fixnum] :limit (none) Limit on the number of results returned.
91
114
  # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
92
- # @see Game
115
+ # @see Game Game
93
116
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/games.md#get-gamestop GET /games/top
94
117
  # @return [Array<Game>] List of games sorted by number of current viewers on Twitch, most popular first.
95
- def self.top(options = {})
118
+ def top(options = {})
96
119
  params = {}
97
120
 
98
121
  if options[:hls]
99
122
  params[:hls] = true
100
123
  end
101
124
 
102
- return connection.accumulate(
125
+ return @query.connection.accumulate(
103
126
  :path => 'games/top',
104
127
  :params => params,
105
128
  :json => 'top',
106
- :class => Game,
129
+ :create => Game,
107
130
  :limit => options[:limit],
108
131
  :offset => options[:offset]
109
132
  )
@@ -111,18 +134,18 @@ module Kappa::V2
111
134
 
112
135
  # Get a list of games with names similar to the specified name.
113
136
  # @example
114
- # Games.find(:name => 'diablo')
137
+ # Twitch.games.find(:name => 'diablo')
115
138
  # @example
116
- # Games.find(:name => 'starcraft', :live => true)
139
+ # Twitch.games.find(:name => 'starcraft', :live => true)
117
140
  # @param options [Hash] Search criteria.
118
141
  # @option options [String] :name Game name search term. This can be a partial name, e.g. `"league"`.
119
142
  # @option options [Boolean] :live (false) If `true`, only returns games that are currently live on at least one channel.
120
143
  # @option options [Fixnum] :limit (none) Limit on the number of results returned.
121
- # @see GameSuggestion
144
+ # @see GameSuggestion GameSuggestion
122
145
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/search.md#get-searchgames GET /search/games
123
146
  # @raise [ArgumentError] If `:name` is not specified.
124
147
  # @return [Array<GameSuggestion>] List of games matching the criteria.
125
- def self.find(options)
148
+ def find(options)
126
149
  raise ArgumentError, 'options' if options.nil?
127
150
  raise ArgumentError, 'name' if options[:name].nil?
128
151
 
@@ -135,11 +158,11 @@ module Kappa::V2
135
158
  params.merge!(:live => true)
136
159
  end
137
160
 
138
- return connection.accumulate(
161
+ return @query.connection.accumulate(
139
162
  :path => 'search/games',
140
163
  :params => params,
141
164
  :json => 'games',
142
- :class => GameSuggestion,
165
+ :create => GameSuggestion,
143
166
  :limit => options[:limit]
144
167
  )
145
168
  end
@@ -1,4 +1,4 @@
1
- module Kappa
1
+ module Twitch
2
2
  # @private
3
3
  module IdEquality
4
4
  def hash
@@ -1,4 +1,4 @@
1
- module Kappa::V2
1
+ module Twitch::V2
2
2
  # A group of URLs pointing to variously-sized versions of the same image.
3
3
  class Images
4
4
  # @private
@@ -10,6 +10,8 @@ module Kappa::V2
10
10
  end
11
11
 
12
12
  # Get a URL pointing to an image with a specific size.
13
+ # @example
14
+ # url = images.url(320, 200)
13
15
  # @param width [Fixnum] Desired width of the image.
14
16
  # @param height [Fixnum] Desired height of the image.
15
17
  # @return [String] URL pointing to the image with the specified size.
@@ -17,16 +19,24 @@ module Kappa::V2
17
19
  @template_url.gsub('{width}', width.to_s).gsub('{height}', height.to_s)
18
20
  end
19
21
 
22
+ # @example
23
+ # "http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-240x144.jpg"
20
24
  # @return [String] URL for the large-sized version of this image.
21
25
  attr_reader :large_url
22
26
 
27
+ # @example
28
+ # "http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-120x72.jpg"
23
29
  # @return [String] URL for the medium-sized version of this image.
24
30
  attr_reader :medium_url
25
31
 
32
+ # @example
33
+ # "http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-60x36.jpg"
26
34
  # @return [String] URL for the small-sized version of this image.
27
35
  attr_reader :small_url
28
36
 
29
37
  # @note You shouldn't need to use this property directly. See #url for getting a formatted URL instead.
38
+ # @example
39
+ # "http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-{width}x{height}.jpg"
30
40
  # @return [String] Template image URL with placeholders for width and height.
31
41
  # @see #url
32
42
  attr_reader :template_url
@@ -0,0 +1,22 @@
1
+ module Twitch::V2
2
+ # @private
3
+ class Query
4
+ def initialize(connection)
5
+ @connection = connection
6
+ @channels = Channels.new(self)
7
+ @streams = Streams.new(self)
8
+ @users = Users.new(self)
9
+ @games = Games.new(self)
10
+ @teams = Teams.new(self)
11
+ @videos = Videos.new(self)
12
+ end
13
+
14
+ attr_reader :connection
15
+ attr_reader :channels
16
+ attr_reader :streams
17
+ attr_reader :users
18
+ attr_reader :games
19
+ attr_reader :teams
20
+ attr_reader :videos
21
+ end
22
+ end
@@ -1,75 +1,63 @@
1
1
  require 'cgi'
2
2
 
3
- module Kappa::V2
3
+ module Twitch::V2
4
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
5
+ # @see Streams#get Streams#get
6
+ # @see Streams#find Streams#find
7
+ # @see Streams#featured Streams#featured
6
8
  # @see Streams
7
9
  # @see Channel
8
10
  class Stream
9
- include Connection
10
- include Kappa::IdEquality
11
+ include Twitch::IdEquality
11
12
 
12
13
  # @private
13
- def initialize(hash)
14
+ def initialize(hash, query)
15
+ @query = query
14
16
  @id = hash['_id']
15
17
  @broadcaster = hash['broadcaster']
16
18
  @game_name = hash['game']
17
19
  @name = hash['name']
18
20
  @viewer_count = hash['viewers']
19
21
  @preview_url = hash['preview']
20
- @channel = Channel.new(hash['channel'])
22
+ @channel = Channel.new(hash['channel'], @query)
21
23
  @url = @channel.url
22
24
  end
23
25
 
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
26
  # Get the owner of this stream.
49
27
  # @note This incurs an additional web request.
50
28
  # @return [User] The user that owns this stream.
51
29
  def user
52
- User.get(@channel.name)
30
+ @query.users.get(@channel.name)
53
31
  end
54
32
 
33
+ # @example
34
+ # 6226912672
55
35
  # @return [Fixnum] Unique Twitch ID.
56
36
  attr_reader :id
57
37
 
58
38
  # @example
59
39
  # "fme", "xsplit", "obs", "rebroadcast", "delay", "unknown rtmp"
60
- # @deprecated This attribute will not be present in the V3 API.
40
+ # @deprecated This attribute is not present in the V3 API.
61
41
  # @return [String] The broadcasting software used for this stream.
62
42
  attr_reader :broadcaster
63
43
 
44
+ # @example
45
+ # "Super Meat Boy"
64
46
  # @return [String] The name of the game currently being streamed.
65
47
  attr_reader :game_name
66
48
 
49
+ # @example
50
+ # "live_user_lethalfrag"
67
51
  # @return [String] The unique Twitch name for this stream.
68
52
  attr_reader :name
69
53
 
54
+ # @example
55
+ # 2342
70
56
  # @return [Fixnum] The number of viewers currently watching the stream.
71
57
  attr_reader :viewer_count
72
58
 
59
+ # @example
60
+ # "http://static-cdn.jtvnw.net/previews-ttv/live_user_lethalfrag-320x200.jpg"
73
61
  # @return [String] URL of a preview screenshot taken from the video stream.
74
62
  attr_reader :preview_url
75
63
 
@@ -77,36 +65,66 @@ module Kappa::V2
77
65
  # @return [Channel] The `Channel` associated with this stream.
78
66
  attr_reader :channel
79
67
 
68
+ # @example
69
+ # "http://www.twitch.tv/lethalfrag"
80
70
  # @return [String] The URL for this stream.
81
71
  attr_reader :url
82
72
  end
83
73
 
84
- # Query class used for finding featured streams or streams meeting certain criteria.
74
+ # Query class for finding featured streams or streams meeting certain criteria.
85
75
  # @see Stream
86
76
  class Streams
87
- include Connection
77
+ # @private
78
+ def initialize(query)
79
+ @query = query
80
+ end
81
+
82
+ # Get a live stream by name.
83
+ # @example
84
+ # s = Twitch.streams.get('lagtvmaximusblack')
85
+ # s.nil? # => false (stream is live)
86
+ # s.game_name # => "StarCraft II: Heart of the Swarm"
87
+ # s.viewer_count # => 2403
88
+ # @example
89
+ # s = Twitch.streams.get('destiny')
90
+ # s.nil? # => true (stream is offline)
91
+ # @param stream_name [String] The name of the stream to get. This is the same as the channel or user name.
92
+ # @see #find
93
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamschannel GET /streams/:channel
94
+ # @return [Stream] A valid `Stream` object if the stream exists and is currently live, `nil` otherwise.
95
+ def get(stream_name)
96
+ encoded_name = CGI.escape(stream_name)
97
+ json = @query.connection.get("streams/#{encoded_name}")
98
+ stream = json['stream']
99
+ if json['status'] == 404 || !stream
100
+ nil
101
+ else
102
+ Stream.new(stream, @query)
103
+ end
104
+ end
88
105
 
89
106
  # Get a list of streams for a specific game, for a set of channels, or by other criteria.
90
107
  # @example
91
- # Streams.find(:game => 'League of Legends', :limit => 50)
108
+ # Twitch.streams.find(:game => 'League of Legends', :limit => 50)
92
109
  # @example
93
- # Streams.find(:channel => ['fgtvlive', 'incontroltv', 'destiny'])
110
+ # Twitch.streams.find(:channel => ['fgtvlive', 'incontroltv', 'destiny'])
94
111
  # @example
95
- # Streams.find(:game => 'Diablo III', :channel => ['nl_kripp', 'protech'])
112
+ # Twitch.streams.find(:game => 'Diablo III', :channel => ['nl_kripp', 'protech'])
96
113
  # @param options [Hash] Search criteria.
97
114
  # @option options [String/Game/#name] :game Only return streams currently streaming the specified game.
98
115
  # @option options [Array<String/Channel/#name>] :channel Only return streams for these channels.
99
116
  # 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}.
117
+ # or channel names. If you want to find the stream for a single channel, see {Streams#get}.
101
118
  # @option options [Boolean] :embeddable (nil) If `true`, limit the streams to those that can be embedded. If `false` or `nil`, do not limit.
102
119
  # @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
120
  # @option options [Fixnum] :limit (none) Limit on the number of results returned.
104
121
  # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
105
- # @see Stream.get
122
+ # @see #get
106
123
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streams GET /streams
107
124
  # @raise [ArgumentError] If `options` does not specify a search criteria (`:game`, `:channel`, `:embeddable`, or `:hls`).
125
+ # @raise [ArgumentError] If `:channel` is not an array.
108
126
  # @return [Array<Stream>] List of streams matching the specified criteria.
109
- def self.find(options)
127
+ def find(options)
110
128
  check = options.dup
111
129
  check.delete(:limit)
112
130
  check.delete(:offset)
@@ -116,6 +134,10 @@ module Kappa::V2
116
134
 
117
135
  channels = options[:channel]
118
136
  if channels
137
+ if !channels.respond_to?(:map)
138
+ raise ArgumentError, ':channel'
139
+ end
140
+
119
141
  params[:channel] = channels.map { |channel|
120
142
  if channel.respond_to?(:name)
121
143
  channel.name
@@ -142,11 +164,11 @@ module Kappa::V2
142
164
  params[:embeddable] = true
143
165
  end
144
166
 
145
- return connection.accumulate(
167
+ return @query.connection.accumulate(
146
168
  :path => 'streams',
147
169
  :params => params,
148
170
  :json => 'streams',
149
- :class => Stream,
171
+ :create => -> hash { Stream.new(hash, @query) },
150
172
  :limit => options[:limit],
151
173
  :offset => options[:offset]
152
174
  )
@@ -155,28 +177,28 @@ module Kappa::V2
155
177
  # Get the list of currently featured (promoted) streams. This includes the list of streams shown on the Twitch homepage.
156
178
  # @note There is no guarantee of how many streams are featured at any given time.
157
179
  # @example
158
- # Streams.featured
180
+ # Twitch.streams.featured
159
181
  # @example
160
- # Streams.featured(:limit => 5)
182
+ # Twitch.streams.featured(:limit => 5)
161
183
  # @param options [Hash] Filter criteria.
162
184
  # @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
185
  # @option options [Fixnum] :limit (none) Limit on the number of results returned.
164
186
  # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
165
187
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamsfeatured GET /streams/featured
166
188
  # @return [Array<Stream>] List of currently featured streams.
167
- def self.featured(options = {})
189
+ def featured(options = {})
168
190
  params = {}
169
191
 
170
192
  if options[:hls]
171
193
  params[:hls] = true
172
194
  end
173
195
 
174
- return connection.accumulate(
196
+ return @query.connection.accumulate(
175
197
  :path => 'streams/featured',
176
198
  :params => params,
177
199
  :json => 'featured',
178
200
  :sub_json => 'stream',
179
- :class => Stream,
201
+ :create => -> hash { Stream.new(hash, @query) },
180
202
  :limit => options[:limit],
181
203
  :offset => options[:offset]
182
204
  )