Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ session_id = @opentok.create_session( @location, session_properties )
### Generating Token
With the generated session_id, you can start generating tokens for each user.
`generate_token` takes in hash with 1-4 properties:
> session_id (string) - required
> session_id (string) - REQUIRED
> role (string) - OPTIONAL. subscriber, publisher, or moderator
> expire_time (int) - OPTIONAL. Time when token will expire in unix timestamp
> connection_data (string) - OPTIONAL. Metadata to store data (names, user id, etc)
Expand All @@ -69,17 +69,46 @@ token = @opentok.generate_token :session_id => session, :role => OpenTok::RoleCo
</pre>

### Downloading Archive Videos
To Download archives, first you must first create a token that has a **moderator** role
To Download archived video, you must have an Archive ID which you get from the javascript library

#### Quick Overview of the javascript library: <http://www.tokbox.com/opentok/api/tools/js/documentation/api/Session.html#createArchive>
1. Create an event listener on `archiveCreated` event: `session.addEventListener('archiveCreated', archiveCreatedHandler);`
2. Create an archive: `archive = session.createArchive(...);`
3. When archive is successfully created `archiveCreatedHandler` would be triggered. An Archive object containing `archiveId` property is passed into your function. Save this in your database, this archiveId is what you use to reference the archive for playbacks and download videos
4. After your archive has been created, you can start recording videos into it by calling `session.startRecording(archive)`
Optionally, you can also use the standalone archiving, which means that each archive would have only 1 video: <http://www.tokbox.com/opentok/api/tools/js/documentation/api/RecorderManager.html>

### Get Archive Manifest
With your **moderator token** and OpentokSDK Object, you can generate OpenTokArchive Object, which contains information for all videos in the Archive
`get_archive_manifest()` takes in 2 parameters: **archiveId** and **moderator token**
> **returns** an `OpenTokArchive`. The *resources* property of this object is array of `OpenTokArchiveVideoResource`, and each `OpenTokArchiveVideoResource` object represents a video in the archive.
> archive_id (string) - REQUIRED.
> **returns** an `OpenTokArchive` object. The *resources* property of this object is array of `OpenTokArchiveVideoResource` objects, and each `OpenTokArchiveVideoResource` object represents a video in the archive.

Example:(Make sure you have the OpentokSDK Object)
<pre>
@token = 'moderator_token'
@archiveId = '5f74aee5-ab3f-421b-b124-ed2a698ee939' #Obtained from Javascript Library
otArchive = @opentok.get_archive_manifest(@archiveId, @token)
</pre>

### Get video ID
With your `OpenTokArchive` object, call `getId()`
`OpenTokArchive.resources` is an array of `OpenTokArchiveVideoResource` objects. OpenTokArchiveVideoResource has `getId()` method that returns the videoId
`getId()` will return the video ID (a String)

Example:
<pre>
otArchive = @opentok.get_archive_manifest(@archiveId, @token)
otVideoResource = otArchive.resources[0]
videoId = otVideoResource.getId()
</pre>

### Get Download Url
`downloadArchiveURL` takes 1 parameters: `video ID` and returns download URL for the video

`OpenTokArchive` has `downloadArchiveURL` that will return an url string for downloading the video in the archive. You must call this function every time you want the file, because this url expires after 24 hours
> video_id (string) - REQUIRED
> token (string) - REQUIRED
> returns url string

Example:
<pre>
url = otArchive.downloadArchiveURL(video_id, token)
</pre>
47 changes: 42 additions & 5 deletions lib/open_tok/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,55 @@ module OpenTok
class Archive
attr_accessor :archive_id, :archive_title, :resources, :timeline

def initialize(archive_id, archive_title, resources, timeline)
def initialize(archive_id, archive_title, resources, timeline, apiUrl, token)
@archive_id = archive_id
@archive_title = archive_title
@resources = resources
@timeline = timeline
@apiUrl = apiUrl
@token = token
end

def do_request(api_url, token)
url = URI.parse(api_url)
req = Net::HTTP::Get.new(url.path)

req.add_field 'X-TB-TOKEN-AUTH', token

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if @apiUrl.start_with?("https")
res = http.start {|http| http.request(req)}
case res
when Net::HTTPSuccess, Net::HTTPRedirection
return res.read_body
else
res.error!
end
rescue Net::HTTPExceptions
raise
raise OpenTokException.new 'Unable to create fufill request: ' + $!
rescue NoMethodError
raise
raise OpenTokException.new 'Unable to create a fufill request at this time: ' + $1
end

