Parent

Vzaar::Base

You can use Vzaar::Base class for accessing and managing your resources on vzaar.

Public Class Methods

new(options= {}) click to toggle source

When creating a Vzaar::Base instance you can (but don’t have to) specify login and application_token. However if you don’t specify them you won’t be able to perform authenticated calls.

You can also specify server different from live server, e.g. sandbox.vzaar.com, if you’re just doing testing. Additionally you can pass your logger as the :logger option. By default the log/debug info is written to the standard output.

The options can be read from environment variables. Just set VZAAR_LOGIN, VZAAR_APPLICATION_TOKEN and/or VZAAR_SERVER and you don’t have to worry about passing the options to the initializer.

Usage:

  • vzaar = Vzaar::Base.new

  • vzaar = Vzaar::Base.new :login => ‘your_vzaar_login’, :application_token => ‘your_very_long_application_token‘

  • vzaar = Vzaar::Base.new :login => ‘your_vzaar_login’, :application_token => ‘your_app_token’, :server => ‘sandbox.vzaar.com’

  • vzaar = Vzaar::Base.new :server => ‘sandbox.vzaar.com’, :logger => your_logger

  • vzaar = Vzaar::Base.new :logger => your_logger

    # File lib/vzaar/base.rb, line 24
24:     def initialize(options= {})
25:       login = options[:login] || ENV['VZAAR_LOGIN'] || ''
26:       application_token = options[:application_token] || ENV['VZAAR_APPLICATION_TOKEN'] || ''
27:       server = options[:server] || ENV['VZAAR_SERVER'] || VZAAR_LIVE_SERVER
28:       @logger = options[:logger] || Logger.new(STDOUT)
29: 
30:       server.gsub! 'http://', ''
31:       server.gsub! 'https://', ''
32:       consumer = OAuth::Consumer.new '', '', { :site => "http://#{server}" }
33:       @public_connection = OAuth::AccessToken.new consumer, '', ''
34:       consumer = OAuth::Consumer.new '', '', { :site => "https://#{server}" }
35:       if login.length > 0 and application_token.length > 0
36:         @auth_connection = OAuth::AccessToken.new consumer, login, application_token
37:       else
38:         # Authenticated requests won't be possible
39:         @auth_connection = nil
40:         log_info "Authenticated calls won't be possible"
41:       end
42:     end

Public Instance Methods

account_type(account_type_id) click to toggle source

Gets the details of an account type.

Usage:

  • account_type = vzaar.account_type 1

  • title = vzaar.account_type(1).title

  • bandwidth = vzaar.account_type(1).bandwidth

    # File lib/vzaar/base.rb, line 63
63:     def account_type(account_type_id)
64:       result = nil
65:       public_connection(HTTP_GET, "/api/accounts/#{account_type_id}.xml") do |xml|
66:         result = AccountType.new xml
67:       end
68:       result
69:     end
delete_video(video_id, method = HTTP_DELETE) click to toggle source

Deletes a video from a users account. Use either ‘DELETE’ or ‘POST’ method. You must be the owner of the video in order to authorize on the server.

Usage:

  • vzaar.delete_video 1234 (uses ‘DELETE’ method)

  • vzaar.delete_video 1234, ‘POST’

     # File lib/vzaar/base.rb, line 156
156:     def delete_video(video_id, method = HTTP_DELETE)
157:       if method == HTTP_DELETE
158:         auth_connection method, "/api/videos/#{video_id}.xml"
159:       else
160:         request_xml = %{
161:           <?xml version="1.0" encoding="UTF-8"?>
162:           <vzaar-api>
163:             <_method>delete</_method>
164:           </vzaar-api>
165:         }
166:         auth_connection method, "/api/videos/#{video_id}.xml", request_xml
167:       end
168:     end
edit_video(video_id, title, description, method = HTTP_PUT) click to toggle source

Edits a video title and description. Use either ‘PUT’ or ‘POST’ method.

