Parent

OpenTok::OpenTokSDK

Attributes

api_url[RW]

Public Class Methods

new(partner_id, partner_secret, options = nil) click to toggle source

Create a new OpenTokSDK object.

The first two attributes are required; parnter_id and partner_secret are the api-key and secret that are provided to you.

You can also pass in optional options;

  • :api_url sets the location of the api (staging or production)

    # File lib/open_tok/open_tok_sdk.rb, line 46
46:     def initialize(partner_id, partner_secret, options = nil)
47:       @partner_id = partner_id
48:       @partner_secret = partner_secret.strip
49:       
50:       if options.is_a?(::Hash)
51:         @api_url = options[:api_url] || API_URL
52:       end
53:       
54:       unless @api_url
55:         @api_url = API_URL
56:       end
57:     end

Public Instance Methods

create_session(location='', opts={}) click to toggle source

Generates a new OpenTok::Session and set it’s session_id, situating it in TokBox’s global network near the IP of the specified @location@.

See www.tokbox.com/opentok/tools/documentation/overview/session_creation.html for more information

     # File lib/open_tok/open_tok_sdk.rb, line 99
 99:     def create_session(location='', opts={})
100:       opts.merge!({:partner_id => @partner_id, :location=>location})
101:       doc = do_request("/session/create", opts)
102:       if not doc.get_elements('Errors').empty?
103:         raise OpenTokException.new doc.get_elements('Errors')[0].get_elements('error')[0].children.to_s
104:       end
105:       OpenTok::Session.new(doc.root.get_elements('Session')[0].get_elements('session_id')[0].children[0].to_s)
106:     end
generate_token(opts = {}) click to toggle source

Generate token for the given session_id. The options you can provide are;

  • :session_id (required) generate a token for the provided session

  • :create_time

  • :expire_time (optional) The time when the token will expire, defined as an integer value for a Unix timestamp (in seconds). If you do not specify this value, tokens expire in 24 hours after being created.

  • :role (optional) Added in OpenTok v0.91.5. This defines the role the user will have. There are three roles: subscriber, publisher, and moderator.

See www.tokbox.com/opentok/tools/documentation/overview/token_creation.html for more information on all options.

    # File lib/open_tok/open_tok_sdk.rb, line 66
66:     def generate_token(opts = {})
67:       {:session_id=>nil, :create_time=>nil, :expire_time=>nil, :role=>nil}.merge!(opts)
68: 
69:       create_time = opts[:create_time].nil? ? Time.now  :  opts[:create_time]
70:       session_id = opts[:session_id].nil? ? '' : opts[:session_id]
71:       role = opts[:role].nil? ? RoleConstants::PUBLISHER : opts[:role]
72: 
73:       data_params = {
74:         :role => role,
75:         :session_id => session_id,
76:         :create_time => create_time.to_i,
77:         :nonce => rand
78:       }
79: 
80:       if not opts[:expire_time].nil?
81:         data_params[:expire_time] = opts[:expire_time].to_i
82:       end
83: 
84:       data_string = data_params.urlencode
85: 
86:       sig = sign_string(data_string, @partner_secret)
87:       meta_string = {
88:         :partner_id => @partner_id,
89:         :sdk_version => @@SDK_VERSION,
90:         :sig => sig
91:       }.urlencode
92: 
93:       @@TOKEN_SENTINEL + Base64.encode64(meta_string + ":" + data_string).gsub("\n","")
94:     end

Protected Instance Methods

do_request(api_url, params, token=nil) click to toggle source
     # File lib/open_tok/open_tok_sdk.rb, line 113
113:     def do_request(api_url, params, token=nil)
114:       url = URI.parse(@api_url + api_url)
115:       if not params.empty?
116:         req = Net::HTTP::Post.new(url.path)
117:         req.set_form_data(params)
118:       else
119:         req = Net::HTTP::Get.new(url.path)
120:       end
121: 
122:       if not token.nil?
123:         req.add_field 'X-TB-TOKEN-AUTH', token
124:       else
125:         req.add_field 'X-TB-PARTNER-AUTH', "#{@partner_id}:#{@partner_secret}"
126:       end
127:       http = Net::HTTP.new(url.host, url.port)
128:       http.use_ssl = true if @api_url.start_with?("https")
129:       res = http.start {|http| http.request(req)}
130:       case res
131:       when Net::HTTPSuccess, Net::HTTPRedirection
132:         # OK
133:         doc = REXML::Document.new(res.read_body)
134:         return doc
135:       else
136:         res.error!
137:       end
138:     rescue Net::HTTPExceptions
139:       raise
140:       raise OpenTokException.new 'Unable to create fufill request: ' + $!
141:     rescue NoMethodError
142:       raise
143:       raise OpenTokException.new 'Unable to create a fufill request at this time: ' + $1
144:     end
sign_string(data, secret) click to toggle source
     # File lib/open_tok/open_tok_sdk.rb, line 109
109:     def sign_string(data, secret)
110:       OpenSSL::HMAC.hexdigest(DIGEST, secret, data)
111:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.