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,13 +1,13 @@
1
1
  require 'time'
2
2
 
3
- module Kappa::V2
3
+ module Twitch::V2
4
4
  # Teams are an organization of channels.
5
- # @see .get Team.get
5
+ # @see Teams#get Teams#get
6
+ # @see Teams#all Teams#all
6
7
  # @see Teams
7
8
  # @see Channel
8
9
  class Team
9
- include Connection
10
- include Kappa::IdEquality
10
+ include Twitch::IdEquality
11
11
 
12
12
  # @private
13
13
  def initialize(hash)
@@ -23,74 +23,96 @@ module Kappa::V2
23
23
  @url = "http://www.twitch.tv/team/#{@name}"
24
24
  end
25
25
 
26
- # Get a team by name.
27
- # @param team_name [String] The name of the team to get.
28
- # @return [Team] A valid `Team` object if the team exists, `nil` otherwise.
29
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/teams.md#get-teamsteam GET /teams/:team
30
- def self.get(team_name)
31
- json = connection.get("teams/#{team_name}")
32
- if json['status'] == 404
33
- nil
34
- else
35
- new(json)
36
- end
37
- end
38
-
26
+ # @example
27
+ # 12
39
28
  # @return [Fixnum] Unique Twitch ID.
40
29
  attr_reader :id
41
30
 
31
+ # @example
32
+ # "TeamLiquid is awesome. and esports. video games. \n\n"
42
33
  # @return [String] Info about the team. This is displayed on the team's page and can contain HTML.
43
34
  attr_reader :info
44
35
 
36
+ # @example
37
+ # "http://static-cdn.jtvnw.net/jtv_user_pictures/team-eg-background_image-da36973b6d829ac6.png"
45
38
  # @return [String] URL for background image.
46
39
  attr_reader :background_url
47
40
 
41
+ # @example
42
+ # "http://static-cdn.jtvnw.net/jtv_user_pictures/team-eg-banner_image-1ad9c4738f4698b1-640x125.png"
48
43
  # @return [String] URL for banner image.
49
44
  attr_reader :banner_url
50
45
 
46
+ # @example
47
+ # "http://static-cdn.jtvnw.net/jtv_user_pictures/team-eg-team_logo_image-9107b874d4c3fc3b-300x300.jpeg"
51
48
  # @return [String] URL for the logo image.
52
49
  attr_reader :logo_url
53
50
 
51
+ # @example
52
+ # "teamliquid"
53
+ # @see #display_name
54
54
  # @return [String] Unique Twitch name.
55
55
  attr_reader :name
56
56
 
57
+ # @example
58
+ # "TeamLiquid"
59
+ # @see #name
57
60
  # @return [String] User-friendly display name. This name is used for the team's page title.
58
61
  attr_reader :display_name
59
62
 
63
+ # @example
64
+ # 2013-05-24 00:17:10 UTC
60
65
  # @return [Time] When the team was last updated (UTC).
61
66
  attr_reader :updated_at
62
67
 
68
+ # @example
69
+ # 2011-10-27 01:00:44 UTC
63
70
  # @return [Time] When the team was created (UTC).
64
71
  attr_reader :created_at
65
72
 
66
73
  # @example
67
- # "http://www.twitch.tv/team/root"
74
+ # "http://www.twitch.tv/team/teamliquid"
68
75
  # @return [String] URL for the team's Twitch landing page.
69
76
  attr_reader :url
70
77
  end
71
78
 
72
- # Query class used for finding all active teams.
79
+ # Query class for finding all active teams.
73
80
  # @see Team
74
81
  class Teams
75
- include Connection
82
+ # @private
83
+ def initialize(query)
84
+ @query = query
85
+ end
86
+
87
+ # Get a team by name.
88
+ # @example
89
+ # Twitch.teams.get('teamliquid')
90
+ # @param team_name [String] The name of the team to get.
91
+ # @return [Team] A valid `Team` object if the team exists, `nil` otherwise.
92
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/teams.md#get-teamsteam GET /teams/:team
93
+ def get(team_name)
94
+ json = @query.connection.get("teams/#{team_name}")
95
+ if json['status'] == 404
96
+ nil
97
+ else
98
+ Team.new(json)
99
+ end
100
+ end
76
101
 
77
102
  # Get the list of all active teams.
78
103
  # @example
79
- # Teams.all
104
+ # Twitch.teams.all
80
105
  # @example
81
- # Teams.all(:limit => 10)
106
+ # Twitch.teams.all(:limit => 10)
82
107
  # @param options [Hash] Filter criteria.
83
108
  # @option options [Fixnum] :limit (none) Limit on the number of results returned.