Usage:

  • vzaar.edit_video 1234, ‘new title’, ‘new desc’ (uses ‘PUT’ method)

  • vzaar.edit_video 1234, ‘new title’, ‘new desc’, ‘POST’

     # File lib/vzaar/base.rb, line 175
175:     def edit_video(video_id, title, description, method = HTTP_PUT)
176:       request_xml = %{
177:         <?xml version="1.0" encoding="UTF-8"?>
178:         <vzaar-api>
179:       }
180:       request_xml += %{<_method>put</_method>} if method != HTTP_PUT
181:       request_xml += %{
182:           <video>
183:             <title>#{title}</title>
184:             <description>#{description}</description >
185:           </video>
186:         </vzaar-api>
187:       }
188:       auth_connection HTTP_PUT, "/api/videos/#{video_id}.xml", request_xml
189:     end
process_video(options = {}) click to toggle source

Tells vzaar that you have uploaded a video to S3 and now you want to register it in vzaar. This method is called automatically from within the upload_video method.

Usage:

  • vzaar.process_video :guid => signature.guid,

:title => ‘Some title’, :description => ‘Some description’, :profile => 1, :transcoding => true

     # File lib/vzaar/base.rb, line 231
231:     def process_video(options = {})
232:       request_xml = %{
233:         <?xml version="1.0" encoding="UTF-8"?>
234:         <vzaar-api>
235:           <video>
236:             <guid>#{options[:guid]}</guid>
237:             <title>#{options[:title]}</title>
238:             <description>#{options[:description]}</description>
239:             <profile>#{options[:profile]}</profile>
240:       }
241:       if !options[:transcoding].nil?
242:         request_xml += %{
243:             <transcoding>#{options[:transcoding]}</transcoding>
244:         }
245:       end
246:       request_xml += %{ 
247:           </video>
248:         </vzaar-api>
249:       }
250:       auth_connection HTTP_POST, '/api/videos', request_xml
251:     end
signature(options = {}) click to toggle source

Provides a signature which is required to upload a video directly to S3 bucket. Options:

  • success_action_redirect - when sending files to S3 you can be redirected to a given url on success. You’ll need to specify the url when requesting a signature. Vzaar API server will attach a guid to it and return full url in the response. You’ll need to specify the full url later when uploading a video in order to get authorized on S3.

  • include_metadata - if you set the param to true, then when uploading a video you can and have to(!) send metadata to S3 along with your video. The names of the metadata must be: ‘x-amz-meta-title’ and ‘x-amz-meta-profile’. None of them can be omitted even if empty. Vzaar doesn’t restric the values of the metadata in any way. If include_metadata is false, which is the default behaviour, no metadata can be send to S3.

  • flash_request - adds flash specific params to the signature

Usage:

  • vzaar.signature

  • vzaar.signature :success_action_redirect => ‘my.domain.com/using_vzaar

  • vzaar.signature :success_action_redirect => ‘my.domain.com/using_vzaar’, :include_metadata => true

  • vzaar.signature :include_metadata => true

  • vzaar.signature :flash_request => true

     # File lib/vzaar/base.rb, line 203
203:     def signature(options = {})
204:       signature = nil
205:       url = '/api/videos/signature'
206:       if options[:success_action_redirect]
207:         url += "?success_action_redirect=#{options[:success_action_redirect]}"
208:       end
209:       if options[:include_metadata]
210:         url += url.include?('?') ? '&' : '?'
211:         url += "include_metadata=yes"
212:       end
213:       if options[:flash_request]
214:         url += url.include?('?') ? '&' : '?'
215:         url += "flash_request=yes"
216:       end
217:       auth_connection HTTP_GET, url do |xml|
218:         signature = Signature.new xml
219:       end
220:       signature
221:     end
upload_video(path, title, description, profile, transcoding = nil) click to toggle source

