Class: NSIVideoConvert::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nsivideoconvert/client.rb,
lib/nsivideoconvert/configuration.rb

Defined Under Namespace

Classes: Configuration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Note:

if you had used the ‘configure’ method, you can use it without parameters and those you provided before will be used (see Client#configure)

Initialize a client to a VideoConvert node

Examples:

videoconvert = NSIVideoConvert::Client.new host: 'localhost', port: '8886', user: 'test', password: 'test'


23
24
25
26
27
28
29
# File 'lib/nsivideoconvert/client.rb', line 23

def initialize(params = {})
  params = Configuration.settings.merge(params)
  @user = params[:user]
  @password = params[:password]
  @host = params[:host]
  @port = params[:port]
end

Class Method Details

.configure { ... } ⇒ Object

Pre-configure the NSIVideoConvert module with default params for the NSIVideoConvert::Client

Examples:

NSIVideoConvert::Client.configure do
  user     "why"
  password "chunky"
  host     "localhost"
  port     "8888"
end

Yields:



125
126
127
# File 'lib/nsivideoconvert/client.rb', line 125

def self.configure(&block)
  Configuration.instance_eval(&block)
end

Instance Method Details

#convert(options = {}) ⇒ Hash

Note:

the filename is very importante, the videoconvert node will use the proper coding/encoding option for the video type

Note:

if provided both video_link and file options, file will be ignored and the client will download the video instead

Send a video be converted by a nsi.videoconvert node

Examples:

A simple convertion

require 'base64'
video = Base64.encode64(File.new('video.ogv', 'r').read)
response = nsivideoconvert.convert(:file => video, :filename => 'video.ogv')
nsivideoconvert.done(response["video_key"])
nsivideoconvert.grains_keys_for(response["video_key"])

Converting from a SAM uid

video = Base64.encode64(File.new('video.ogv', 'r').read)
response = sam.store({:doc => doc})
video_key = response["key"]
response = nsivideoconvert.convert(:sam_uid => video_key, :filename => 'video.ogv')
nsivideoconvert.done(response["video_key"])
nsivideoconvert.grains_keys_for(response["video_key"])

Downloading and converting from web

response = nsivideoconvert.convert(:video_link => 'http://google.com/video.ogv')
nsivideoconvert.done(response["video_key"])
nsivideoconvert.grains_keys_for(response["video_key"])

Sending a callback url

video = Base64.encode64(File.new('video.ogv', 'r').read)
nsivideoconvert.convert(:file => video, :filename => 'video.ogv', :callback => 'http://google.com')
nsivideoconvert.convert(:video_link => 'http://google.com/video.ogv', :callback => 'http://google.com')

Using a custom verb to the callback

video = Base64.encode64(File.new('video.ogv', 'r').read)
nsivideoconvert.convert(:file => video, :filename => 'video.ogv', :callback => 'http://google.com', :verb => "PUT")
nsivideoconvert.convert(:video_link => 'http://google.com/video.ogv', :callback => 'http://google.com', :verb => "PUT")

Options Hash (options):

  • file (String)

    the base64 encoded file to be converted

  • sam_uid (String)

    the UID of a video at SAM

  • filename (String)

    the filename of the video

  • video_link (String)

    link to the video that’ll be converted

  • callback (String)

    a callback url to the file convertion

  • verb (String)

    the callback request verb, when not provided, nsi.videoconvert defaults to POST

Raises:

  • NSIVideoConvert::Errors::Client::MissingParametersError when an invalid or incomplete set of parameters is provided

  • NSIVideoConvert::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSIVideoConvert::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSIVideoConvert::Errors::Client::KeyNotFoundError when an invalid sam_uid is provided



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nsivideoconvert/client.rb', line 77

def convert(options = {})
  @request_data = Hash.new
  if options[:video_link]
    insert_download_data options
  elsif options[:sam_uid] && options[:filename]
    file_data = {:sam_uid => options[:sam_uid], :filename => options[:filename]}
    @request_data.merge! file_data
  elsif options[:file] && options[:filename]
    file_data = {:video => options[:file], :filename => options[:filename]}
    @request_data.merge! file_data
  else
    raise NSIVideoConvert::Errors::Client::MissingParametersError
  end
  insert_callback_data options
  request = prepare_request :POST, @request_data.to_json
  execute_request(request)
end

#done(key) ⇒ Hash

Verify if a video is already converted

Examples:

nsivideoconvert.done("some key")

Raises:

  • NSIVideoConvert::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSIVideoConvert::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSIVideoConvert::Errors::Client::KeyNotFoundError when an invalid key is provided



109
110
111
112
# File 'lib/nsivideoconvert/client.rb', line 109

def done(key)
  request = prepare_request :GET, {:key => key}.to_json
  execute_request(request)
end