viddler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-04-16
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ilya Sabanin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/ext/hash.rb
9
+ lib/ext/open_struct.rb
10
+ lib/viddler.rb
11
+ lib/viddler/api_spec.rb
12
+ lib/viddler/base.rb
13
+ lib/viddler/comment.rb
14
+ lib/viddler/multipart_params.rb
15
+ lib/viddler/request.rb
16
+ lib/viddler/user.rb
17
+ lib/viddler/version.rb
18
+ lib/viddler/video.rb
19
+ log/debug.log
20
+ script/console
21
+ script/destroy
22
+ script/generate
23
+ script/txt2html
24
+ setup.rb
25
+ tasks/deployment.rake
26
+ tasks/environment.rake
27
+ tasks/website.rake
28
+ test/test_helper.rb
29
+ test/test_viddler.rb
30
+ website/index.html
31
+ website/index.txt
32
+ website/javascripts/rounded_corners_lite.inc.js
33
+ website/stylesheets/screen.css
34
+ website/template.html.erb
@@ -0,0 +1,66 @@
1
+ = Viddler.rb
2
+
3
+ * http://viddler.rubyforge.com
4
+ * svn co https://apricode.svn.beanstalkapp.com/gems/trunk/viddler
5
+
6
+ == DESCRIPTION:
7
+
8
+ Ruby wrapper around Viddler.com[http://www.viddler.com] API.
9
+
10
+ == FEATURES:
11
+
12
+ Currently implemented API methods:
13
+
14
+ * viddler.users.register
15
+ * viddler.users.auth
16
+ * viddler.users.getProfile
17
+ * viddler.users.setProfile
18
+ * viddler.users.setOptions
19
+ * viddler.videos.getRecordToken
20
+ * viddler.videos.upload
21
+ * viddler.videos.getStatus
22
+ * viddler.videos.getDetails
23
+ * viddler.videos.getDetailsByUrl
24
+ * viddler.videos.setDetails
25
+ * viddler.videos.getByUser
26
+ * viddler.videos.getByTag
27
+ * viddler.videos.getFeatured
28
+
29
+ == NOT YET IMPLEMENTED:
30
+
31
+ * Ruby wrapper for video permissions.
32
+
33
+ == REQUIREMENTS:
34
+
35
+ * active_support[http://rubyforge.org/projects/activesupport/]
36
+ * rest-client[http://rubyforge.org/projects/rest-client/]
37
+ * mime-types[http://rubyforge.org/projects/mime-types/]
38
+
39
+ == INSTALL:
40
+
41
+ * sudo gem install viddler
42
+
43
+ == LICENSE:
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2008 Ilya Sabanin
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,70 @@
1
+ require 'viddler/version'
2
+
3
+ AUTHOR = 'Ilya Sabanin' # can also be an array of Authors
4
+ EMAIL = 'ilya.sabanin@gmail.com'
5
+ DESCRIPTION = 'Ruby wrapper around Viddler.com API.'
6
+ GEM_NAME = 'viddler' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'viddler' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = YAML.load(`svn info`)['Revision']
34
+ VERS = Viddler::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'viddler documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.developer(AUTHOR, EMAIL)
52
+ p.description = DESCRIPTION
53
+ p.summary = DESCRIPTION
54
+ p.url = HOMEPATH
55
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
56
+ p.test_globs = ["test/**/test_*.rb"]
57
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
58
+
59
+ # == Optional
60
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
61
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
62
+
63
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
64
+
65
+ end
66
+
67
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
68
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
69
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
70
+ $hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen active_support rest_client mime/types].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,10 @@
1
+ class Hash #:nodoc:
2
+
3
+ def assert_required_keys(keys)
4
+ for key in keys
5
+ raise(ArgumentError, "Missing required key(s): #{key}") unless self.keys.include?(key)
6
+ end
7
+ true
8
+ end
9
+
10
+ end
@@ -0,0 +1,7 @@
1
+ class OpenStruct #:nodoc:
2
+
3
+ def table
4
+ @table
5
+ end
6
+
7
+ end
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'active_support'
6
+ require 'ostruct'
7
+
8
+ require 'ext/open_struct'
9
+ require 'ext/hash'
10
+ require 'viddler/api_spec'
11
+ require 'viddler/base'
12
+ require 'viddler/multipart_params'
13
+ require 'viddler/request'
14
+ require 'viddler/video'
15
+ require 'viddler/comment'
16
+ require 'viddler/user'
17
+
18
+ # Module to encapsule the Viddler API.
19
+ module Viddler
20
+ end
@@ -0,0 +1,94 @@
1
+ module Viddler #:nodoc:
2
+
3
+ class ApiSpec #:nodoc:
4
+
5
+ USERS_REGISTER_ATTRS = {
6
+ :required => [
7
+ :user,
8
+ :email,
9
+ :fname,
10
+ :lname,
11
+ :password,
12
+ :question,
13
+ :answer,
14
+ :lang,
15
+ :termsaccepted
16
+ ],
17
+ :optional => [
18
+ :company
19
+ ]
20
+ }
21
+
22
+ USERS_SETPROFILE_ATTRS = {
23
+ :optional => [
24
+ :first_name,
25
+ :last_name,
26
+ :about_me,
27
+ :birthdate,
28
+ :gender,
29
+ :company,
30
+ :city
31
+ ]
32
+ }
33
+
34
+ USERS_SETOPTIONS_ATTRS = {
35
+ :optional => [
36
+ :show_account,
37
+ :tagging_enabled,
38
+ :commenting_enabled,
39
+ :show_related_videos,
40
+ :embedding_enabled,
41
+ :clicking_through_enabled,
42
+ :email_this_enabled,
43
+ :trackbacks_enabled,
44
+ :favourites_enabled,
45
+ :custom_logo_enabled
46
+ ]
47
+ }
48
+
49
+ VIDEOS_UPLOAD_ATTRS = {
50
+ :required => [
51
+ :title,
52
+ :description,
53
+ :tags,
54
+ :file,
55
+ :make_public
56
+ ]
57
+ }
58
+
59
+ VIDEOS_SETDETAILS_ATTRS = {
60
+ :optional => {
61
+ :title,
62
+ :description,
63
+ :tags,
64
+ :view_perm,
65
+ :view_users,
66
+ :view_use_secret,
67
+ :embed_perm,
68
+ :embed_users,
69
+ :commenting_perm,
70
+ :tagging_perm,
71
+ :download_perm,
72
+ :download_users
73
+ }
74
+ }
75
+
76
+ def self.check_attributes(viddler_method, attributes)
77
+ valid_attributes = viddler_method_to_const(viddler_method)
78
+ required = valid_attributes[:required] || Array.new
79
+ optional = valid_attributes[:optional] || Array.new
80
+
81
+ attributes.assert_valid_keys(required + optional)
82
+ attributes.assert_required_keys(required)
83
+ end
84
+
85
+ protected
86
+
87
+ def self.viddler_method_to_const(method)
88
+ const_name = method.gsub('.', '_').upcase
89
+ const_get("#{const_name}_ATTRS")
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,398 @@
1
+ module Viddler
2
+
3
+ # Generic Viddler exception class.
4
+ class ViddlerError < StandardError #:nodoc:
5
+ end
6
+
7
+ # Raised when username and password has not been set.
8
+ class AuthenticationRequiredError < ViddlerError #:nodoc:
9
+ def message
10
+ 'Method that you re trying to execute requires username and password.'
11
+ end
12
+ end
13
+
14
+ # Raised when calling not yet implemented API methods.
15
+ class NotImplementedError < ViddlerError #:nodoc:
16
+ def message
17
+ 'This method is not yet implemented.'
18
+ end
19
+ end
20
+
21
+ # A class that can be instantiated for access to a Viddler API.
22
+ #
23
+ # Examples:
24
+ #
25
+ # @viddler = Viddler::Base.new(API_KEY, USERNAME, PASSWORD)
26
+ #
27
+ # Username and password are optional, some API methods doesn't require them:
28
+ #
29
+ # @viddler = Viddler::Base.new(YOUR_API_KEY)
30
+ #
31
+ class Base
32
+ # Creates new viddler instance.
33
+ #
34
+ # Example:
35
+ #
36
+ # @viddler = Viddler::Base.new(api_key, username, password)
37
+ #
38
+ def initialize(api_key, username=nil, password=nil)
39
+ @api_key, @username, @password = api_key, username, password
40
+ @session_id = nil
41
+ end
42
+
43
+ # Implements <tt>viddler.users.auth[http://wiki.developers.viddler.com/index.php/Viddler.users.auth]</tt>.
44
+ #
45
+ # It's not necessary for you to call this method manually before each method that requires authentication. Viddler.rb will do that for you automatically. You can use this method for checking credentials and for trying if connection to Viddler works.
46
+ #
47
+ # Example:
48
+ #
49
+ # @viddler.authenticate
50
+ #
51
+ # Returns <tt>sessionid</tt> string.
52
+ #
53
+ def authenticate
54
+ raise AuthenticationRequiredError unless could_authenticate?
55
+ request = Viddler::Request.new(:get, 'users.auth')
56
+ request.run do |p|
57
+ p.api_key = @api_key
58
+ p.user = @username
59
+ p.password = @password
60
+ end
61
+ @session_id = request.response['auth']['sessionid']
62
+ end
63
+
64
+ def authenticated?
65
+ @session_id ? true : false
66
+ end
67
+
68
+ # Implements <tt>viddler.users.register[http://wiki.developers.viddler.com/index.php/Viddler.users.register]</tt>. <b>Restricted to Viddler qualified API keys only.</b>
69
+ #
70
+ # <tt>new_attributes</tt> hash should contain next required keys:
71
+ # * <tt>user:</tt> The chosen Viddler user name;
72
+ # * <tt>email:</tt> The email address of the user;
73
+ # * <tt>fname:</tt> The user's first name;
74
+ # * <tt>lname:</tt> The user's last name;
75
+ # * <tt>password:</tt> The user's password with Viddler;
76
+ # * <tt>question:</tt> The text of the user's secret question;
77
+ # * <tt>answer:</tt> The text of the answer to the secret question;
78
+ # * <tt>lang:</tt> The language of the user for the account (e.g. EN)
79
+ # * <tt>termsaccepted:</tt> "1" indicates the user has accepted Viddler's terms and conditions.
80
+ #
81
+ # and could contain next optional keys:
82
+ # * <tt>company:</tt> The user's company affiliation.
83
+ #
84
+ # Example:
85
+ #
86
+ # @viddler.register_user(:user => 'login', :email => 'mail@example.com', ...)
87
+ #
88
+ # Returns new user's username string.
89
+ #
90
+ def register_user(new_attributes={})
91
+ Viddler::ApiSpec.check_attributes('users.register', new_attributes)
92
+
93
+ request = Viddler::Request.new(:get, 'users.register')
94
+ request.run do |p|
95
+ p.api_key = @api_key
96
+ for param, value in new_attributes
97
+ p.send("#{param}=", value)
98
+ end
99
+ end
100
+ request.response['user']['username']
101
+ end
102
+
103
+ # Implements <tt>viddler.videos.upload[http://wiki.developers.viddler.com/index.php/Viddler.videos.upload]</tt>. Requires authentication.
104
+ #
105
+ # <tt>new_attributes</tt> hash should contain next required keys:
106
+ # * <tt>title:</tt> The video title;
107
+ # * <tt>description:</tt> The video description;
108
+ # * <tt>tags:</tt> The video tags;
109
+ # * <tt>file:</tt> The video file;
110
+ # * <tt>make_public:</tt> Use "1" for true and "0" for false to choose whether or not the video goes public when uploaded.
111
+ #
112
+ # Example:
113
+ #
114
+ # @viddler.upload_video(:title => 'Great Title', :file => File.open('/movies/movie.mov'), ...)
115
+ #
116
+ # Returns Viddler::Video instance.
117
+ #
118
+ def upload_video(new_attributes={})
119
+ authenticate unless authenticated?
120
+ Viddler::ApiSpec.check_attributes('videos.upload', new_attributes)
121
+
122
+ request = Viddler::Request.new(:post, 'videos.upload')
123
+ request.run do |p|
124
+ p.api_key = @api_key
125
+ p.sessionid = @session_id
126
+ for param, value in new_attributes
127
+ p.send("#{param}=", value)
128
+ end
129
+ end
130
+ Viddler::Video.new(request.response['video'])
131
+ end
132
+
133
+ # Implements <tt>viddler.users.getProfile[http://wiki.developers.viddler.com/index.php/Viddler.users.getProfile]</tt>.
134
+ #
135
+ # Example:
136
+ #
137
+ # @viddler.find_profile(viddler_username)
138
+ #
139
+ # Returns Viddler::User instance.
140
+ #
141
+ def find_profile(viddler_name)
142
+ request = Viddler::Request.new(:get, 'users.getProfile')
143
+ request.run do |p|
144
+ p.api_key = @api_key
145
+ p.user = viddler_name
146
+ end
147
+ Viddler::User.new request.response['user']
148
+ end
149
+
150
+ # Implements <tt>viddler.users.setProfile[http://wiki.developers.viddler.com/index.php/Viddler.users.setProfile]</tt>. Requires authentication.
151
+ #
152
+ # <tt>new_attributes</tt> hash could contain next optional keys:
153
+ # * <tt>first_name</tt>
154
+ # * <tt>last_name</tt>
155
+ # * <tt>about_me</tt>
156
+ # * <tt>birthdate:</tt> Birthdate in yyyy-mm-dd format;
157
+ # * <tt>gender:</tt> Either m or y.
158
+ # * <tt>company</tt>
159
+ # * <tt>city</tt>
160
+ #
161
+ # Example:
162
+ #
163
+ # @viddler.update_profile(:first_name => 'Vasya', :last_name => 'Pupkin')
164
+ #
165
+ # Returns Viddler::User instance.
166
+ #
167
+ def update_profile(new_attributes={})
168
+ authenticate unless authenticated?
169
+ Viddler::ApiSpec.check_attributes('users.setProfile', new_attributes)
170
+
171
+ request = Viddler::Request.new(:post, 'users.setProfile')
172
+ request.run do |p|
173
+ p.api_key = @api_key
174
+ p.sessionid = @session_id
175
+ for param, value in new_attributes
176
+ p.send("#{param}=", value)
177
+ end
178
+ end
179
+ Viddler::User.new request.response['user']
180
+ end
181
+
182
+ # Implements <tt>viddler.users.setOptions[http://wiki.developers.viddler.com/index.php/Viddler.users.setOptions]</tt>. Requires authentication. <b>Restricted to Viddler partners only.</b>
183
+ #
184
+ # <tt>new_attributes</tt> hash could contain next optional keys:
185
+ # * <tt>show_account:</tt> "1", "0" - Show/hide your account in Viddler. If you set it to "0" both your account and your videos won't be visible on viddler.com site
186
+ # * <tt>tagging_enabled:</tt> "1", "0" - Enable/disable tagging on all your videos
187
+ # * <tt>commenting_enabled:</tt> "1", "0" - Enable/disable commenting on all your videos
188
+ # * <tt>show_related_videos:</tt> "1", "0" - Show/hide related videos on all your videos
189
+ # * <tt>embedding_enabled:</tt> "1", "0" - Enable/disable embedding of off all your videos
190
+ # * <tt>clicking_through_enabled:</tt> "1", "0" - Enable/disable redirect to viddler site while clicking on embedded player
191
+ # * <tt>email_this_enabled:</tt> "1", "0" - Enable/disable email this option on all your videos
192
+ # * <tt>trackbacks_enabled:</tt> "1", "0" - Enable/disable trackbacks on all your videos
193
+ # * <tt>favourites_enabled:</tt> "1", "0" - Enable/disable favourites on all your videos
194
+ # * <tt>custom_logo_enabled:</tt> "1", "0" - Enable/disable custom logo on all your videos. Note that logo itself must be send to viddler manually.
195
+ #
196
+ # Example:
197
+ #
198
+ # @viddler.update_account(:show_account => '0')
199
+ #
200
+ # Returns number of updated parameters.
201
+ #
202
+ def update_account(new_attributes={})
203
+ authenticate unless authenticated?
204
+ Viddler::ApiSpec.check_attributes('users.setOptions', new_attributes)
205
+
206
+ request = Viddler::Request.new(:get, 'users.setOptions')
207
+ request.run do |p|
208
+ p.api_key = @api_key
209
+ p.sessionid = @session_id
210
+ for param, value in new_attributes
211
+ p.send("#{param}=", value)
212
+ end
213
+ end
214
+ request.response['updated'].to_i
215
+ end
216
+
217
+ # Implements <tt>viddler.videos.getStatus[http://wiki.developers.viddler.com/index.php/Viddler.vidoes.getStatus]</tt>.
218
+ #
219
+ # Example:
220
+ #
221
+ # @viddler.get_video_status(video_id)
222
+ #
223
+ # This methods returns OpenStruct instance with Viddler's status information on a given video. We don't control what Viddler returns and it's all basically technical internal information of Viddler. Use this on your own risk.
224
+ #
225
+ def get_video_status(video_id)
226
+ request = Viddler::Request.new(:get, 'videos.getStatus')
227
+ request.run do |p|
228
+ p.api_key = @api_key
229
+ p.video_id = video_id
230
+ end
231
+ OpenStruct.new request.response['video_status']
232
+ end
233
+
234
+ # Implements <tt>viddler.videos.getDetails[http://wiki.developers.viddler.com/index.php/Viddler.videos.getDetails]</tt>. Authentication is optional.
235
+ #
236
+ # Example:
237
+ #
238
+ # @viddler.find_video_by_id(video_id)
239
+ #
240
+ # Returns Viddler::Video instance.
241
+ #
242
+ def find_video_by_id(video_id)
243
+ # Authentication is optional for this request
244
+ authenticate if could_authenticate? and !authenticated?
245
+
246
+ request = Viddler::Request.new(:get, 'videos.getDetails')
247
+ request.run do |p|
248
+ p.api_key = @api_key
249
+ p.video_id = video_id
250
+ p.sessionid = @session_id if authenticated?
251
+ end
252
+ Viddler::Video.new(request.response['video'])
253
+ end
254
+
255
+ # Implements <tt>viddler.videos.getDetailsByUrl[http://wiki.developers.viddler.com/index.php/Viddler.videos.getDetailsByUrl]</tt>. Authentication is optional.
256
+ #
257
+ # Example:
258
+ #
259
+ # @viddler.find_video_by_url('http://www.viddler.com/explore/username/videos/video_num/')
260
+ #
261
+ # Returns Viddler::Video instance.
262
+ #
263
+ def find_video_by_url(video_url)
264
+ # Authentication is optional for this request
265
+ authenticate if could_authenticate? and !authenticated?
266
+
267
+ request = Viddler::Request.new(:get, 'videos.getDetailsByUrl')
268
+ request.run do |p|
269
+ p.sessionid = @session_id if authenticated?
270
+ p.api_key = @api_key
271
+ p.url = video_url
272
+ end
273
+ Viddler::Video.new(request.response['video'])
274
+ end
275
+
276
+ # Implements <tt>viddler.videos.setDetails[http://wiki.developers.viddler.com/index.php/Viddler.videos.setDetails]</tt>. Requires authentication.
277
+ #
278
+ # <tt>new_attributes</tt> hash could contain next optional keys:
279
+ # * <tt>title:</tt> Video title - 500 characters max
280
+ # * <tt>description:</tt> Video description
281
+ # * <tt>tags:</tt> List of tags to be set on video. Setting tags will update current tags set (both timed and global video tags). To set timed tag use format tagname[timestamp_in_ms] as tagname. For example - using tag1,tag2,tag3[2500] will set 2 global and 1 timed tag at 2.5s
282
+ # * <tt>view_perm:</tt> View permission. Can be set to public, shared_all, shared or private
283
+ # * <tt>view_users:</tt> List of users which may view this video if view_perm is set to shared. Only your viddler friends are allowed here. If you provide multiple usernames - non valid viddler friends usernames will be ignored.
284
+ # * <tt>view_use_secret:</tt> If view_perm is set to non public value, you may activate secreturl for your video. If you want to enable or regenerate secreturl pass "1" as parameter value. If you want to disable secreturl pass "0" as parameter value.
285
+ # * <tt>embed_perm:</tt> Embedding permission. Supported permission levels are the same as for view_perm. This and all permissions below cannot be less restrictive than view_perm. You cannot set it to public if view_perm is for example shared.
286
+ # * <tt>embed_users:</tt> Same as view_users. If view_perm is shared, this list cannot contain more users than view_users. Invalid usernames will be removed.
287
+ # * <tt>commenting_perm:</tt> Commenting permission. Description is the same as for embed_perm
288
+ # * <tt>commenting_users:</tt> Same as embed_users.
289
+ # * <tt>tagging_perm:</tt> Tagging permission. Description is the same as for embed_perm
290
+ # * <tt>tagging_users:</tt> Same as embed_users.
291
+ # * <tt>download_perm:</tt> Download permission. Description is the same as for embed_perm
292
+ # * <tt>download_users:</tt> Same as embed_users.
293
+ #
294
+ # Example:
295
+ #
296
+ # @viddler.update_video(video_id, :title => 'Brand new title')
297
+ #
298
+ # Returns Viddler::Video instance.
299
+ #
300
+ def update_video(video_id, new_attributes={})
301
+ authenticate unless authenticated?
302
+ Viddler::ApiSpec.check_attributes('videos.setDetails', new_attributes)
303
+
304
+ request = Viddler::Request.new(:get, 'videos.setDetails')
305
+ request.run do |p|
306
+ p.api_key = @api_key
307
+ p.sessionid = @session_id
308
+ p.video_id = video_id
309
+ for param, value in new_attributes
310
+ p.send("#{param}=", value)
311
+ end
312
+ end
313
+ Viddler::Video.new(request.response['video'])
314
+ end
315
+
316
+ # Implements <tt>viddler.videos.getByUser[http://wiki.developers.viddler.com/index.php/Viddler.videos.getByUser]</tt>. Authentication is optional.
317
+ #
318
+ # Options hash could contain next values:
319
+ # * <tt>page</tt>: The "page number" of results to retrieve (e.g. 1, 2, 3);
320
+ # * <tt>per_page</tt>: The number of results to retrieve per page (maximum 100). If not specified, the default value equals 20.
321
+ #
322
+ # Example:
323
+ #
324
+ # @viddler.find_all_videos_by_user(username, :page => 2)
325
+ #
326
+ # Returns array of Viddler::Video instances.
327
+ #
328
+ def find_all_videos_by_user(username, options={})
329
+ authenticate if could_authenticate? and !authenticated?
330
+
331
+ options.assert_valid_keys(:page, :per_page)
332
+
333
+ request = Viddler::Request.new(:get, 'videos.getByUser')
334
+ request.run do |p|
335
+ p.api_key = @api_key
336
+ p.sessionid = @session_id if authenticated?
337
+ p.user = username
338
+ p.page = options[:page] || 1
339
+ p.per_page = options[:per_page] || 20
340
+ end
341
+ parse_videos_list(request.response['video_list'])
342
+ end
343
+
344
+ # Implements <tt>viddler.videos.getByTag[http://wiki.developers.viddler.com/index.php/Viddler.videos.getByTag]</tt>.
345
+ #
346
+ # Options hash could contain next values:
347
+ # * <tt>page</tt>: The "page number" of results to retrieve (e.g. 1, 2, 3);
348
+ # * <tt>per_page</tt>: The number of results to retrieve per page (maximum 100). If not specified, the default value equals 20.
349
+ #
350
+ # Example:
351
+ #
352
+ # @viddler.find_all_videos_by_tag('super tag', :per_page => 5)
353
+ #
354
+ # Returns array of Viddler::Video instances.
355
+ #
356
+ def find_all_videos_by_tag(tag_name, options={})
357
+ options.assert_valid_keys(:page, :per_page)
358
+
359
+ request = Viddler::Request.new(:get, 'videos.getByTag')
360
+ request.run do |p|
361
+ p.api_key = @api_key
362
+ p.tag = tag_name
363
+ p.page = options[:page] || 1
364
+ p.per_page = options[:per_page] || 20
365
+ end
366
+ parse_videos_list(request.response['video_list'])
367
+ end
368
+
369
+ # Implements <tt>viddler.videos.getFeatured[http://wiki.developers.viddler.com/index.php/Viddler.videos.getFeatured]</tt>.
370
+ #
371
+ # Example:
372
+ #
373
+ # @viddler.find_all_featured_videos
374
+ #
375
+ # Returns array of Viddler::Video instances.
376
+ #
377
+ def find_all_featured_videos
378
+ request = Viddler::Request.new(:get, 'videos.getFeatured')
379
+ request.run do |p|
380
+ p.api_key = @api_key
381
+ end
382
+ parse_videos_list(request.response['video_list'])
383
+ end
384
+
385
+ private
386
+
387
+ def could_authenticate?
388
+ @username and @password
389
+ end
390
+
391
+ def parse_videos_list(video_list)
392
+ video_list['video'].collect do |attr|
393
+ next unless attr.is_a?(Hash)
394
+ Viddler::Video.new(attr)
395
+ end
396
+ end
397
+ end
398
+ end