Skip to content

Commit 23c6380

Browse files
authored
DEVX-6803: Adding Captions API (#267)
* Adding Captions API functionality to the library
1 parent c39bdbc commit 23c6380

10 files changed

+523
-209
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 4.8.0
2+
3+
* Add support for Captions API [#267](https://github.com/opentok/OpenTok-Ruby-SDK/pull/267)
4+
15
# 4.7.1
26

37
* Updates docs comments for `Broadcasts` and `Sip` [#266](https://github.com/opentok/OpenTok-Ruby-SDK/pull/266)

lib/opentok/captions.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module OpenTok
2+
# A class for working with OpenTok captions.
3+
class Captions
4+
# @private
5+
def initialize(client)
6+
@client = client
7+
end
8+
9+
# Starts live captions for the specified OpenTok session.
10+
# See the {https://tokbox.com/developer/guides/live-captions/ OpenTok Live Captions developer guide}.
11+
#
12+
# @example
13+
# opts = { "language_code" => "en-GB",
14+
# "max_duration" => 5000,
15+
# "partial_captions" => false,
16+
# "status_callback_url" => status_callback_url
17+
# }
18+
# response = opentok.captions.start(session_id, token, opts)
19+
#
20+
# @param [String] session_id The session ID corresponding to the session for which captions will start.
21+
# @param [String] token The token for the session ID with which the SIP user will use to connect.
22+
# @param [Hash] options A hash defining options for the captions. For example:
23+
# @option options [String] :language_code The BCP-47 code for a spoken language used on this call.
24+
# The default value is "en-US". The following language codes are supported:
25+
# - "en-AU" (English, Australia)
26+
# - "en-GB" (Englsh, UK)
27+
# - "es-US" (English, US)
28+
# - "zh-CN” (Chinese, Simplified)
29+
# - "fr-FR" (French)
30+
# - "fr-CA" (French, Canadian)
31+
# - "de-DE" (German)
32+
# - "hi-IN" (Hindi, Indian)
33+
# - "it-IT" (Italian)
34+
# - "ja-JP" (Japanese)
35+
# - "ko-KR" (Korean)
36+
# - "pt-BR" (Portuguese, Brazilian)
37+
# - "th-TH" (Thai)
38+
# @option options [Integer] :max_duration The maximum duration for the audio captioning, in seconds.
39+
# The default value is 14,400 seconds (4 hours), the maximum duration allowed.
40+
# @option options [Boolean] :partial_captions Whether to enable this to faster captioning at the cost of some
41+
# degree of inaccuracies. The default value is `true`.
42+
# @option options [String] :status_callback_url A publicly reachable URL controlled by the customer and capable
43+
# of generating the content to be rendered without user intervention. The minimum length of the URL is 15
44+
# characters and the maximum length is 2048 characters.
45+
# For more information, see {https://tokbox.com/developer/guides/live-captions/#live-caption-status-updates Live Caption status updates}.
46+
def start(session_id, token, options = {})
47+
@client.start_live_captions(session_id, token, options)
48+
end
49+
50+
# Starts live captions for the specified OpenTok session.
51+
# See the {https://tokbox.com/developer/guides/live-captions/ OpenTok Live Captions developer guide}.
52+
#
53+
# @example
54+
# response = opentok.captions.stop(captions_id)
55+
#
56+
# @param [String] captions_id The ID for the captions to be stopped (returned from the `start` request).
57+
def stop(captions_id)
58+
@client.stop_live_captions(captions_id)
59+
end
60+
end
61+
end

lib/opentok/client.rb

+268-203
Large diffs are not rendered by default.

lib/opentok/exceptions.rb

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ class OpenTokBroadcastError < OpenTokError; end
1818
class OpenTokWebSocketError < OpenTokError; end
1919
# Defines errors raised when you perform Experience Composer render operations.
2020
class OpenTokRenderError < OpenTokError; end
21+
# Defines errors raised when you perform Captions operations.
22+
class OpenTokCaptionsError < OpenTokError; end
2123
end

lib/opentok/opentok.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require "opentok/signals"
1313
require "opentok/broadcasts"
1414
require "opentok/renders"
15+
require "opentok/captions"
1516

1617
module OpenTok
1718
# Contains methods for creating OpenTok sessions and generating tokens. It also includes
@@ -214,6 +215,16 @@ def broadcasts
214215
@broadcasts ||= Broadcasts.new client
215216
end
216217

218+
# A Captions object, which lets you start and stop live captions for an OpenTok session.
219+
def captions
220+
@captions ||= Captions.new client
221+
end
222+
223+
# A Connections object, which lets you disconnect clients from an OpenTok session.
224+
def connections
225+
@connections ||= Connections.new client
226+
end
227+
217228
# A Renders object, which lets you work with OpenTok Experience Composer renders.
218229
def renders
219230
@renders ||= Renders.new client
@@ -234,11 +245,6 @@ def signals
234245
@signals ||= Signals.new client
235246
end
236247

237-
# A Connections object, which lets you disconnect clients from an OpenTok session.
238-
def connections
239-
@connections ||= Connections.new client
240-
end
241-
242248
# A WebSocket object, which lets you connect OpenTok streams to a WebSocket URI.
243249
def websocket
244250
@websocket ||= WebSocket.new client

lib/opentok/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module OpenTok
22
# @private
3-
VERSION = '4.7.1'
3+
VERSION = '4.8.0'
44
end

spec/cassettes/OpenTok_Captions/receives_a_valid_response_when_starting_captions.yml

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/OpenTok_Captions/receives_a_valid_response_when_starting_captions_with_options.yml

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/OpenTok_Captions/receives_a_valid_response_when_stopping_captions.yml

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/opentok/captions_spec.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "opentok/opentok"
2+
require "opentok/captions"
3+
require "opentok/version"
4+
require "spec_helper"
5+
6+
describe OpenTok::Captions do
7+
before(:each) do
8+
now = Time.parse("2017-04-18 20:17:40 +1000")
9+
allow(Time).to receive(:now) { now }
10+
end
11+
12+
let(:api_key) { "123456" }
13+
let(:api_secret) { "1234567890abcdef1234567890abcdef1234567890" }
14+
let(:session_id) { "SESSIONID" }
15+
let(:captions_id) { "CAPTIONSID" }
16+
let(:expiring_token) { "TOKENID" }
17+
let(:status_callback_url) { "https://example.com/captions/status" }
18+
let(:opentok) { OpenTok::OpenTok.new api_key, api_secret }
19+
let(:captions) { opentok.captions }
20+
subject { captions }
21+
22+
it "receives a valid response when starting captions", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
23+
response = captions.start(session_id, expiring_token)
24+
expect(response).not_to be_nil
25+
expect(response.code).to eq(202)
26+
end
27+
28+
it "receives a valid response when starting captions with options", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
29+
opts = { "language_code" => "en-GB",
30+
"max_duration" => 5000,
31+
"partial_captions" => false,
32+
"status_callback_url" => status_callback_url
33+
}
34+
35+
response = captions.start(session_id, expiring_token, opts)
36+
expect(response).not_to be_nil
37+
expect(response.code).to eq(202)
38+
end
39+
40+
it "receives a valid response when stopping captions", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
41+
response = captions.stop(captions_id)
42+
expect(response.code).to eq(202)
43+
end
44+
end

0 commit comments

Comments
 (0)