lib/opentok/client.rb in opentok-4.7.1 vs lib/opentok/client.rb in opentok-4.8.0

- old
+ new

@@ -60,10 +60,12 @@ end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end + # Archives methods + def start_archive(session_id, opts) opts.extend(HashExtensions) body = { "sessionId" => session_id }.merge(opts.camelize_keys!) response = self.class.post("/v2/project/#{@api_key}/archive", { :body => body.to_json, @@ -213,465 +215,528 @@ end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def forceDisconnect(session_id, connection_id) - response = self.class.delete("/v2/project/#{@api_key}/session/#{session_id}/connection/#{connection_id}", { + # Broadcasts methods + + def start_broadcast(session_id, opts) + opts.extend(HashExtensions) + body = { :sessionId => session_id }.merge(opts.camelize_keys!) + response = self.class.post("/v2/project/#{@api_key}/broadcast", { + :body => body.to_json, :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 204 + when 200 response when 400 - raise ArgumentError, "Force disconnect failed. Connection ID #{connection_id} or Session ID #{session_id} is invalid" + raise OpenTokBroadcastError, "The broadcast could not be started. The request was invalid or invalid layout options or exceeded the limit of five simultaneous RTMP streams." when 403 - raise OpenTokAuthenticationError, "You are not authorized to forceDisconnect, check your authentication credentials or token type is non-moderator" + raise OpenTokAuthenticationError, "Authentication failed while starting a broadcast. API Key: #{@api_key}" + when 409 + raise OpenTokBroadcastError, "The broadcast has already been started for this session." + when 500 + raise OpenTokError, "OpenTok server error." + else + raise OpenTokBroadcastError, "The broadcast could not be started" + end + rescue StandardError => e + raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" + end + + def get_broadcast(broadcast_id) + response = self.class.get("/v2/project/#{@api_key}/broadcast/#{broadcast_id}", { + :headers => generate_headers + }) + case response.code + when 200 + response + when 400 + raise OpenTokBroadcastError, "The request was invalid." + when 403 + raise OpenTokAuthenticationError, "Authentication failed while getting a broadcast. API Key: #{@api_key}" when 404 - raise OpenTokConnectionError, "The client specified by the connection ID: #{connection_id} is not connected to the session" + raise OpenTokBroadcastError, "No matching broadcast found (with the specified ID)" + when 500 + raise OpenTokError, "OpenTok server error." + else + raise OpenTokBroadcastError, "Could not fetch broadcast information." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def force_mute_stream(session_id, stream_id) - response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/stream/#{stream_id}/mute", { - :headers => generate_headers("Content-Type" => "application/json") + def stop_broadcast(broadcast_id) + response = self.class.post("/v2/project/#{@api_key}/broadcast/#{broadcast_id}/stop", { + :headers => generate_headers }) case response.code when 200 response when 400 - raise ArgumentError, "Force mute failed. Stream ID #{stream_id} or Session ID #{session_id} is invalid" + raise OpenTokBroadcastError, "The request was invalid." when 403 - raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed while stopping a broadcast. API Key: #{@api_key}" when 404 - raise OpenTokConnectionError, "Either Stream ID #{stream_id} or Session ID #{session_id} is invalid" + raise OpenTokBroadcastError, "No matching broadcast found (with the specified ID) or it is already stopped" + when 500 + raise OpenTokError, "OpenTok server error." + else + raise OpenTokBroadcastError, "The broadcast could not be stopped." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def force_mute_session(session_id, opts) + def list_broadcasts(offset, count, session_id) + query = Hash.new + query[:offset] = offset unless offset.nil? + query[:count] = count unless count.nil? + query[:sessionId] = session_id unless session_id.nil? + response = self.class.get("/v2/project/#{@api_key}/broadcast", { + :query => query.empty? ? nil : query, + :headers => generate_headers, + }) + case response.code + when 200 + response + when 403 + raise OpenTokAuthenticationError, + "Authentication failed while retrieving broadcasts. API Key: #{@api_key}" + else + raise OpenTokBroadcastError, "The broadcasts could not be retrieved." + end + rescue StandardError => e + raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" + end + + def layout_broadcast(broadcast_id, opts) opts.extend(HashExtensions) - body = opts.camelize_keys! - response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/mute", { - :body => body.to_json, + response = self.class.put("/v2/project/#{@api_key}/broadcast/#{broadcast_id}/layout", { + :body => opts.camelize_keys!.to_json, :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response when 400 - raise ArgumentError, "Force mute failed. The request could not be processed due to a bad request" + raise OpenTokBroadcastError, "The layout operation could not be performed. The request was invalid or invalid layout options." when 403 - raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" - when 404 - raise OpenTokConnectionError, "Session ID #{session_id} is invalid" + raise OpenTokAuthenticationError, "Authentication failed for broadcast layout. API Key: #{@api_key}" + when 500 + raise OpenTokError, "OpenTok server error." + else + raise OpenTokBroadcastError, "The broadcast layout could not be performed." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def signal(session_id, connection_id, opts) + def select_streams_for_broadcast(broadcast_id, opts) opts.extend(HashExtensions) - connectionPath = connection_id.to_s.empty? ? "" : "/connection/#{connection_id}" - url = "/v2/project/#{@api_key}/session/#{session_id}#{connectionPath}/signal" - response = self.class.post(url, { - :body => opts.camelize_keys!.to_json, + body = opts.camelize_keys! + response = self.class.patch("/v2/project/#{@api_key}/broadcast/#{broadcast_id}/streams", { + :body => body.to_json, :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 204 response when 400 - raise ArgumentError, "One of the signal properties — data, type, sessionId or connectionId — is invalid." + raise OpenTokBroadcastError, "The request was invalid." when 403 - raise OpenTokAuthenticationError, "You are not authorized to send the signal. Check your authentication credentials." + raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" when 404 - raise OpenTokError, "The client specified by the connectionId property is not connected to the session." - when 413 - raise OpenTokError, "The type string exceeds the maximum length (128 bytes), or the data string exceeds the maximum size (8 kB)." + raise OpenTokBroadcastError, "No matching broadcast found with the specified ID: #{broadcast_id}" + when 405 + raise OpenTokBroadcastError, "The broadcast was started with streamMode set to 'auto', which does not support stream manipulation." + when 500 + raise OpenTokError, "OpenTok server error." else - raise OpenTokError, "The signal could not be send." + raise OpenTokBroadcastError, "The broadcast streams could not be updated." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def connect_websocket(session_id, token, websocket_uri, opts) - opts.extend(HashExtensions) + # Captions methods + + def start_live_captions(session_id, token, options) + options.extend(HashExtensions) body = { "sessionId" => session_id, "token" => token, - "websocket" => { "uri" => websocket_uri }.merge(opts.camelize_keys!) - } + }.merge(options.camelize_keys!) - response = self.class.post("/v2/project/#{@api_key}/connect", { + response = self.class.post("/v2/project/#{@api_key}/captions", { :body => body.to_json, :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 200 + when 202 response when 400 - raise ArgumentError, "One of the properties is invalid." + raise OpenTokCaptionsError, "The request was invalid." when 403 - raise OpenTokAuthenticationError, "You are not authorized to start the call, check your authentication information." + raise OpenTokAuthenticationError, "Authentication failed while starting captions. API Key: #{@api_key}" when 409 - raise OpenTokWebSocketError, "Conflict. Only routed sessions are allowed to initiate Connect Calls." + raise OpenTokCaptionsError, "Live captions have already started for this OpenTok Session: #{session_id}" when 500 raise OpenTokError, "OpenTok server error." else - raise OpenTokWebSocketError, "The WebSocket could not be connected" + raise OpenTokCaptionsError, "Captions could not be started" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def dial(session_id, token, sip_uri, opts) - opts.extend(HashExtensions) - body = { "sessionId" => session_id, - "token" => token, - "sip" => { "uri" => sip_uri }.merge(opts.camelize_keys!) - } - - response = self.class.post("/v2/project/#{@api_key}/dial", { - :body => body.to_json, + def stop_live_captions(captions_id) + response = self.class.post("/v2/project/#{@api_key}/captions/#{captions_id}/stop", { :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 200 + when 202 response when 403 - raise OpenTokAuthenticationError, "Authentication failed while dialing a SIP session. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed while starting captions. API Key: #{@api_key}" when 404 - raise OpenTokSipError, "The SIP session could not be dialed. The Session ID does not exist: #{session_id}" + raise OpenTokCaptionsError, "No matching captions_id was found: #{captions_id}" + when 500 + raise OpenTokError, "OpenTok server error." else - raise OpenTokSipError, "The SIP session could not be dialed" + raise OpenTokCaptionsError, "Captions could not be stopped" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def play_dtmf_to_connection(session_id, connection_id, dtmf_digits) - body = { "digits" => dtmf_digits } + # Connections methods - response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/connection/#{connection_id}/play-dtmf", { - :body => body.to_json, - :headers => generate_headers("Content-Type" => "application/json") + def forceDisconnect(session_id, connection_id) + response = self.class.delete("/v2/project/#{@api_key}/session/#{session_id}/connection/#{connection_id}", { + :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 200 + when 204 response when 400 - raise ArgumentError, "One of the properties — dtmf_digits #{dtmf_digits} or session_id #{session_id} — is invalid." + raise ArgumentError, "Force disconnect failed. Connection ID #{connection_id} or Session ID #{session_id} is invalid" when 403 - raise OpenTokAuthenticationError, "Authentication failed. This can occur if you use an invalid OpenTok API key or an invalid JSON web token. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "You are not authorized to forceDisconnect, check your authentication credentials or token type is non-moderator" when 404 - raise OpenTokError, "The specified session #{session_id} does not exist or the client specified by the #{connection_id} property is not connected to the session." - else - raise OpenTokError, "An error occurred when attempting to play DTMF digits to the session" + raise OpenTokConnectionError, "The client specified by the connection ID: #{connection_id} is not connected to the session" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def play_dtmf_to_session(session_id, dtmf_digits) - body = { "digits" => dtmf_digits } + # Renders methods - response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/play-dtmf", { - :body => body.to_json, - :headers => generate_headers("Content-Type" => "application/json") + def start_render(session_id, opts) + opts.extend(HashExtensions) + body = { :sessionId => session_id }.merge(opts.camelize_keys!) + response = self.class.post("/v2/project/#{@api_key}/render", { + :body => body.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 200 + when 202 response when 400 - raise ArgumentError, "One of the properties — dtmf_digits #{dtmf_digits} or session_id #{session_id} — is invalid." + raise OpenTokRenderError, "The render could not be started. The request was invalid." when 403 - raise OpenTokAuthenticationError, "Authentication failed. This can occur if you use an invalid OpenTok API key or an invalid JSON web token. API Key: #{@api_key}" - when 404 - raise OpenTokError, "The specified session does not exist. Session ID: #{session_id}" + raise OpenTokAuthenticationError, "Authentication failed while starting a render. API Key: #{@api_key}" + when 500 + raise OpenTokError, "OpenTok server error." else - raise OpenTokError, "An error occurred when attempting to play DTMF digits to the session" + raise OpenTokRenderError, "The render could not be started" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def info_stream(session_id, stream_id) - streamId = stream_id.to_s.empty? ? '' : "/#{stream_id}" - url = "/v2/project/#{@api_key}/session/#{session_id}/stream#{streamId}" - response = self.class.get(url, - headers: generate_headers('Content-Type' => 'application/json')) + def get_render(render_id) + response = self.class.get("/v2/project/#{@api_key}/render/#{render_id}", { + :headers => generate_headers + }) case response.code when 200 response when 400 - raise ArgumentError, 'Invalid request. You did not pass in a valid session ID or stream ID.' + raise OpenTokRenderError, "The request was invalid." when 403 - raise OpenTokAuthenticationError, 'Check your authentication credentials. You passed in an invalid OpenTok API key.' - when 408 - raise ArgumentError, 'You passed in an invalid stream ID.' + raise OpenTokAuthenticationError, "Authentication failed while getting a render. API Key: #{@api_key}" + when 404 + raise OpenTokRenderError, "No matching render found (with the specified ID)" when 500 - raise OpenTokError, 'OpenTok server error.' + raise OpenTokError, "OpenTok server error." else - raise OpenTokError, 'Could not fetch the stream information.' + raise OpenTokRenderError, "Could not fetch render information." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def layout_streams(session_id, opts) - opts.extend(HashExtensions) - response = self.class.put("/v2/project/#{@api_key}/session/#{session_id}/stream", { - :body => opts.camelize_keys!.to_json, - :headers => generate_headers("Content-Type" => "application/json") + def stop_render(render_id) + response = self.class.delete("/v2/project/#{@api_key}/render/#{render_id}", { + :headers => generate_headers }) case response.code - when 200 + when 204 response when 400 - raise OpenTokStreamLayoutError, "Setting the layout failed. The request was invalid or invalid layout options were given." + raise OpenTokRenderError, "The request was invalid." when 403 - raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed while stopping a render. API Key: #{@api_key}" + when 404 + raise OpenTokRenderError, "No matching render found (with the specified ID) or it is already stopped" when 500 - raise OpenTokError, "Setting the layout failed. OpenTok server error." + raise OpenTokError, "OpenTok server error." else - raise OpenTokStreamLayoutError, "Setting the layout failed." + raise OpenTokRenderError, "The render could not be stopped." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def start_broadcast(session_id, opts) - opts.extend(HashExtensions) - body = { :sessionId => session_id }.merge(opts.camelize_keys!) - response = self.class.post("/v2/project/#{@api_key}/broadcast", { - :body => body.to_json, - :headers => generate_headers("Content-Type" => "application/json") + def list_renders(offset, count) + query = Hash.new + query[:offset] = offset unless offset.nil? + query[:count] = count unless count.nil? + response = self.class.get("/v2/project/#{@api_key}/render", { + :query => query.empty? ? nil : query, + :headers => generate_headers, }) case response.code when 200 response - when 400 - raise OpenTokBroadcastError, "The broadcast could not be started. The request was invalid or invalid layout options or exceeded the limit of five simultaneous RTMP streams." when 403 - raise OpenTokAuthenticationError, "Authentication failed while starting a broadcast. API Key: #{@api_key}" - when 409 - raise OpenTokBroadcastError, "The broadcast has already been started for this session." + raise OpenTokAuthenticationError, + "Authentication failed while retrieving renders. API Key: #{@api_key}" when 500 raise OpenTokError, "OpenTok server error." else - raise OpenTokBroadcastError, "The broadcast could not be started" + raise OpenTokRenderError, "The renders could not be retrieved." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def get_broadcast(broadcast_id) - response = self.class.get("/v2/project/#{@api_key}/broadcast/#{broadcast_id}", { - :headers => generate_headers + # Sip methods + + def dial(session_id, token, sip_uri, opts) + opts.extend(HashExtensions) + body = { "sessionId" => session_id, + "token" => token, + "sip" => { "uri" => sip_uri }.merge(opts.camelize_keys!) + } + + response = self.class.post("/v2/project/#{@api_key}/dial", { + :body => body.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response - when 400 - raise OpenTokBroadcastError, "The request was invalid." when 403 - raise OpenTokAuthenticationError, "Authentication failed while getting a broadcast. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed while dialing a SIP session. API Key: #{@api_key}" when 404 - raise OpenTokBroadcastError, "No matching broadcast found (with the specified ID)" - when 500 - raise OpenTokError, "OpenTok server error." + raise OpenTokSipError, "The SIP session could not be dialed. The Session ID does not exist: #{session_id}" else - raise OpenTokBroadcastError, "Could not fetch broadcast information." + raise OpenTokSipError, "The SIP session could not be dialed" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def stop_broadcast(broadcast_id) - response = self.class.post("/v2/project/#{@api_key}/broadcast/#{broadcast_id}/stop", { - :headers => generate_headers + def play_dtmf_to_connection(session_id, connection_id, dtmf_digits) + body = { "digits" => dtmf_digits } + + response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/connection/#{connection_id}/play-dtmf", { + :body => body.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response when 400 - raise OpenTokBroadcastError, "The request was invalid." + raise ArgumentError, "One of the properties — dtmf_digits #{dtmf_digits} or session_id #{session_id} — is invalid." when 403 - raise OpenTokAuthenticationError, "Authentication failed while stopping a broadcast. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed. This can occur if you use an invalid OpenTok API key or an invalid JSON web token. API Key: #{@api_key}" when 404 - raise OpenTokBroadcastError, "No matching broadcast found (with the specified ID) or it is already stopped" - when 500 - raise OpenTokError, "OpenTok server error." + raise OpenTokError, "The specified session #{session_id} does not exist or the client specified by the #{connection_id} property is not connected to the session." else - raise OpenTokBroadcastError, "The broadcast could not be stopped." + raise OpenTokError, "An error occurred when attempting to play DTMF digits to the session" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def list_broadcasts(offset, count, session_id) - query = Hash.new - query[:offset] = offset unless offset.nil? - query[:count] = count unless count.nil? - query[:sessionId] = session_id unless session_id.nil? - response = self.class.get("/v2/project/#{@api_key}/broadcast", { - :query => query.empty? ? nil : query, - :headers => generate_headers, + def play_dtmf_to_session(session_id, dtmf_digits) + body = { "digits" => dtmf_digits } + + response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/play-dtmf", { + :body => body.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response + when 400 + raise ArgumentError, "One of the properties — dtmf_digits #{dtmf_digits} or session_id #{session_id} — is invalid." when 403 - raise OpenTokAuthenticationError, - "Authentication failed while retrieving broadcasts. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed. This can occur if you use an invalid OpenTok API key or an invalid JSON web token. API Key: #{@api_key}" + when 404 + raise OpenTokError, "The specified session does not exist. Session ID: #{session_id}" else - raise OpenTokBroadcastError, "The broadcasts could not be retrieved." + raise OpenTokError, "An error occurred when attempting to play DTMF digits to the session" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def layout_broadcast(broadcast_id, opts) - opts.extend(HashExtensions) - response = self.class.put("/v2/project/#{@api_key}/broadcast/#{broadcast_id}/layout", { - :body => opts.camelize_keys!.to_json, - :headers => generate_headers("Content-Type" => "application/json") - }) + # Streams methods + + def info_stream(session_id, stream_id) + streamId = stream_id.to_s.empty? ? '' : "/#{stream_id}" + url = "/v2/project/#{@api_key}/session/#{session_id}/stream#{streamId}" + response = self.class.get(url, + headers: generate_headers('Content-Type' => 'application/json')) case response.code when 200 response when 400 - raise OpenTokBroadcastError, "The layout operation could not be performed. The request was invalid or invalid layout options." + raise ArgumentError, 'Invalid request. You did not pass in a valid session ID or stream ID.' when 403 - raise OpenTokAuthenticationError, "Authentication failed for broadcast layout. API Key: #{@api_key}" + raise OpenTokAuthenticationError, 'Check your authentication credentials. You passed in an invalid OpenTok API key.' + when 408 + raise ArgumentError, 'You passed in an invalid stream ID.' when 500 - raise OpenTokError, "OpenTok server error." + raise OpenTokError, 'OpenTok server error.' else - raise OpenTokBroadcastError, "The broadcast layout could not be performed." + raise OpenTokError, 'Could not fetch the stream information.' end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def select_streams_for_broadcast(broadcast_id, opts) + def layout_streams(session_id, opts) opts.extend(HashExtensions) - body = opts.camelize_keys! - response = self.class.patch("/v2/project/#{@api_key}/broadcast/#{broadcast_id}/streams", { - :body => body.to_json, + response = self.class.put("/v2/project/#{@api_key}/session/#{session_id}/stream", { + :body => opts.camelize_keys!.to_json, :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 204 + when 200 response when 400 - raise OpenTokBroadcastError, "The request was invalid." + raise OpenTokStreamLayoutError, "Setting the layout failed. The request was invalid or invalid layout options were given." when 403 raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" - when 404 - raise OpenTokBroadcastError, "No matching broadcast found with the specified ID: #{broadcast_id}" - when 405 - raise OpenTokBroadcastError, "The broadcast was started with streamMode set to 'auto', which does not support stream manipulation." when 500 - raise OpenTokError, "OpenTok server error." + raise OpenTokError, "Setting the layout failed. OpenTok server error." else - raise OpenTokBroadcastError, "The broadcast streams could not be updated." + raise OpenTokStreamLayoutError, "Setting the layout failed." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def start_render(session_id, opts) - opts.extend(HashExtensions) - body = { :sessionId => session_id }.merge(opts.camelize_keys!) - response = self.class.post("/v2/project/#{@api_key}/render", { - :body => body.to_json, + def force_mute_stream(session_id, stream_id) + response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/stream/#{stream_id}/mute", { :headers => generate_headers("Content-Type" => "application/json") }) case response.code - when 202 + when 200 response when 400 - raise OpenTokRenderError, "The render could not be started. The request was invalid." + raise ArgumentError, "Force mute failed. Stream ID #{stream_id} or Session ID #{session_id} is invalid" when 403 - raise OpenTokAuthenticationError, "Authentication failed while starting a render. API Key: #{@api_key}" - when 500 - raise OpenTokError, "OpenTok server error." - else - raise OpenTokRenderError, "The render could not be started" + raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" + when 404 + raise OpenTokConnectionError, "Either Stream ID #{stream_id} or Session ID #{session_id} is invalid" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def get_render(render_id) - response = self.class.get("/v2/project/#{@api_key}/render/#{render_id}", { - :headers => generate_headers + def force_mute_session(session_id, opts) + opts.extend(HashExtensions) + body = opts.camelize_keys! + response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/mute", { + :body => body.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response when 400 - raise OpenTokRenderError, "The request was invalid." + raise ArgumentError, "Force mute failed. The request could not be processed due to a bad request" when 403 - raise OpenTokAuthenticationError, "Authentication failed while getting a render. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}" when 404 - raise OpenTokRenderError, "No matching render found (with the specified ID)" - when 500 - raise OpenTokError, "OpenTok server error." - else - raise OpenTokRenderError, "Could not fetch render information." + raise OpenTokConnectionError, "Session ID #{session_id} is invalid" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def stop_render(render_id) - response = self.class.delete("/v2/project/#{@api_key}/render/#{render_id}", { - :headers => generate_headers + # Signals methods + + def signal(session_id, connection_id, opts) + opts.extend(HashExtensions) + connectionPath = connection_id.to_s.empty? ? "" : "/connection/#{connection_id}" + url = "/v2/project/#{@api_key}/session/#{session_id}#{connectionPath}/signal" + response = self.class.post(url, { + :body => opts.camelize_keys!.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 204 response when 400 - raise OpenTokRenderError, "The request was invalid." + raise ArgumentError, "One of the signal properties — data, type, sessionId or connectionId — is invalid." when 403 - raise OpenTokAuthenticationError, "Authentication failed while stopping a render. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "You are not authorized to send the signal. Check your authentication credentials." when 404 - raise OpenTokRenderError, "No matching render found (with the specified ID) or it is already stopped" - when 500 - raise OpenTokError, "OpenTok server error." + raise OpenTokError, "The client specified by the connectionId property is not connected to the session." + when 413 + raise OpenTokError, "The type string exceeds the maximum length (128 bytes), or the data string exceeds the maximum size (8 kB)." else - raise OpenTokRenderError, "The render could not be stopped." + raise OpenTokError, "The signal could not be send." end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def list_renders(offset, count) - query = Hash.new - query[:offset] = offset unless offset.nil? - query[:count] = count unless count.nil? - response = self.class.get("/v2/project/#{@api_key}/render", { - :query => query.empty? ? nil : query, - :headers => generate_headers, + # WebSocket methods + + def connect_websocket(session_id, token, websocket_uri, opts) + opts.extend(HashExtensions) + body = { "sessionId" => session_id, + "token" => token, + "websocket" => { "uri" => websocket_uri }.merge(opts.camelize_keys!) + } + + response = self.class.post("/v2/project/#{@api_key}/connect", { + :body => body.to_json, + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response + when 400 + raise ArgumentError, "One of the properties is invalid." when 403 - raise OpenTokAuthenticationError, - "Authentication failed while retrieving renders. API Key: #{@api_key}" + raise OpenTokAuthenticationError, "You are not authorized to start the call, check your authentication information." + when 409 + raise OpenTokWebSocketError, "Conflict. Only routed sessions are allowed to initiate Connect Calls." when 500 raise OpenTokError, "OpenTok server error." else - raise OpenTokRenderError, "The renders could not be retrieved." + raise OpenTokWebSocketError, "The WebSocket could not be connected" end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - end end