Uploads a video to vzaar. You can force transcoding video by setting transcoding param to true, you can force DNE (Do not encode) by setting transcoding param to false. When trancoding == nil (default) then user settings on vzaar will decide whether to encode or DNE.

Usage:

  • vzaar.upload_video ‘/home/me/video.mp4’, ‘some title’, ‘some desc’, ‘1’

  • vzaar.upload_video ‘/home/me/video.mp4’, ““

     # File lib/vzaar/base.rb, line 261
261:     def upload_video(path, title, description, profile, transcoding = nil)
262:       # Get signature
263:       sig = signature
264:       @logger.debug "Uploading..." 
265:       # Upload to S3
266:       res = upload_to_s3 sig.acl, sig.bucket, sig.policy, sig.aws_access_key,
267:         sig.signature, sig.key, path
268:       if res
269:         @logger.debug "Upload complete"
270:         # And process in vzaar
271:         process_video :guid => sig.guid, :title => title,
272:           :description => description, :profile => profile,
273:           :transcoding => transcoding
274:       else
275:         @logger.debug "Upload to s3 failed"
276:         return nil
277:       end
278:     end
user_details(login, authenticated = false) click to toggle source

Gets a user public details. Whitelabel users can retrive their details by using the method with ‘authenticated’ option on.

Usage:

  • me = vzaar.user_details ‘some_login’ (this works only if ‘some_login’ is not a protected resource)

  • me = vzaar.user_details ‘your_login’, true (‘your_login’ must be the same as the one provided for Vzaar::Base.new method in order to authorize on server.)

Note: even if you created an authorized instance of Vzaar::Base class (by specifying login and application token in Vzaar::Base.new), you need to set the ‘authenticated’ param to true in order to perform authenticated call.

    # File lib/vzaar/base.rb, line 82
82:     def user_details(login, authenticated = false)
83:       result = nil
84:       if authenticated
85:         auth_connection(HTTP_GET, "/api/users/#{login}.xml") do |xml|
86:           result = User.new xml
87:         end
88:       else
89:         public_connection(HTTP_GET, "/api/users/#{login}.xml") do |xml|
90:           result = User.new xml
91:         end
92:       end
93:       result
94:     end
video_details(video_id, authenticated = false) click to toggle source

Gets video details, inlcuding embed code. Use ‘authenticated’ option to retrieve details of private video.

Usage:

  • video = vzaar.video_details 1234 (1234 must be a public video)

  • video = vzaar.video_details 1234, true (1234 can be a private video but you must the owner)

Note: even if you create an authorized instance of Vzaar::Base class if you don’t set the ‘authenticated’ param to true you will not be able to retrieve data for private video.

     # File lib/vzaar/base.rb, line 136
136:     def video_details(video_id, authenticated = false)
137:       result = nil
138:       if authenticated
139:         auth_connection(HTTP_GET, "/api/videos/#{video_id}.xml") do |xml|
140:           result = VideoDetails.new xml
141:         end
142:       else
143:         public_connection(HTTP_GET, "/api/videos/#{video_id}.xml") do |xml|
144:           result = VideoDetails.new xml
145:         end
146:       end
147:       result
148:     end
video_list(login, authenticated = false) click to toggle source

Gets a list of a user’s active videos along with it’s relevant metadata. Set ‘authenticated’ option to true to retrieve private videos.

Usage:

  • videos = vzaar.video_list ‘your_login’ (gets your public videos)

  • videos = vzaar.video_list ‘some_other_login’ (gets public videos of some_other_login)

  • videos = vzaar.video_list ‘your_login’, true (gets all your videos, provided your_login is the one you provided for Vzaar::Base initializer)

  • videos = vzaar.video_list ‘some_other_login’, true (gets public videos of some_other_login - you cannot access other users’ private videos)

Note: even if you created an authorized instance of Vzaar::Base class if you don’t set the ‘authenticated’ param to true you will receive only public videos.

     # File lib/vzaar/base.rb, line 108
