kappa 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,148 +1,184 @@
1
- # <img src="https://raw.github.com/schmich/kappa/master/assets/kappa.png" /> Kappa
2
-
3
- Kappa is the Ruby library for interfacing with the [Twitch.tv API](https://github.com/justintv/Twitch-API).
4
-
5
- [![Gem Version](https://badge.fury.io/rb/kappa.png)](http://rubygems.org/gems/kappa)
6
- [![Build Status](https://secure.travis-ci.org/schmich/kappa.png)](http://travis-ci.org/schmich/kappa)
7
- [![Dependency Status](https://gemnasium.com/schmich/kappa.png)](https://gemnasium.com/schmich/kappa)
8
- [![Coverage Status](https://coveralls.io/repos/schmich/kappa/badge.png?branch=master)](https://coveralls.io/r/schmich/kappa?branch=master)
9
- [![Code Climate](https://codeclimate.com/github/schmich/kappa.png)](https://codeclimate.com/github/schmich/kappa)
10
-
11
- ## Getting Started
12
-
13
- `gem install kappa`
14
-
15
- ```ruby
16
- require 'kappa'
17
-
18
- include Kappa::V2
19
-
20
- grubby = Channel.get('followgrubby')
21
- puts grubby.streaming?
22
- ```
23
-
24
- ## Examples
25
-
26
- ### <a id="channels"></a>Channels
27
-
28
- Channels serve as the home location for a [user's](#users) content. Channels have a [stream](#streams), can run commercials, store [videos](#videos), display information and status, and have a customized page including banners and backgrounds.
29
-
30
- See also [`Kappa::V2::Channel`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Channel) documentation.
31
-
32
- ```ruby
33
- c = Channel.get('destiny')
34
- c.nil? # => false (channel exists)
35
- c.stream # => #<Kappa::V2::Stream> (current live stream)
36
- c.url # => "http://www.twitch.tv/destiny"
37
- c.status # => "Destiny - Diamond I ADC - Number 1 Draven player..."
38
- c.teams # => [#<Kappa::V2::Team>]
39
- c.videos # => [#<Kappa::V2::Video>, ...]
40
- c.followers # => [#<Kappa::V2::User>, ...]
41
- ```
42
-
43
- ### <a id="streams"></a>Streams
44
-
45
- Streams are video broadcasts that are currently live. They belong to a [user](#users) and are part of a [channel](#channels).
46
-
47
- See also [`Kappa::V2::Stream`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Stream) and [`Kappa::V2::Streams`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Streams) documentation.
48
-
49
- ```ruby
50
- s = Stream.get('idrajit')
51
- s.nil? # => false (currently live)
52
- s.game_name # => "StarCraft II: Heart of the Swarm"
53
- s.viewer_count # => 7267
54
- s.channel.url # => "http://www.twitch.tv/idrajit"
55
- ```
56
-
57
- ### <a id="users"></a>Users
58
-
59
- These are members of the Twitch community who have a Twitch account. If broadcasting, they can own a [stream](#streams) that they can broadcast on their [channel](#channels). If mainly viewing, they might follow or subscribe to channels.
60
-
61
- See also [`Kappa::V2::User`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/User) documentation.
62
-
63
- ```ruby
64
- u = User.get('snoopeh')
65
- u.nil? # => false (user exists)
66
- u.channel # => #<Kappa::V2::Channel>
67
- u.following.map(&:name) # => ["national_esl1", "dreamhacklol", "riotgames"]
68
- ```
69
-
70
- ### <a id="videos"></a>Videos
71
-
72
- Videos are broadcasts or highlights owned by a [channel](#channels). Broadcasts are unedited videos that are saved after a streaming session. Highlights are videos edited from broadcasts by the channel's owner.
73
-
74
- See also [`Kappa::V2::Video`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Video) and [`Kappa::V2::Videos`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Videos) documentation.
75
-
76
- ```ruby
77
- v = Video.get('a395995729')
78
- v.nil? # => false (video exists)
79
- v.title # => "DreamHack Open Stockholm 26-27 April"
80
- v.game_name # => "StarCraft II: Heart of the Swarm"
81
- v.recorded_at # => #<DateTime: 2013-04-26T18:33:48+00:00>
82
- v.view_count # => 12506
83
- ```
84
-
85
- ### <a id="teams"></a>Teams
86
-
87
- Teams are an organization of [channels](#channels).
88
-
89
- See also [`Kappa::V2::Team`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Team) documentation.
90
-
91
- ```ruby
92
- t = Team.get('teamliquid')
93
- t.display_name # => "TeamLiquid"
94
- t.info # => "TeamLiquid is awesome. and esports. video games. \n\n"
95
- t.updated_at # => #<DateTime: 2013-04-27T16:58:55+00:00>
96
- ```
97
-
98
- ### <a id="games"></a>Games
99
-
100
- Games are categories (e.g. League of Legends, Diablo 3) used by [streams](#streams) and [channels](#channels). Games can be searched for by query.
101
-
102
- See also [`Kappa::V2::Game`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Game), [`Kappa::V2::Games`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/Games), and [`Kappa::V2::GameSuggestion`](http://rdoc.info/github/schmich/kappa/master/Kappa/V2/GameSuggestion) documentation.
103
-
104
- ```ruby
105
- top = Games.top(:limit => 3)
106
- top.map(&:name) # => ["League of Legends", "Dota 2", "StarCraft II: Heart of the Swarm"]
107
- ```
108
-
109
- ```ruby
110
- g = Games.top(:limit => 1).first
111
- g.name # => "League of Legends"
112
- g.channel_count # => 906
113
- g.viewer_count # => 79223
114
- g.box_images.medium_url # =>"http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg"
115
- ```
116
-
117
- ```ruby
118
- s = Games.find(:name => 'diablo', :live => true)
119
- s.map(&:name) # => ["Diablo III", "Diablo II", "Diablo", "Diablo II: Lord of Destruction"]
120
- s.map(&:popularity) # => [120, 4, 1, 1]
121
- ```
122
-
123
- ## Documentation
124
-
125
- Detailed API documentation is available at [http://rdoc.info/github/schmich/kappa/master/frames](http://rdoc.info/github/schmich/kappa/master/frames).
126
-
127
- ## Versioning
128
-
129
- - TODO: Semantic versioning
130
- - TODO: Twitch API versions
131
-
132
- ## Contributing
133
-
134
- - [Fork and clone the repo.](http://help.github.com/fork-a-repo/)
135
- - [Create a branch for your changes.](http://learn.github.com/p/branching.html)
136
- - Run `bundle install` to install development requirements.
137
- - Implement your feature or bug fix.
138
- - Add specs under the `spec` folder to prevent regressions or to test new code.
139
- - Add [YARD](http://rubydoc.info/docs/yard/file/docs/GettingStarted.md) documentation for new features. Run `rake yard` to view documentation.
140
- - Run `rake coverage` to run specs with code coverage. All specs must pass; coverage must remain at 100%.
141
- - Commit and push your changes.
142
- - [Submit a pull request.](http://help.github.com/send-pull-requests/)
143
-
144
- ## License
145
-
146
- Copyright &copy; 2013 Chris Schmich
147
- <br />
148
- MIT License, See [LICENSE](LICENSE) for details.
1
+ # <img src="https://raw.github.com/schmich/kappa/master/assets/kappa.png" /> Kappa
2
+
3
+ Kappa is the Ruby library for interfacing with the [Twitch.tv API](https://github.com/justintv/Twitch-API).
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/kappa.png)](http://rubygems.org/gems/kappa)
6
+ [![Build Status](https://secure.travis-ci.org/schmich/kappa.png)](http://travis-ci.org/schmich/kappa)
7
+ [![Dependency Status](https://gemnasium.com/schmich/kappa.png)](https://gemnasium.com/schmich/kappa)
8
+ [![Coverage Status](https://coveralls.io/repos/schmich/kappa/badge.png?branch=master)](https://coveralls.io/r/schmich/kappa?branch=master)
9
+ [![Code Climate](https://codeclimate.com/github/schmich/kappa.png)](https://codeclimate.com/github/schmich/kappa)
10
+
11
+ ## Getting Started
12
+
13
+ ```bash
14
+ gem install kappa
15
+ ```
16
+
17
+ ```ruby
18
+ require 'kappa'
19
+
20
+ include Kappa::V2
21
+
22
+ grubby = Channel.get('followgrubby')
23
+ puts grubby.streaming?
24
+ ```
25
+
26
+ ## Overview
27
+
28
+ ### <a id="channels"></a>Channels
29
+
30
+ Channels serve as the home location for a [user's](#users) content. Channels have a [stream](#streams), can run commercials, store [videos](#videos), display information and status, and have a customized page including banners and backgrounds.
31
+
32
+ See also [`Channel`](http://rdoc.info/gems/kappa/Kappa/V2/Channel) documentation.
33
+
34
+ ```ruby
35
+ c = Channel.get('destiny')
36
+ c.nil? # => false (channel exists)
37
+ c.stream # => #<Kappa::V2::Stream> (current live stream)
38
+ c.url # => "http://www.twitch.tv/destiny"
39
+ c.status # => "Destiny - Diamond I ADC - Number 1 Draven player..."
40
+ c.teams # => [#<Kappa::V2::Team>]
41
+ c.videos # => [#<Kappa::V2::Video>, ...]
42
+ c.followers # => [#<Kappa::V2::User>, ...]
43
+ ```
44
+
45
+ ### <a id="streams"></a>Streams
46
+
47
+ Streams are video broadcasts that are currently live. They belong to a [user](#users) and are part of a [channel](#channels).
48
+
49
+ See also [`Stream`](http://rdoc.info/gems/kappa/Kappa/V2/Stream) and [`Streams`](http://rdoc.info/gems/kappa/Kappa/V2/Streams) documentation.
50
+
51
+ ```ruby
52
+ s = Stream.get('idrajit')
53
+ s.nil? # => false (currently live)
54
+ s.game_name # => "StarCraft II: Heart of the Swarm"
55
+ s.viewer_count # => 7267
56
+ s.channel.url # => "http://www.twitch.tv/idrajit"
57
+ ```
58
+
59
+ ### <a id="users"></a>Users
60
+
61
+ These are members of the Twitch community who have a Twitch account. If broadcasting, they can own a [stream](#streams) that they can broadcast on their [channel](#channels). If mainly viewing, they might follow or subscribe to channels.
62
+
63
+ See also [`User`](http://rdoc.info/gems/kappa/Kappa/V2/User) documentation.
64
+
65
+ ```ruby
66
+ u = User.get('snoopeh')
67
+ u.nil? # => false (user exists)
68
+ u.channel # => #<Kappa::V2::Channel>
69
+ u.following.map(&:name) # => ["national_esl1", "dreamhacklol", "riotgames"]
70
+ ```
71
+
72
+ ### <a id="videos"></a>Videos
73
+
74
+ Videos are broadcasts or highlights owned by a [channel](#channels). Broadcasts are unedited videos that are saved after a streaming session. Highlights are videos edited from broadcasts by the channel's owner.
75
+
76
+ See also [`Video`](http://rdoc.info/gems/kappa/Kappa/V2/Video) and [`Videos`](http://rdoc.info/gems/kappa/Kappa/V2/Videos) documentation.
77
+
78
+ ```ruby
79
+ v = Video.get('a395995729')
80
+ v.nil? # => false (video exists)
81
+ v.title # => "DreamHack Open Stockholm 26-27 April"
82
+ v.game_name # => "StarCraft II: Heart of the Swarm"
83
+ v.recorded_at # => 2013-04-26 18:33:48 UTC
84
+ v.view_count # => 12506
85
+ ```
86
+
87
+ ### <a id="teams"></a>Teams
88
+
89
+ Teams are an organization of [channels](#channels).
90
+
91
+ See also [`Team`](http://rdoc.info/gems/kappa/Kappa/V2/Team) documentation.
92
+
93
+ ```ruby
94
+ t = Team.get('teamliquid')
95
+ t.nil? # => false (team exists)
96
+ t.display_name # => "TeamLiquid"
97
+ t.info # => "TeamLiquid is awesome. and esports. video games. \n\n"
98
+ t.updated_at # => 2013-05-24 00:17:10 UTC
99
+ ```
100
+
101
+ ### <a id="games"></a>Games
102
+
103
+ Games are categories (e.g. League of Legends, Diablo 3) used by [streams](#streams) and [channels](#channels). Games can be searched for by query.
104
+
105
+ See also [`Game`](http://rdoc.info/gems/kappa/Kappa/V2/Game), [`Games`](http://rdoc.info/gems/kappa/Kappa/V2/Games), and [`GameSuggestion`](http://rdoc.info/gems/kappa/Kappa/V2/GameSuggestion) documentation.
106
+
107
+ ```ruby
108
+ top = Games.top(:limit => 3)
109
+ top.map(&:name) # => ["League of Legends", "Dota 2", "StarCraft II: Heart of the Swarm"]
110
+ ```
111
+
112
+ ```ruby
113
+ g = Games.top(:limit => 1).first
114
+ g.name # => "League of Legends"
115
+ g.channel_count # => 906
116
+ g.viewer_count # => 79223
117
+ g.box_images.medium_url # =>"http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg"
118
+ ```
119
+
120
+ ```ruby
121
+ s = Games.find(:name => 'diablo', :live => true)
122
+ s.map(&:name) # => ["Diablo III", "Diablo II", "Diablo", "Diablo II: Lord of Destruction"]
123
+ s.map(&:popularity) # => [120, 4, 1, 1]
124
+ ```
125
+
126
+ ## Examples
127
+
128
+ ## Documentation
129
+
130
+ - Current release: [http://rdoc.info/gems/kappa/frames](http://rdoc.info/gems/kappa/frames)
131
+ - Latest master: [http://rdoc.info/github/schmich/kappa/frames](http://rdoc.info/github/schmich/kappa/frames)
132
+ - Twitch REST API: [https://github.com/justintv/Twitch-API](https://github.com/justintv/Twitch-API)
133
+
134
+ ## Versioning
135
+
136
+ ### Library version
137
+
138
+ Kappa adheres to the [Semantic Versioning 2.0.0](http://semver.org/) specification. Most importantly, any
139
+ compatibility- or API-breaking changes will result in a new major version (e.g. `1.x.x` to `2.x.x`). Because
140
+ of this, you should use a [pessimistic version constraint](http://docs.rubygems.org/read/chapter/16#page74) when
141
+ taking a dependency on this library. For example:
142
+
143
+ ```ruby
144
+ gem 'kappa', '~> 1.0'
145
+ ```
146
+
147
+ Any new backwards-compatible features will result in a new minor version (e.g. `x.1.x` to `x.2.x`) while any
148
+ backwards-compatible bugfixes will result in a new patch version (e.g. `x.x.1` to `x.x.2`).
149
+
150
+ ### Twitch API versions
151
+
152
+ Twitch supports multiple versions of their API simultaneously, with each version potentially providing different data and behaving differently. Because of this, you must specify which version of the Twitch API you wish to use. With Kappa, this is done with modules.
153
+
154
+ For example, if you want to use the v2 Twitch API:
155
+
156
+ ```ruby
157
+ # Option 1: Include the module once.
158
+ include Kappa::V2
159
+ c = Channel.get('day9tv')
160
+ u = User.get('artosis')
161
+ ```
162
+ ```ruby
163
+ # Option 2: Specify the full class name each time.
164
+ c = Kappa::V2::Channel.get('day9tv')
165
+ u = Kappa::V2::User.get('artosis')
166
+ ```
167
+
168
+ ## Contributing
169
+
170
+ - [Fork and clone the repo.](http://help.github.com/fork-a-repo/)
171
+ - [Create a branch for your changes.](http://learn.github.com/p/branching.html)
172
+ - Run `bundle install` to install development requirements.
173
+ - Implement your feature or bug fix.
174
+ - Add specs under the `spec` folder to prevent regressions or to test new code.
175
+ - Add [YARD](http://rubydoc.info/docs/yard/file/docs/GettingStarted.md) documentation for new features. Run `rake yard` to view documentation.
176
+ - Run `rake coverage` to run specs with code coverage. All specs must pass; coverage must remain at 100%. Run `rake coverage:view` to see a detailed report.
177
+ - Commit and push your changes.
178
+ - [Submit a pull request.](http://help.github.com/send-pull-requests/)
179
+
180
+ ## License
181
+
182
+ Copyright &copy; 2013 Chris Schmich
183
+ <br />
184
+ MIT License, See [LICENSE](LICENSE) for details.
@@ -1,11 +1,12 @@
1
- require 'kappa/id_equality'
2
- require 'kappa/connection'
3
- require 'kappa/channel'
4
- require 'kappa/stream'
5
- require 'kappa/game'
6
- require 'kappa/video'
7
- require 'kappa/team'
8
- require 'kappa/user'
9
- require 'kappa/images'
10
- require 'kappa/twitch'
11
- require 'kappa/version'
1
+ require 'kappa/id_equality'
2
+ require 'kappa/proxy'
3
+ require 'kappa/connection'
4
+ require 'kappa/channel'
5
+ require 'kappa/stream'
6
+ require 'kappa/game'
7
+ require 'kappa/video'
8
+ require 'kappa/team'
9
+ require 'kappa/user'
10
+ require 'kappa/images'
11
+ require 'kappa/twitch'
12
+ require 'kappa/version'
@@ -1,184 +1,143 @@
1
- require 'cgi'
2
-
3
- module Kappa::V2
4
- # Channels serve as the home location for a user's content. Channels have a stream, can run
5
- # commercials, store videos, display information and status, and have a customized page including
6
- # banners and backgrounds.
7
- # @see .get Channel.get
8
- # @see Stream
9
- # @see User
10
- class Channel
11
- # TODO:
12
- # c.subscriptions
13
- # c.start_commercial
14
- # c.reset_stream_key
15
- # c.foo = 'bar' ; c.save!
16
- # Current user's channel
17
- include Connection
18
- include Kappa::IdEquality
19
-
20
- # @private
21
- def initialize(hash)
22
- @id = hash['_id']
23
- @background_url = hash['background']
24
- @banner_url = hash['banner']
25
- @created_at = DateTime.parse(hash['created_at'])
26
- @display_name = hash['display_name']
27
- @game_name = hash['game']
28
- @logo_url = hash['logo']
29
- @mature = hash['mature'] || false
30
- @name = hash['name']
31
- @status = hash['status']
32
- @updated_at = DateTime.parse(hash['updated_at'])
33
- @url = hash['url']
34
- @video_banner_url = hash['video_banner']
35
-
36
- @teams = []
37
- teams = hash['teams']
38
- teams.each do |team_json|
39
- @teams << Team.new(team_json)
40
- end
41
- end
42
-
43
- # Get a channel by name.
44
- # @param channel_name [String] The name of the channel to get. This is the same as the stream or user name.
45
- # @return [Channel] A valid `Channel` object if the channel exists, `nil` otherwise.
46
- def self.get(channel_name)
47
- encoded_name = CGI.escape(channel_name)
48
- json = connection.get("channels/#{encoded_name}")
49
-
50
- # HTTP 422 can happen if the channel is associated with a Justin.tv account.
51
- if !json || json['status'] == 404 || json['status'] == 422
52
- nil
53
- else
54
- new(json)
55
- end
56
- end
57
-
58
- # Does this channel have mature content? This flag is specified by the owner of the channel.
59
- # @return [Boolean] `true` if the channel has mature content, `false` otherwise.
60
- def mature?
61
- @mature
62
- end
63
-
64
- # Get the live stream associated with this channel.
65
- # @return [Stream] Live stream object for this channel, or `nil` if the channel is not currently streaming.
66
- # @see #streaming?
67
- def stream
68
- Stream.get(@name)
69
- end
70
-
71
- # Does this channel currently have a live stream?
72
- # @note This makes a separate request to get the channel's stream. If you want to actually use the stream object, you should call `#stream` instead.
73
- # @return [Boolean] `true` if the channel currently has a live stream, `false` otherwise.
74
- # @see #stream
75
- def streaming?
76
- !stream.nil?
77
- end
78
-
79
- #
80
- # GET /channels/:channel/editors
81
- # https://github.com/justintv/Twitch-API/blob/master/v2_resources/channels.md#get-channelschanneleditors
82
- #
83
- # @private
84
- # Private until implemented.
85
- def editors
86
- # TODO
87
- end
88
-
89
- #
90
- # GET /channels/:channels/videos
91
- # https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-channelschannelvideos
92
- #
93
- # @private
94
- # Private until implemented.
95
- def videos(params = {})
96
- # TODO
97
- end
98
-
99
- # TODO: Requires authentication.
100
- # @private
101
- # Private until implemented.
102
- def subscribers
103
- end
104
-
105
- #
106
- # GET /channels/:channel/subscriptions/:user
107
- # https://github.com/justintv/Twitch-API/blob/master/v2_resources/subscriptions.md#get-channelschannelsubscriptionsuser
108
- #
109
- # TODO: Requires authentication.
110
- # @private
111
- # Private until implemented.
112
- def has_subscriber?(user)
113
- # Support User object or username (string)
114
- end
115
-
116
- # Get the users following this channel.
117
- # @note The number of followers is potentially very large, so it's recommended that you specify a `:limit`.
118
- # @param options [Hash] Filter criteria.
119
- # @option options [Fixnum] :limit (none) Limit on the number of results returned.
120
- # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
121
- # @see User
122
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/channels.md#get-channelschannelfollows GET /channels/:channel/follows
123
- # @return [Array<User>] List of users following this channel.
124
- def followers(options = {})
125
- params = {}
126
-
127
- limit = options[:limit]
128
- if limit && (limit < 100)
129
- params[:limit] = limit
130
- else
131
- params[:limit] = 100
132
- limit = 0
133
- end
134
-
135
- return connection.accumulate(
136
- :path => "channels/#{@name}/follows",
137
- :params => params,
138
- :json => 'follows',
139
- :sub_json => 'user',
140
- :class => User,
141
- :limit => limit
142
- )
143
- end
144
-
145
- # @return [Fixnum] Unique Twitch ID.
146
- attr_reader :id
147
-
148
- # @return [String] URL for background image.
149
- attr_reader :background_url
150
-
151
- # @return [String] URL for banner image.
152
- attr_reader :banner_url
153
-
154
- # @return [DateTime] When the channel was created.
155
- attr_reader :created_at
156
-
157
- # @return [String] User-friendly display name. This name is used for the channel's page title.
158
- attr_reader :display_name
159
-
160
- # @return [String] Name of the primary game for this channel.
161
- attr_reader :game_name
162
-
163
- # @return [String] URL for the logo image.
164
- attr_reader :logo_url
165
-
166
- # @return [String] Unique Twitch name.
167
- attr_reader :name
168
-
169
- # @return [String] Current status set by the channel's owner.
170
- attr_reader :status
171
-
172
- # @return [DateTime] When the channel was last updated. When a stream is started, its channel is updated.
173
- attr_reader :updated_at
174
-
175
- # @return [String] The URL for the channel's main page.
176
- attr_reader :url
177
-
178
- # @return [String] URL for the image shown when the stream is offline.
179
- attr_reader :video_banner_url
180
-
181
- # @return [Array<Team>] The list of teams that this channel is associated with. Not all channels have associated teams.
182
- attr_reader :teams
183
- end
184
- end
1
+ require 'cgi'
2
+ require 'time'
3
+
4
+ module Kappa::V2
5
+ # Channels serve as the home location for a user's content. Channels have a stream, can run
6
+ # commercials, store videos, display information and status, and have a customized page including
7
+ # banners and backgrounds.
8
+ # @see .get Channel.get
9
+ # @see Stream
10
+ # @see User
11
+ class Channel
12
+ include Connection
13
+ include Kappa::IdEquality
14
+
15
+ # @private
16
+ def initialize(hash)
17
+ @id = hash['_id']
18
+ @background_url = hash['background']
19
+ @banner_url = hash['banner']
20
+ @created_at = Time.parse(hash['created_at']).utc
21
+ @display_name = hash['display_name']
22
+ @game_name = hash['game']
23
+ @logo_url = hash['logo']
24
+ @mature = hash['mature'] || false
25
+ @name = hash['name']
26
+ @status = hash['status']
27
+ @updated_at = Time.parse(hash['updated_at']).utc
28
+ @url = hash['url']
29
+ @video_banner_url = hash['video_banner']
30
+
31
+ @teams = []
32
+ teams = hash['teams']
33
+ teams.each do |team_json|
34
+ @teams << Team.new(team_json)
35
+ end
36
+ end
37
+
38
+ # Get a channel by name.
39
+ # @param channel_name [String] The name of the channel to get. This is the same as the stream or user name.
40
+ # @return [Channel] A valid `Channel` object if the channel exists, `nil` otherwise.
41
+ def self.get(channel_name)
42
+ encoded_name = CGI.escape(channel_name)
43
+ json = connection.get("channels/#{encoded_name}")
44
+
45
+ # HTTP 422 can happen if the channel is associated with a Justin.tv account.
46
+ if !json || json['status'] == 404 || json['status'] == 422
47
+ nil
48
+ else
49
+ new(json)
50
+ end
51
+ end
52
+
53
+ # Does this channel have mature content? This flag is specified by the owner of the channel.
54
+ # @return [Boolean] `true` if the channel has mature content, `false` otherwise.
55
+ def mature?
56
+ @mature
57
+ end
58
+
59
+ # Get the live stream associated with this channel.
60
+ # @note This incurs an additional web request.
61
+ # @return [Stream] Live stream object for this channel, or `nil` if the channel is not currently streaming.
62
+ # @see #streaming?
63
+ def stream
64
+ Stream.get(@name)
65
+ end
66
+
67
+ # Does this channel currently have a live stream?
68
+ # @note This makes a separate request to get the channel's stream. If you want to actually use the stream object, you should call `#stream` instead.
69
+ # @return [Boolean] `true` if the channel currently has a live stream, `false` otherwise.
70
+ # @see #stream
71
+ def streaming?
72
+ !stream.nil?
73
+ end
74
+
75
+ # Get the owner of this channel.
76
+ # @note This incurs an additional web request.
77
+ # @return [User] The user that owns this channel.
78
+ def user
79
+ User.get(@name)
80
+ end
81
+
82
+ # Get the users following this channel.
83
+ # @note The number of followers is potentially very large, so it's recommended that you specify a `:limit`.
84
+ # @param options [Hash] Filter criteria.
85
+ # @option options [Fixnum] :limit (none) Limit on the number of results returned.
86
+ # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
87
+ # @see User
88
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/channels.md#get-channelschannelfollows GET /channels/:channel/follows
89
+ # @return [Array<User>] List of users following this channel.
90
+ def followers(options = {})
91
+ params = {}
92
+
93
+ return connection.accumulate(
94
+ :path => "channels/#{@name}/follows",
95
+ :params => params,
96
+ :json => 'follows',
97
+ :sub_json => 'user',
98
+ :class => User,
99
+ :limit => options[:limit],
100
+ :offset => options[:offset]
101
+ )
102
+ end
103
+
104
+ # @return [Fixnum] Unique Twitch ID.
105
+ attr_reader :id
106
+
107
+ # @return [String] URL for background image.
108
+ attr_reader :background_url
109
+
110
+ # @return [String] URL for banner image.
111
+ attr_reader :banner_url
112
+
113
+ # @return [Time] When the channel was created (UTC).
114
+ attr_reader :created_at
115
+
116
+ # @return [String] User-friendly display name. This name is used for the channel's page title.
117
+ attr_reader :display_name
118
+
119
+ # @return [String] Name of the primary game for this channel.
120
+ attr_reader :game_name
121
+
122
+ # @return [String] URL for the logo image.
123
+ attr_reader :logo_url
124
+
125
+ # @return [String] Unique Twitch name.
126
+ attr_reader :name
127
+
128
+ # @return [String] Current status set by the channel's owner.
129
+ attr_reader :status
130
+
131
+ # @return [Time] When the channel was last updated (UTC). For example, when a stream is started, its channel is updated.
132
+ attr_reader :updated_at
133
+
134
+ # @return [String] The URL for the channel's main page.
135
+ attr_reader :url
136
+
137
+ # @return [String] URL for the image shown when the stream is offline.
138
+ attr_reader :video_banner_url
139
+
140
+ # @return [Array<Team>] The list of teams that this channel is associated with. Not all channels have associated teams.
141
+ attr_reader :teams
142
+ end
143
+ end