Class: OpenTok::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/opentok/session.rb

Overview

Represents an OpenTok session.

Use the OpenTok.createSession() method to create an OpenTok session. Use the session_id property of the Session object to get the session ID.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#locationString (readonly)

The location hint IP address. See the OpenTok.createSession() method.

Returns:

  • (String)

    the current value of location



43
44
45
# File 'lib/opentok/session.rb', line 43

def location
  @location
end

#media_modeString (readonly)

Set to :routed if the session uses the OpenTok Media Router or to :relayed if the session attempts to transmit streams directly between clients.

Returns:

  • (String)

    the current value of media_mode



43
44
45
# File 'lib/opentok/session.rb', line 43

def media_mode
  @media_mode
end

#session_idString (readonly)

The session ID.

Returns:

  • (String)

    the current value of session_id



43
44
45
# File 'lib/opentok/session.rb', line 43

def session_id
  @session_id
end

Instance Method Details

#generate_token(options) ⇒ String

Generates a token.

Parameters:

  • options (Hash)

Options Hash (options):

  • :role (String)

    The role for the token. Set this to one of the following values:

    • :subscriber – A subscriber can only subscribe to streams.

    • :publisher – A publisher can publish streams, subscribe to streams, and signal. (This is the default value if you do not specify a role.)

    • :moderator – In addition to the privileges granted to a publisher, in clients using the OpenTok.js 2.2 library, a moderator can call the forceUnpublish() and forceDisconnect() method of the Session object.

  • :expire_time (integer)

    The expiration time, in seconds since the UNIX epoch. Pass in 0 to use the default expiration time of 24 hours after the token creation time. The maximum expiration time is 30 days after the creation time.

  • :data (String)

    A string containing connection metadata describing the end-user. For example, you can pass the user ID, name, or other data describing the end-user. The length of the string is limited to 1000 characters. This data cannot be updated once it is set.

Returns:

  • (String)

    The token string.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/opentok/session.rb', line 43

class Session

  include TokenGenerator
  generates_tokens({
    :api_key => ->(instance) { instance.api_key },
    :api_secret => ->(instance) { instance.api_secret },
    :session_id => ->(instance) { instance.session_id }
  })

  attr_reader :session_id, :media_mode, :location, :api_key, :api_secret

  # @private
  # this implementation doesn't completely understand the format of a Session ID
  # that is intentional, that is too much responsibility.
  def self.belongs_to_api_key?(session_id, api_key)
    encoded = session_id[2..session_id.length]
                        .gsub('-', '+')
                        .gsub('_', '/')
    decoded = Base64.decode64(encoded)
    decoded.include? api_key
  end

  # @private
  def initialize(api_key, api_secret, session_id, opts={})
    @api_key, @api_secret, @session_id = api_key, api_secret, session_id
    @media_mode, @location = opts.fetch(:media_mode, :routed), opts[:location]
  end

  # @private
  def to_s
    @session_id
  end
end