Skip to content

Commit

Permalink
adds a dial method and associated spec to initiate a SIP call (#133)
Browse files Browse the repository at this point in the history
* adds a dial method and associated spec to initiate a SIP call

* Ensure dial request has JWT auth header, including test cassette

* Ensure the correct mock time is used in the SIP spec tests

* Don't specify patch number for ruby version in test matrix
  • Loading branch information
herestomwiththeweather authored and aiham committed Sep 7, 2017
1 parent 734b52b commit 5e230a0
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Expand Up @@ -6,11 +6,11 @@ cache: bundler
before_install: gem update bundler
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
- 2.3.0
- 2.4.0
- 2.0
- 2.1
- 2.2
- 2.3
- 2.4
notifications:
slack:
secure: agVll2R9PTPvJMcUgbvOh9eGt60zGDc8kPUwEsiQr828rCgXh/ZxD5irYDyKQg3ZsS8+f3MjFCwzU7uQALkia2pDrie9d8g8m1dt4Q5U7Qm6QecshvE0U9JwbB5Ngxaftbqyf0XEFrE7CKs7RI1BzFRpe8m+fdZgfwccX8Gb7pc=
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -178,6 +178,24 @@ as the `:archive_mode` property of the `options` parameter passed into the
For more information on archiving, see the
[OpenTok archiving](https://tokbox.com/opentok/tutorials/archiving/) programming guide.


## Initiating a SIP call

You can initiate a SIP call using the `opentok.sip.dial(session_id, token, sip_uri, opts)` method. This requires a SIP url. You will often need to pass options for authenticating to the SIP provider and specifying encrypted session establishment.


```ruby
opts = { "auth" => { "username" => sip_username,
"password" => sip_password },
"secure" => "true"
}
response = opentok.sip.dial(session_id, token, "sip:+15128675309@acme.pstn.example.com;transport=tls", opts)
```

For more information on SIP Interconnect, see the
[OpenTok SIP Interconnect](https://tokbox.com/developer/guides/sip/) programming guide.


# Samples

There are two sample applications included in this repository. To get going as fast as possible, clone the whole
Expand Down
25 changes: 25 additions & 0 deletions lib/opentok/client.rb
Expand Up @@ -162,5 +162,30 @@ def delete_archive(archive_id)
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,
:headers => generate_headers("Content-Type" => "application/json")
})
case response.code
when 200
response
when 403
raise OpenTokAuthenticationError, "Authentication failed while dialing a sip session. API Key: #{@api_key}"
when 404
raise OpenTokSipError, "The sip session could not be dialed. The Session ID does not exist: #{session_id}"
else
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
end
end
2 changes: 2 additions & 0 deletions lib/opentok/exceptions.rb
Expand Up @@ -4,6 +4,8 @@ module OpenTok
class OpenTokError < StandardError; end
# Defines errors raised by archive-related methods of the OpenTok Ruby SDK.
class OpenTokArchiveError < OpenTokError; end
# Defines errors raised by SIP methods of the OpenTok Ruby SDK.
class OpenTokSipError < OpenTokError; end
# Defines errors raised when you attempt an operation using an invalid OpenTok API key or secret.
class OpenTokAuthenticationError < OpenTokError; end

Expand Down
5 changes: 5 additions & 0 deletions lib/opentok/opentok.rb
Expand Up @@ -3,6 +3,7 @@
require "opentok/client"
require "opentok/token_generator"
require "opentok/archives"
require "opentok/sip"

require "resolv"
require "set"
Expand Down Expand Up @@ -172,6 +173,10 @@ def archives
@archives ||= Archives.new client
end

def sip
@sip ||= Sip.new client
end

protected

def client
Expand Down
13 changes: 13 additions & 0 deletions lib/opentok/sip.rb
@@ -0,0 +1,13 @@
require "opentok/client"

module OpenTok
class Sip
def dial(session_id, token, sip_uri, opts)
response = @client.dial(session_id, token, sip_uri, opts)
end

def initialize(client)
@client = client
end
end
end
38 changes: 38 additions & 0 deletions spec/cassettes/OpenTok_Sip/receives_a_valid_response.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions spec/opentok/sip_spec.rb
@@ -0,0 +1,30 @@
require "opentok/opentok"
require "opentok/sip"
require "spec_helper"

describe OpenTok::Sip do
before(:each) do
now = Time.parse("2017-04-18 20:17:40 +1000")
allow(Time).to receive(:now) { now }
end

let(:api_key) { "123456" }
let(:api_secret) { "1234567890abcdef1234567890abcdef1234567890" }
let(:session_id) { "SESSIONID" }
let(:expiring_token) { "TOKENID" }
let(:sip_uri) { "sip:+15128675309@acme.pstn.example.com;transport=tls" }
let(:sip_username) { "bob" }
let(:sip_password) { "abc123" }
let(:opentok) { OpenTok::OpenTok.new api_key, api_secret }
let(:sip) { opentok.sip }
subject { sip }

it "receives a valid response", :vcr => { :erb => { :version => OpenTok::VERSION } } do
opts = { "auth" => { "username" => sip_username,
"password" => sip_password },
"secure" => "true"
}
response = sip.dial(session_id, expiring_token, sip_uri, opts)
expect(response).not_to be_nil
end
end

0 comments on commit 5e230a0

Please sign in to comment.