84
109
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/teams.md#get-teams GET /teams
85
110
  # @return [Array<Team>] List of all active teams.
86
- def self.all(options = {})
87
- params = {}
88
-
89
- return connection.accumulate(
111
+ def all(options = {})
112
+ return @query.connection.accumulate(
90
113
  :path => 'teams',
91
- :params => params,
92
114
  :json => 'teams',
93
- :class => Team,
115
+ :create => Team,
94
116
  :limit => options[:limit],
95
117
  :offset => options[:offset]
96
118
  )
@@ -1,19 +1,20 @@
1
1
  require 'cgi'
2
2
  require 'time'
3
3
 
4
- module Kappa::V2
4
+ module Twitch::V2
5
5
  # These are members of the Twitch community who have a Twitch account. If broadcasting,
6
6
  # they can own a stream that they can broadcast on their channel. If mainly viewing,
7
7
  # they might follow or subscribe to channels.
8
- # @see .get User.get
8
+ # @see Users#get Users#get
9
+ # @see Users
9
10
  # @see Channel
10
11
  # @see Stream
11
12
  class User
12
- include Connection
13
- include Kappa::IdEquality
13
+ include Twitch::IdEquality
14
14
 
15
15
  # @private
16
- def initialize(hash)
16
+ def initialize(hash, query)
17
+ @query = query
17
18
  @id = hash['_id']
18
19
  @created_at = Time.parse(hash['created_at']).utc
19
20
  @display_name = hash['display_name']
@@ -23,26 +24,12 @@ module Kappa::V2
23
24
  @updated_at = Time.parse(hash['updated_at']).utc
24
25
  end
25
26
 
26
- # Get a user by name.
27
- # @param user_name [String] The name of the user to get. This is the same as the channel or stream name.
28
- # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/users.md#get-usersuser GET /users/:user
29
- # @return [User] A valid `User` object if the user exists, `nil` otherwise.
30
- def self.get(user_name)
31
- encoded_name = CGI.escape(user_name)
32
- json = connection.get("users/#{encoded_name}")
33
- if !json || json['status'] == 404
34
- nil
35
- else
36
- new(json)
37
- end
38
- end
39
-
40
27
  # Get the `Channel` associated with this user.
41
28
  # @note This incurs an additional web request.
42
29
  # @return [Channel] The `Channel` associated with this user, or `nil` if this is a Justin.tv account.
43
- # @see Channel.get
30
+ # @see Channel#get Channel#get
44
31
  def channel
45
- Channel.get(@name)
32
+ @query.channels.get(@name)
46
33
  end
47
34
 
48
35
  # Get the live stream associated with this user.
@@ -50,7 +37,7 @@ module Kappa::V2
50
37
  # @return [Stream] Live stream object for this user, or `nil` if the user is not currently streaming.
51
38
  # @see #streaming?
52
39
  def stream
53
- Stream.get(@name)
40
+ @query.streams.get(@name)
54
41
  end
55
42
 
56
43
  # Is this user currently streaming?
@@ -74,51 +61,87 @@ module Kappa::V2
74
61
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/follows.md#get-usersuserfollowschannels GET /users/:user/follows/channels
75
62
  # @return [Array<Channel>] List of channels the user is currently following.
76
63
  def following(options = {})
77
- return connection.accumulate(
64
+ return @query.connection.accumulate(
78
65
  :path => "users/#{@name}/follows/channels",
79
66
  :json => 'follows',
80
67
  :sub_json => 'channel',
81
- :class => Channel,
68
+ :create => -> hash { Channel.new(hash, @query) },
82
69
  :limit => options[:limit],
83
70
  :offset => options[:offset]
84
71
  )
85
72
  end
86
73
 
87
- # @param channel [String, Channel] The name of the channel (or `Channel` object) to check.
74
+ # @param channel [String/Channel/User/Stream/#name] The name of the channel to check.
88
75
  # @return [Boolean] `true` if the user is following the channel, `false` otherwise.
89
76
  # @see #following
90
77
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/follows.md#get-usersuserfollowschannelstarget GET /users/:user/follows/:channels/:target
91
- def following?(channel)
92
- channel_name = case channel
93
- when String
94
- channel
95
- when Channel
96
- channel.name
78
+ def following?(target)
79
+ name = if target.respond_to?(:name)
80
+ target.name
81
+ else
82
+ target.to_s
97
83
  end
98
84
 
99
- channel_name = CGI.escape(channel_name)
85
+ name = CGI.escape(name)
100
86
 
101
- json = connection.get("users/#{@name}/follows/channels/#{channel_name}")
87
+ json = @query.connection.get("users/#{@name}/follows/channels/#{name}")
102
88
  status = json['status']
103
89
  return !status || (status != 404)
104
90
  end
105
91
 
92
+ # @example
93
+ # 23945610
106
94
  # @return [Fixnum] Unique Twitch ID.
107
95
  attr_reader :id
108
96
 
97
+ # @example
98
+ # 2011-08-08 21:03:44 UTC
109
99
  # @return [Time] When the user account was created (UTC).
110
100
  attr_reader :created_at
111
101
 
102
+ # @example
103
+ # 2013-07-19 23:51:43 UTC
112
104
  # @return [Time] When the user account was last updated (UTC).
113
105
  attr_reader :updated_at
114
106
 
107
+ # @example
108
+ # "LAGTVMaximusBlack"
115
109
  # @return [String] User-friendly display name.
116
110
  attr_reader :display_name
117
111
 
112
+ # @example
113
+ # "http://static-cdn.jtvnw.net/jtv_user_pictures/lagtvmaximusblack-profile_image-4b77a2305f5d85c8-300x300.png"
118
114
  # @return [String] URL for the logo image.
119
115
  attr_reader :logo_url
120
116
 
117
+ # @example
118
+ # "lagtvmaximusblack"
121
119
  # @return [String] Unique Twitch name.
122
120
  attr_reader :name
123
121
  end
122
+
123
+ # Query class for finding users.
124
+ # @see User
125
+ class Users
126
+ # @private
127
+ def initialize(query)
128
+ @query = query
129
+ end
130
+
131
+ # Get a user by name.
132
+ # @example
133
+ # Twitch.users.get('totalbiscuit')
134
+ # @param user_name [String] The name of the user to get. This is the same as the channel or stream name.
135
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/users.md#get-usersuser GET /users/:user
136
+ # @return [User] A valid `User` object if the user exists, `nil` otherwise.
137
+ def get(user_name)
138
+ encoded_name = CGI.escape(user_name)
139
+ json = @query.connection.get("users/#{encoded_name}")
140
+ if !json || json['status'] == 404
141
+ nil
142
+ else
143
+ User.new(json, @query)
144
+ end
145
+ end
146
+ end
124
147
  end
@@ -1,3 +1,3 @@
1
- module Kappa
2
- VERSION = '0.3.0'
1
+ module Twitch
2
+ VERSION = '0.4.0'
3
3
  end
@@ -1,12 +1,13 @@
1
1
  require 'cgi'
2
2
  require 'time'
3
3
 
4
- module Kappa::V2
4
+ module Twitch::V2
5
5
  # @private
6
6
  class ChannelProxy
7
- def initialize(name, display_name)
7
+ def initialize(name, display_name, query)
8
8
  @name = name
9
9
  @display_name = display_name
10
+ @query = query
10
11
  end
11
12
 
12
13
  attr_reader :name
@@ -15,22 +16,22 @@ module Kappa::V2
15
16
  include Proxy
16
17
 
17
18
  proxy {
18
- Channel.get(@name)
19
+ @query.channels.get(@name)
19
20
  }
20
21
  end
21
22
 
22
23
  # Videos are broadcasts or highlights owned by a channel. Broadcasts are unedited
23
24
  # videos that are saved after a streaming session. Highlights are videos edited from
24
25
  # broadcasts by the channel's owner.
25
- # @see .get Video.get
26
+ # @see Videos#get Videos#get
27
+ # @see Videos#top Videos#top
26
28
  # @see Videos
27
29
  # @see Channel
28
30
  class Video
29
- include Connection
30
- include Kappa::IdEquality
31
+ include Twitch::IdEquality
31
32
 
32
33
  # @private
33
- def initialize(hash)
34
+ def initialize(hash, query)
34
35
  @id = hash['_id']
35
36
  @title = hash['title']
36
37
  @recorded_at = Time.parse(hash['recorded_at']).utc
@@ -40,48 +41,38 @@ module Kappa::V2
40
41
  @length = hash['length']
41
42
  @game_name = hash['game']
42
43
  @preview_url = hash['preview']
44
+ @embed_html = hash['embed']
43
45
 
44
46
  @channel = ChannelProxy.new(
45
47
  hash['channel']['name'],
46
- hash['channel']['display_name']
48
+ hash['channel']['display_name'],
49
+ query
47
50
  )
48
51
  end
49
52
 
50
- # Get a video by ID.
51
- # @example
52
- # v = Video.get('a396294648')
53
- # v.title # => "DreamHack Open Stockholm 26-27 April"
54
- # @param id [String] The ID of the video to get.
55
- # @raise [ArgumentError] If `id` is `nil` or blank.
56
- # @return [Video] A valid `Video` object if the video exists, `nil` otherwise.
57
- def self.get(id)
58
- raise ArgumentError if !id || id.strip.empty?
59
-
60
- encoded_id = CGI.escape(id)
61
- json = connection.get("videos/#{encoded_id}")
62
- if !json || json['status'] == 404
63
- nil
64
- else
65
- new(json)
66
- end
67
- end
68
-
69
53
  # @note This is a `String`, not a `Fixnum` like most other object IDs.
70
54
  # @example
71
- # v = Video.get('a396294648')
72
- # v.id # => "a396294648"
55
+ # "a396294648"
73
56
  # @return [String] Unique Twitch ID for this video.
74
57
  attr_reader :id
75
58
 
59
+ # @example
60
+ # "DreamHack Open Stockholm 26-27 April"
76
61
  # @return [String] Title of this video. This is seen on the video's page.
77
62
  attr_reader :title
78
63
 
64
+ # @example
65
+ # 2013-04-27 09:37:30 UTC
79
66
  # @return [Time] When this video was recorded (UTC).
80
67
  attr_reader :recorded_at
81
68
 
69
+ # @example
70
+ # "http://www.twitch.tv/dreamhacktv/b/396294648"
82
71
  # @return [String] URL of this video on Twitch.
83
72
  attr_reader :url
84
73
 
74
+ # @example
75
+ # 81754
85
76
  # @return [Fixnum] The number of views this video has received all-time.
86
77
  attr_reader :view_count
87
78
 
@@ -89,41 +80,72 @@ module Kappa::V2
89
80
  attr_reader :description
90
81
 
91
82
  # @example
92
- # v.length # => 4205 (1 hour, 10 minutes, 5 seconds)
83
+ # 4205 # (1 hour, 10 minutes, 5 seconds)
93
84
  # @return [Fixnum] The length of this video (seconds).
94
85
  attr_reader :length
95
86
 
87
+ # @example
88
+ # "StarCraft II: Heart of the Swarm"
96
89
  # @return [String] The name of the game played in this video.
97
90
  attr_reader :game_name
98
91
 
92
+ # @example
93
+ # "http://static-cdn.jtvnw.net/jtv.thumbs/archive-396294648-320x240.jpg"
99
94
  # @return [String] URL of a preview screenshot taken from the video stream.
100
95
  attr_reader :preview_url
101
96
 
102
97
  # @return [Channel] The channel on which this video was originally streamed.
103
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
104
  end
105
105
 
106
- # Query class used for finding top videos.
106
+ # Query class for finding videos.
107
107
  # @see Video
108
108
  class Videos
109
- include Connection
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
+ encoded_id = CGI.escape(id)
124
+ json = @query.connection.get("videos/#{encoded_id}")
125
+ if !json || json['status'] == 404
126
+ nil
127
+ else
128
+ Video.new(json, @query)
129
+ end
130
+ end
110
131
 
111
132
  # Get the list of most popular videos based on view count.
112
133
  # @note The number of videos returned is potentially very large, so it's recommended that you specify a `:limit`.
113
134
  # @example
114
- # Videos.top
135
+ # Twitch.videos.top
115
136
  # @example
116
- # Videos.top(:period => :month, :game => 'Super Meat Boy')
137
+ # Twitch.videos.top(:period => :month, :game => 'Super Meat Boy')
117
138
  # @example
118
- # Videos.top(:period => :all, :limit => 10)
139
+ # Twitch.videos.top(:period => :all, :limit => 10)
119
140
  # @param options [Hash] Filter criteria.
120
- # @option options [Symbol] :period (:week) Return videos only in this time period. Supported values are `:week`, `:month`, `:all`.
141
+ # @option options [Symbol] :period (:week) Return videos only in this time period. Valid values are `:week`, `:month`, `:all`.
121
142
  # @option options [String] :game (nil) Return videos only for this game.
122
143
  # @option options [Fixnum] :limit (none) Limit on the number of results returned.
123
144
  # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
124
145
  # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-videostop GET /videos/top
146
+ # @raise [ArgumentError] If `:period` is not one of `:week`, `:month`, or `:all`.
125
147
  # @return [Array<Video>] List of top videos.
126
- def self.top(options = {})
148
+ def top(options = {})
127
149
  params = {}
128
150
 
129
151
  if options[:game]
@@ -137,11 +159,11 @@ module Kappa::V2
137
159
 
138
160
  params[:period] = period.to_s
139
161
 
140
- return connection.accumulate(
162
+ return @query.connection.accumulate(
141
163
  :path => 'videos/top',
142
164
  :params => params,
143
165
  :json => 'videos',
144
- :class => Video,
166
+ :create => -> hash { Video.new(hash, @query) },
145
167
  :limit => options[:limit],
146
168
  :offset => options[:offset]
147
169
  )