def download_archive_url(video_id)
"#{API_URL}/archive/url/#{@archive_id}/#{video_id}"
doc = do_request "#{@apiUrl}/archive/url/#{@archive_id}/#{video_id}"
if not doc.get_elements('Errors').empty?
raise OpenTokException.new doc.get_elements('Errors')[0].get_elements('error')[0].children.to_s
end
doc
end

def downloadArchiveURL(video_id, token="")
if token==""
return "#{@apiUrl}/archive/url/#{@archive_id}/#{video_id}"
else
return do_request "#{@apiUrl}/archive/url/#{@archive_id}/#{video_id}", token
end
end

def self.parse_manifest(manifest)
def self.parse_manifest(manifest, apiUrl, token)
archive_id = manifest.attributes['archiveid']
archive_title = manifest.attributes['title']

Expand All @@ -34,7 +71,7 @@ def self.parse_manifest(manifest)
timeline << OpenTok::ArchiveTimelineEvent.parseXML(event)
end

OpenTok::Archive.new(archive_id, archive_title, resources, timeline)
OpenTok::Archive.new(archive_id, archive_title, resources, timeline, apiUrl, token)
end
end
end
end
6 changes: 5 additions & 1 deletion lib/open_tok/archive_video_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ def initialize(id, length)
@length = length
end

def getId
return @id
end

def self.parseXML(video_resource_item)
OpenTok::ArchiveVideoResource.new(video_resource_item.attributes['id'], video_resource_item.attributes['length'])
end
end

end
end
4 changes: 3 additions & 1 deletion lib/open_tok/open_tok_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ def create_session(location='', opts={})
# This method takes two parameters. The first parameter is the +archive_id+ of the archive that contains the video (a String). The second parameter is the +token+ (a String)
# The method returns an +OpenTok::Archive+ object. The resources property of this object is an array of OpenTok::ArchiveVideoResource objects. Each OpenTok::ArchiveVideoResource object represents a video in the archive.
def get_archive_manifest(archive_id, token)
# TODO: verify that token is MODERATOR token, Staging and production

doc = do_request("/archive/getmanifest/#{archive_id}", {}, token)
if not doc.get_elements('Errors').empty?
raise OpenTokException.new doc.get_elements('Errors')[0].get_elements('error')[0].children.to_s
end
OpenTok::Archive.parse_manifest(doc.get_elements('manifest')[0])
OpenTok::Archive.parse_manifest(doc.get_elements('manifest')[0], @api_url, token)
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/open_tok/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Opentok
VERSION = "0.0.5"
end
VERSION = "0.0.7"
end
2 changes: 1 addition & 1 deletion opentok.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.authors = ["Stijn Mathysen", "Karmen Blake"]
s.email = ["stijn@skylight.be", "karmenblake@gmail.com"]
s.homepage = "https://github.com/stijnster/opentok"
s.homepage = "https://github.com/opentok/Opentok-Ruby-SDK"
s.summary = %q{OpenTok gem}
s.description = %q{OpenTok is a free set of APIs from TokBox that enables websites to weave live group video communication into their online experience. With OpenTok you have the freedom and flexibility to create the most engaging web experience for your users. OpenTok is currently available as a JavaScript and ActionScript 3.0 library. This gem allows you to connect to the API from within Ruby (and Rails)}

Expand Down
111 changes: 99 additions & 12 deletions spec/opentok_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require 'spec_helper'

class TestOpentokSDK < OpenTok::OpenTokSDK
def do_request(api_url, params, token=nil)
super
end
end

describe OpenTok do

before :all do
Expand All @@ -11,23 +17,97 @@

@opentok = OpenTok::OpenTokSDK.new @api_key, @api_secret
end

it "should be possible to valid a OpenTokSDK object with a valid key and secret" do
@opentok.should be_instance_of OpenTok::OpenTokSDK
end

it "a new OpenTokSDK object should point to the staging environment by default" do
@opentok.api_url.should eq @api_staging_url

