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 +1,3 @@
1
- $version = '0.2.0'
1
+ module Kappa
2
+ VERSION = '0.3.0'
3
+ end
@@ -1,100 +1,150 @@
1
- require 'cgi'
2
-
3
- module Kappa::V2
4
- # Videos are broadcasts or highlights owned by a channel. Broadcasts are unedited
5
- # videos that are saved after a streaming session. Highlights are videos edited from
6
- # broadcasts by the channel's owner.
7
- # @see .get Video.get
8
- # @see Videos
9
- # @see Channel
10
- class Video
11
- include Connection
12
- include Kappa::IdEquality
13
-
14
- # @private
15
- def initialize(hash)
16
- @id = hash['_id']
17
- @title = hash['title']
18
- @recorded_at = DateTime.parse(hash['recorded_at'])
19
- @url = hash['url']
20
- @view_count = hash['views']
21
- @description = hash['description']
22
- @length_sec = hash['length']
23
- @game_name = hash['game']
24
- @preview_url = hash['preview']
25
- @channel_name = hash['channel']['name']
26
- # @channel_display_name = json['channel']['display_name']
27
- end
28
-
29
- # Get a video by ID.
30
- # @example
31
- # v = Video.get('a396294648')
32
- # v.title # => "DreamHack Open Stockholm 26-27 April"
33
- # @param id [String] The ID of the video to get.
34
- # @raise [ArgumentError] If `id` is `nil` or blank.
35
- # @return [Video] A valid `Video` object if the video exists, `nil` otherwise.
36
- def self.get(id)
37
- raise ArgumentError if !id || id.strip.empty?
38
-
39
- encoded_id = CGI.escape(id)
40
- json = connection.get("videos/#{encoded_id}")
41
- if !json || json['status'] == 404
42
- nil
43
- else
44
- new(json)
45
- end
46
- end
47
-
48
- # @note This incurs an additional web request.
49
- # @return [Channel] The channel on which this video was originally streamed.
50
- def channel
51
- Channel.new(connection.get("channels/#{@channel_name}"))
52
- end
53
-
54
- # @note This is a `String`, not a `Fixnum` like most other object IDs.
55
- # @example
56
- # v = Video.get('a396294648')
57
- # v.id # => "a396294648"
58
- # @return [String] Unique Twitch ID for this video.
59
- attr_reader :id
60
-
61
- # @return [String] Title of this video. This is seen on the video's page.
62
- attr_reader :title
63
-
64
- # @return [DateTime] When this video was recorded.
65
- attr_reader :recorded_at
66
-
67
- # @return [String] URL of this video on Twitch.
68
- attr_reader :url
69
-
70
- # @return [Fixnum] The number of views this video has received all-time.
71
- attr_reader :view_count
72
-
73
- # @return [String] Description of this video.
74
- attr_reader :description
75
-
76
- # @return [Fixnum] The length of this video in seconds.
77
- attr_reader :length_sec # TODO: Is this actually in seconds? Doesn't seem to match up with video length.
78
-
79
- # @return [String] The name of the game played in this video.
80
- attr_reader :game_name
81
-
82
- # @return [String] URL of a preview screenshot taken from the video stream.
83
- attr_reader :preview_url
84
-
85
- # @return [String] The name of the channel on which this video was originally streamed.
86
- attr_reader :channel_name # TODO: Move this under "v.channel.name" and force the query if other attributes are requested.
87
-
88
- # TODO: embed (HTML embed code)
89
- end
90
-
91
- # Query class used for finding top videos.
92
- # @see Video
93
- class Videos
94
- # @private
95
- # Private until implemented.
96
- def self.top(params = {})
97
- # TODO
98
- end
99
- end
100
- end
1
+ require 'cgi'
2
+ require 'time'
3
+
4
+ module Kappa::V2
5
+ # @private
6
+ class ChannelProxy
7
+ def initialize(name, display_name)
8
+ @name = name
9
+ @display_name = display_name
10
+ end
11
+
12
+ attr_reader :name
13
+ attr_reader :display_name
14
+
15
+ include Proxy
16
+
17
+ proxy {
18
+ Channel.get(@name)
19
+ }
20
+ end
21
+
22
+ # Videos are broadcasts or highlights owned by a channel. Broadcasts are unedited
23
+ # videos that are saved after a streaming session. Highlights are videos edited from
24
+ # broadcasts by the channel's owner.
25
+ # @see .get Video.get
26
+ # @see Videos
27
+ # @see Channel
28
+ class Video
29
+ include Connection
30
+ include Kappa::IdEquality
31
+
32
+ # @private
33
+ def initialize(hash)
34
+ @id = hash['_id']
35
+ @title = hash['title']
36
+ @recorded_at = Time.parse(hash['recorded_at']).utc
37
+ @url = hash['url']
38
+ @view_count = hash['views']
39
+ @description = hash['description']
40
+ @length = hash['length']
41
+ @game_name = hash['game']
42
+ @preview_url = hash['preview']
43
+
44
+ @channel = ChannelProxy.new(
45
+ hash['channel']['name'],
46
+ hash['channel']['display_name']
47
+ )
48
+ end
49
+
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
+ # @note This is a `String`, not a `Fixnum` like most other object IDs.
70
+ # @example
71
+ # v = Video.get('a396294648')
72
+ # v.id # => "a396294648"
73
+ # @return [String] Unique Twitch ID for this video.
74
+ attr_reader :id
75
+
76
+ # @return [String] Title of this video. This is seen on the video's page.
77
+ attr_reader :title
78
+
79
+ # @return [Time] When this video was recorded (UTC).
80
+ attr_reader :recorded_at
81
+
82
+ # @return [String] URL of this video on Twitch.
83
+ attr_reader :url
84
+
85
+ # @return [Fixnum] The number of views this video has received all-time.
86
+ attr_reader :view_count
87
+
88
+ # @return [String] Description of this video.
89
+ attr_reader :description
90
+
91
+ # @example
92
+ # v.length # => 4205 (1 hour, 10 minutes, 5 seconds)
93
+ # @return [Fixnum] The length of this video (seconds).
94
+ attr_reader :length
95
+
96
+ # @return [String] The name of the game played in this video.
97
+ attr_reader :game_name
98
+
99
+ # @return [String] URL of a preview screenshot taken from the video stream.
100
+ attr_reader :preview_url
101
+
102
+ # @return [Channel] The channel on which this video was originally streamed.
103
+ attr_reader :channel
104
+ end
105
+
106
+ # Query class used for finding top videos.
107
+ # @see Video
108
+ class Videos
109
+ include Connection
110
+
111
+ # Get the list of most popular videos based on view count.
112
+ # @note The number of videos returned is potentially very large, so it's recommended that you specify a `:limit`.
113
+ # @example
114
+ # Videos.top
115
+ # @example
116
+ # Videos.top(:period => :month, :game => 'Super Meat Boy')
117
+ # @example
118
+ # Videos.top(:period => :all, :limit => 10)
119
+ # @param options [Hash] Filter criteria.
120
+ # @option options [Symbol] :period (:week) Return videos only in this time period. Supported values are `:week`, `:month`, `:all`.
121
+ # @option options [String] :game (nil) Return videos only for this game.
122
+ # @option options [Fixnum] :limit (none) Limit on the number of results returned.
123
+ # @option options [Fixnum] :offset (0) Offset into the result set to begin enumeration.
124
+ # @see https://github.com/justintv/Twitch-API/blob/master/v2_resources/videos.md#get-videostop GET /videos/top
125
+ # @return [Array<Video>] List of top videos.
126
+ def self.top(options = {})
127
+ params = {}
128
+
129
+ if options[:game]
130
+ params[:game] = options[:game]
131
+ end
132
+
133
+ period = options[:period] || :week
134
+ if ![:week, :month, :all].include?(period)
135
+ raise ArgumentError, 'period'
136
+ end
137
+
138
+ params[:period] = period.to_s
139
+
140
+ return connection.accumulate(
141
+ :path => 'videos/top',
142
+ :params => params,
143
+ :json => 'videos',
144
+ :class => Video,
145
+ :limit => options[:limit],
146
+ :offset => options[:offset]
147
+ )
148
+ end
149
+ end
150
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kappa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,99 +9,155 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-03 00:00:00.000000000 Z
12
+ date: 2013-07-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &24580620 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
21
  version: 0.9.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *24580620
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: addressable
27
- requirement: &24580296 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
- - - ! '>='
35
+ - - ~>
31
36
  - !ruby/object:Gem::Version