108:     def video_list(login, authenticated = false)
109:       result = []
110:       response = nil
111:       if authenticated
112:         response = auth_connection(HTTP_GET, "/api/#{login}/videos.xml")
113:       else
114:         response = public_connection(HTTP_GET, "/api/#{login}/videos.xml")
115:       end
116:       if response and response.body
117:         doc = REXML::Document.new response.body
118:         videos = doc.elements['videos']
119:         videos.elements.each('video') do |video|
120:           result << Video.new(video.to_s)
121:         end
122:       end
123:       result
124:     end
whoami() click to toggle source

Test method for authentication. Returns a login of an authenticated user.

Usage:

  • my_login = vzaar.whoami

    # File lib/vzaar/base.rb, line 48
48:     def whoami
49:       result = nil
50:       auth_connection(HTTP_GET, '/api/test/whoami') do |xml|
51:         doc = REXML::Document.new xml
52:         result = doc.elements['vzaar-api/test/login'].text
53:       end
54:       result
55:     end

Private Instance Methods

auth_connection(method, url, xml = '', &block) click to toggle source

Performs the authenticated connection

     # File lib/vzaar/base.rb, line 335
335:       def auth_connection(method, url, xml = '', &block)
336:         res = nil
337:         begin 
338:           if @auth_connection
339:             case method
340:               when "GET"
341:                 res = @auth_connection.get url
342:               when "POST"
343:                 if xml and xml.length > 0
344:                   res = @auth_connection.post url, xml,
345:                     { 'Content-Type' => 'application/xml' }
346:                 else
347:                   res = @auth_connection.post url
348:                 end
349:               when "PUT"
350:                 if xml and xml.length > 0
351:                   res = @auth_connection.put url, xml,
352:                     { 'Content-Type' => 'application/xml' }
353:                 else 
354:                   res = @auth_connection.put url
355:                 end
356:               when "DELETE"
357:                 if xml and xml.length > 0
358:                   res = @auth_connection.delete url, xml,
359:                     { 'Content-Type' => 'application/xml' }
360:                 else
361:                   res = @auth_connection.delete url
362:                 end
363:               else
364:                 unknown_method
365:             end
366:             case res.code
367:               when HTTP_OK
368:                 yield res.body if block_given?
369:               when HTTP_CREATED 
370:                 yield res.body if block_given?
371:               when HTTP_BAD_GATEWAY
372:                 handle_exception 'server_not_responding'
373:               else
374:                 handle_exception 'not_authorized'
375:             end
376:           else
377:             handle_exception 'authorization_info_not_provided'
378:           end
379:         rescue Exception => e
380:           raise e if e.is_a? VzaarError
381:           handle_exception 'unknown', e.message
382:         end
383:         res
384:       end
handle_exception(type, message = '') click to toggle source
     # File lib/vzaar/base.rb, line 444
444:       def handle_exception(type, message = '')
445:         case type
446:           when 'not_authorized':
447:             message = "You have not been authorized on the server. " +
448:               "Please check your login and application token."
449:           when 'authorization_info_not_provided':
450:             message = 'You need to provide login and application token to perform ' +
451:               'to perform this action.'
452:           when 'server_not_responding':
453:             message = "The server you're trying to connect to is not responding."
454:           when 'protected_resource':
455:             message = "The resource is protected and you have not been authorized " +
456:               "to access it."
457:           when 'resource_not_found':
458:             message = "The resource has not been found on the server."
459:           when 'unknown_method':
460:             message = "The method used for connecting is not a proper HTTP method."
461:           else
462:             message = "Unknown error occured when accessing the server: " + message
463:         end
464:         @logger.error message
465:         raise VzaarError.new message
466:       end
log_info(message) click to toggle source
     # File lib/vzaar/base.rb, line 440
440:       def log_info(message)
441:         @logger.info message
442:       end
public_connection(method, url, xml = '', &block) click to toggle source

Performs the public connection

     # File lib/vzaar/base.rb, line 283