describe "Staging Environment" do
before :all do
@api_key = '14971292'
@api_secret = 'ecbe2b25afec7887bd72fe4763b87add8ce02658'
@opentok = TestOpentokSDK.new @api_key, @api_secret
@opts = {:partner_id => @api_key, :location=>@host}
end

it "should be possible to valid a OpenTokSDK object with a valid key and secret" do
@opentok.should be_instance_of TestOpentokSDK
end

it "a new OpenTokSDK object should point to the staging environment by default" do
@opentok.api_url.should eq @api_staging_url
end

it "should generate a valid session" do
session = @opentok.create_session @host
session.to_s.should match(/\A[0-9A-z_-]{40,}\Z/)
end

it "do_request should respond with valid p2p" do
@opts.merge!({'p2p.preference' => 'enabled'})
doc = @opentok.do_request("/session/create", @opts)
doc.root.get_elements('Session')[0].get_elements('properties')[0].get_elements('p2p')[0].get_elements('preference')[0].children[0].to_s.should =='enabled'
end
end

describe "Session creation" do
it "should be possible to generate a valid API token with a valid key and secret" do
opentok = OpenTok::OpenTokSDK.new @api_key, @api_secret
session = opentok.create_session @host

describe "Production Environment" do
before :all do
@api_key = '11421872'
@api_secret = '296cebc2fc4104cd348016667ffa2a3909ec636f'
@opentok = TestOpentokSDK.new @api_key, @api_secret, {:api_url=>@api_production_url}
@opts = {:partner_id => @api_key, :location=>@host}
end

it "should be possible to valid a OpenTokSDK object with a valid key and secret" do
@opentok.should be_instance_of TestOpentokSDK
end

it "a new OpenTokSDK object should point to the staging environment by default" do
@opentok.api_url.should eq @api_production_url
end

it "should generate a valid session" do
session = @opentok.create_session @host
session.to_s.should match(/\A[0-9A-z_-]{40,}\Z/)
end

it "do_request should respond with valid p2p" do
@opts.merge!({'p2p.preference' => 'enabled'})
doc = @opentok.do_request("/session/create", @opts)
doc.root.get_elements('Session')[0].get_elements('properties')[0].get_elements('p2p')[0].get_elements('preference')[0].children[0].to_s.should =='enabled'
end

describe "Archiving downloads" do
before :all do
@session = '1_MX4xNDk3MTI5Mn5-MjAxMi0wNS0yMCAwMTowMzozMS41MDEzMDArMDA6MDB-MC40NjI0MjI4MjU1MDF-'
@opentok = OpenTok::OpenTokSDK.new @api_key, @api_secret, {:api_url=>@api_production_url}
@token = @opentok.generate_token({:session_id => @session, :role=>OpenTok::RoleConstants::MODERATOR})
@archiveId = '5f74aee5-ab3f-421b-b124-ed2a698ee939'
end

it "should have archive resources" do
otArchive = @opentok.get_archive_manifest(@archiveId, @token)
otArchiveResource = otArchive.resources[0]
vid = otArchiveResource.getId()
vid.should match(/[0-9A-z=]+/)
end

it "should return download url" do
otArchive = @opentok.get_archive_manifest(@archiveId, @token)
otArchiveResource = otArchive.resources[0]
vid = otArchiveResource.getId()
url = otArchive.downloadArchiveURL(vid)
url.start_with?('http').should eq true
end

it "should return file url" do
otArchive = @opentok.get_archive_manifest(@archiveId, @token)
otArchiveResource = otArchive.resources[0]
vid = otArchiveResource.getId()
url = otArchive.downloadArchiveURL(vid, @token)
url.start_with?('http').should eq true
end
end
end


describe "Session creation" do
it "should raise an exception with an invalid key and secret" do
opentok = OpenTok::OpenTokSDK.new 0, ''

Expand Down Expand Up @@ -76,5 +156,12 @@
@opentok = OpenTok::OpenTokSDK.new @api_key, @api_secret
@valid_session = @opentok.create_session(@host).to_s
end

# it "If token does not have moderator role, raise error" do
# token = @opentok.generate_token(:session_id=>@valid_session)
# expect{
# @opentok.get_archive_manifest("", token)
# }.to raise_error OpenTok::OpenTokException
# end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require 'I18n'
require File.dirname(__FILE__) + '/../lib/opentok.rb'
require File.dirname(__FILE__) + '/../lib/opentok.rb'