32
37
  version: 2.3.3
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *24580296
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.3
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rake
38
- requirement: &24579948 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
- - - ! '>='
51
+ - - ~>
42
52
  - !ruby/object:Gem::Version
43
53
  version: '0.9'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *24579948
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: webmock
49
- requirement: &24579624 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
- - - ! '>='
67
+ - - ~>
53
68
  - !ruby/object:Gem::Version
54
69
  version: 1.11.0
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *24579624
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.11.0
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rspec
60
- requirement: &24579288 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
- - - ! '>='
83
+ - - ~>
64
84
  - !ruby/object:Gem::Version
65
85
  version: 2.13.0
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *24579288
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.13.0
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: launchy
71
- requirement: &24578952 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
- - - ! '>='
99
+ - - ~>
75
100
  - !ruby/object:Gem::Version
76
101
  version: 2.3.0
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *24578952
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 2.3.0
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: yard
82
- requirement: &24578604 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
- - - ! '>='
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.8.6
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
86
124
  - !ruby/object:Gem::Version
87
125
  version: 0.8.6
126
+ - !ruby/object:Gem::Dependency
127
+ name: markdown
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.1.1
88
134
  type: :development
89
135
  prerelease: false
90
- version_requirements: *24578604
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.1.1
91
142
  - !ruby/object:Gem::Dependency