283:       def public_connection(method, url, xml = '', &block)
284:         res = nil
285:         begin
286:           case method
287:             when "GET"
288:               res = @public_connection.get url
289:             when "POST"
290:               if xml and xml.length > 0
291:                 res = @public_connection.post url, xml,
292:                   { 'Content-Type' => 'application/xml' }
293:               else
294:                 res = @public_connection.post url
295:               end
296:             when "PUT"
297:               if xml and xml.length > 0
298:                 res = @public_connection.put url, xml,
299:                   { 'Content-Type' => 'application/xml' }
300:               else
301:                 res = @public_connection.put url
302:               end
303:             when "DELETE"
304:               if xml and xml.length > 0
305:                 res = @public_connection.delete url, xml,
306:                   { 'Content-Type' => 'application/xml' }
307:               else
308:                 res = @public_connection.delete url
309:               end
310:             else
311:               handle_exception 'unknown_method'
312:           end
313:           case res.code
314:             when HTTP_OK 
315:               yield res.body if block_given?
316:             when HTTP_CREATED
317:               yield res.body if block_given?
318:             when HTTP_FORBIDDEN
319:               handle_exception 'protected_resource'
320:             when HTTP_NOT_FOUND
321:               handle_exception 'resource_not_found'
322:             when HTTP_BAD_GATEWAY
323:               handle_exception 'server_not_responding'
324:             else
325:               handle_exception 'unknown'
326:           end
327:         rescue Exception => e
328:           raise e if e.is_a? VzaarError
329:           handle_exception 'unknown', e.message
330:         end
331:         res
332:       end
upload_to_s3(acl, bucket, policy, aws_access_key, signature, key, file_path) click to toggle source
     # File lib/vzaar/base.rb, line 386
386:       def upload_to_s3(acl, bucket, policy, aws_access_key, signature, key, file_path)
387:         client = HTTPClient.new
388:         client.send_timeout = 1800
389:         url = "https://#{bucket}.s3.amazonaws.com/"
390:         begin
391:           file = File.open file_path
392:           res = client.post url, [
393:             ['acl', acl],
394:             ['bucket', bucket],
395:             ['success_action_status', '201'],
396:             ['policy', policy],
397:             ['AWSAccessKeyId', aws_access_key],
398:             ['signature', signature],
399:             ['key', key],
400:             ['file', file]
401:           ]
402:         rescue Exception => e
403:           file.close if file
404:           handle_exception 'unknown', e.message
405:         end
406:         file.close if file
407:         if res.status_code == 201
408:           return true
409:         else
410:           return false
411:         end
412:       end
upload_to_s3_curl(acl, bucket, policy, aws_access_key, signature, key, file_path) click to toggle source
     # File lib/vzaar/base.rb, line 414
414:       def upload_to_s3_curl(acl, bucket, policy, aws_access_key, signature, key, file_path)
415:         require 'curb'
416:         acl_field = Curl::PostField.content 'acl', acl
417:         bucket_field = Curl::PostField.content 'bucket', bucket
418:         success_action_status_field = Curl::PostField.content 'success_action_status',
419:           '201'
420:         policy_field = Curl::PostField.content 'policy', policy
421:         aws_access_key_field = Curl::PostField.content 'AWSAccessKeyId', aws_access_key
422:         signature_field = Curl::PostField.content 'signature', signature
423:         key_field = Curl::PostField.content 'key', key
424:         file_field = Curl::PostField.file 'file', file_path
425:         curl = Curl::Easy.new "https://#{bucket}.s3.amazonaws.com/"
426:         curl.multipart_form_post = true
427:         begin
428:           curl.http_post acl_field, bucket_field, success_action_status_field, policy_field,
429:             aws_access_key_field, signature_field, key_field, file_field  
430:         rescue Exception => e
431:           handle_exception 'unknown', e.message
432:         end
433:         if curl.response_code == 201
434:           return true
435:         else
436:           return false
437:         end
438:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.