92
143
  name: simplecov
93
- requirement: &24578280 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
94
145
  none: false
95
146
  requirements:
96
- - - ! '>='
147
+ - - ~>
97
148
  - !ruby/object:Gem::Version
98
149
  version: 0.7.1
99
150
  type: :development
100
151
  prerelease: false
101
- version_requirements: *24578280
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 0.7.1
102
158
  - !ruby/object:Gem::Dependency
103
159
  name: coveralls
104
- requirement: &24578004 !ruby/object:Gem::Requirement
160
+ requirement: !ruby/object:Gem::Requirement
105
161
  none: false
106
162
  requirements:
107
163
  - - ! '>='
@@ -109,7 +165,12 @@ dependencies:
109
165
  version: '0'
110
166
  type: :development
111
167
  prerelease: false
112
- version_requirements: *24578004
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
113
174
  description: ! " A Ruby library for interfacing with the Twitch.tv API\n including
114
175
  users, channels, streams, and followers.\n"
115
176
  email: schmch@gmail.com
@@ -117,18 +178,19 @@ executables: []
117
178
  extensions: []
118
179
  extra_rdoc_files: []
119
180
  files:
120
- - lib/kappa/channel.rb
121
- - lib/kappa/connection.rb
122
- - lib/kappa/game.rb
123
- - lib/kappa/id_equality.rb
124
- - lib/kappa/images.rb
181
+ - lib/kappa.rb
182
+ - lib/kappa/version.rb
125
183
  - lib/kappa/stream.rb
184
+ - lib/kappa/video.rb
126
185
  - lib/kappa/team.rb
127
- - lib/kappa/twitch.rb
186
+ - lib/kappa/images.rb
128
187
  - lib/kappa/user.rb
129
- - lib/kappa/version.rb
130
- - lib/kappa/video.rb
131
- - lib/kappa.rb
188
+ - lib/kappa/id_equality.rb
189
+ - lib/kappa/twitch.rb
190
+ - lib/kappa/channel.rb
191
+ - lib/kappa/proxy.rb
192
+ - lib/kappa/game.rb
193
+ - lib/kappa/connection.rb
132
194
  - README.md
133
195
  homepage: https://github.com/schmich/kappa
134
196
  licenses: []
@@ -150,7 +212,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
212
  version: '0'
151
213
  requirements: []
152
214
  rubyforge_project:
153
- rubygems_version: 1.8.15
215
+ rubygems_version: 1.8.23
154
216
  signing_key:
155
217
  specification_version: 3
156
218
  summary: Ruby library for interfacing with the Twitch.tv API.