Skip to content

Commit 27edfc4

Browse files
authored
DX-2466 add modifyCallBxml functionality (#52)
* DX-2466 add `modifyCallBxml` functionality * fix 415, description, and update version
1 parent 10114fe commit 27edfc4

File tree

6 files changed

+153
-7
lines changed

6 files changed

+153
-7
lines changed

bandwidth.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'bandwidth-sdk'
3-
s.version = '9.3.0'
3+
s.version = '9.4.0'
44
s.summary = 'Bandwidth'
55
s.description = 'The official client SDK for Bandwidht\'s Voice, Messaging, MFA, and WebRTC APIs'
66
s.authors = ['Bandwidth']

lib/bandwidth.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
require_relative 'bandwidth/http/auth/web_rtc_basic_auth.rb'
4848

4949
# External Files
50+
require_relative 'bandwidth/voice_lib/bxml/response.rb'
5051
require_relative 'bandwidth/voice_lib/bxml/bxml.rb'
5152
require_relative 'bandwidth/voice_lib/bxml/verbs/bridge.rb'
5253
require_relative 'bandwidth/voice_lib/bxml/verbs/conference.rb'

lib/bandwidth/voice_lib/bxml/bxml.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
require_relative file
44
}
55

6-
SSML_REGEX = /<([a-zA-Z\/\/].*?)>/
7-
SPEAK_SENTENCE_REGEX = /<SpeakSentence.*?>.*?<\/SpeakSentence>/
8-
96
module Bandwidth
107
module Voice
11-
class Response
8+
class Bxml
129
# Initializer
13-
# @param verbs [Array] optional list of verbs to include into response
10+
# @param verbs [Array] optional list of verbs to include in the bxml tag
1411
def initialize(verbs = nil)
1512
@verbs = verbs || []
1613
end
@@ -19,7 +16,7 @@ def initialize(verbs = nil)
1916
def to_bxml()
2017
xml = Builder::XmlMarkup.new()
2118
xml.instruct!(:xml, :version=>'1.0', :encoding=>'UTF-8')
22-
xml.Response do
19+
xml.Bxml do
2320
@verbs.each {|verb| verb.to_bxml(xml)}
2421
end
2522
xml.target!().gsub(SPEAK_SENTENCE_REGEX){|s|s.gsub(SSML_REGEX, '<\1>')}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'builder'
2+
Dir[File.dirname(__FILE__) + '/verbs/*'].each {|file|
3+
require_relative file
4+
}
5+
6+
SSML_REGEX = /&lt;([a-zA-Z\/\/].*?)&gt;/
7+
SPEAK_SENTENCE_REGEX = /<SpeakSentence.*?>.*?<\/SpeakSentence>/
8+
9+
module Bandwidth
10+
module Voice
11+
class Response
12+
# Initializer
13+
# @param verbs [Array] optional list of verbs to include into response
14+
def initialize(verbs = nil)
15+
@verbs = verbs || []
16+
end
17+
18+
# Return BXML representaion of this response
19+
def to_bxml()
20+
xml = Builder::XmlMarkup.new()
21+
xml.instruct!(:xml, :version=>'1.0', :encoding=>'UTF-8')
22+
xml.Response do
23+
@verbs.each {|verb| verb.to_bxml(xml)}
24+
end
25+
xml.target!().gsub(SPEAK_SENTENCE_REGEX){|s|s.gsub(SSML_REGEX, '<\1>')}
26+
end
27+
28+
# Add one or more verbs to this response
29+
def push(*verbs)
30+
@verbs.push(*verbs)
31+
end
32+
33+
# Add a verb to this response
34+
def <<(verb)
35+
@verbs << verb
36+
end
37+
end
38+
end
39+
end

lib/bandwidth/voice_lib/voice/controllers/api_controller.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,87 @@ def modify_call(account_id,
252252
ApiResponse.new(_response)
253253
end
254254

255+
# Makes a PUT request to /api/v2/accounts/{accountId}/calls/{callId}/bxml
256+
# @param [String] account_id Required parameter: Example:
257+
# @param [String] call_id Required parameter: Example:
258+
# @param [String] body Required parameter: Example:
259+
# @return [void] response from the API call
260+
def modify_call_bxml(account_id,
261+
call_id,
262+
body
263+
)
264+
# Prepare query url.
265+
_query_builder = config.get_base_uri(Server::VOICEDEFAULT)
266+
_query_builder << '/api/v2/accounts/{accountId}/calls/{callId}/bxml'
267+
_query_builder = APIHelper.append_url_with_template_parameters(
268+
_query_builder,
269+
'accountId' => { 'value' => account_id, 'encode' => false },
270+
'callId' => { 'value' => call_id, 'encode' => false }
271+
)
272+
_query_url = APIHelper.clean_url _query_builder
273+
274+
# Prepare headers.
275+
_headers = {
276+
'content-type' => 'application/xml; charset=utf-8'
277+
}
278+
279+
# Prepare and execute HttpRequest.
280+
_request = config.http_client.put(
281+
_query_url,
282+
headers: _headers,
283+
parameters: body.to_json
284+
)
285+
VoiceBasicAuth.apply(config, _request)
286+
_response = execute_request(_request)
287+
288+
# Validate response against endpoint and global error codes.
289+
case _response.status_code
290+
when 400
291+
raise ApiErrorException.new(
292+
'Something\'s not quite right... Your request is invalid. Please' \
293+
' fix it before trying again.',
294+
_response
295+
)
296+
when 401
297+
raise APIException.new(
298+
'Your credentials are invalid. Please use your Bandwidth dashboard' \
299+
' credentials to authenticate to the API.',
300+
_response
301+
)
302+
when 403
303+
raise ApiErrorException.new(
304+
'User unauthorized to perform this action.',
305+
_response
306+
)
307+
when 404
308+
raise ApiErrorException.new(
309+
'The resource specified cannot be found or does not belong to you.',
310+
_response
311+
)
312+
when 415
313+
raise ApiErrorException.new(
314+
'We don\'t support that media type. If a request body is required,' \
315+
' please send it to us as `application/xml`.',
316+
_response
317+
)
318+
when 429
319+
raise ApiErrorException.new(
320+
'You\'re sending requests to this endpoint too frequently. Please' \
321+
' slow your request rate down and try again.',
322+
_response
323+
)
324+
when 500
325+
raise ApiErrorException.new(
326+
'Something unexpected happened. Please try again.',
327+
_response
328+
)
329+
end
330+
validate_response(_response)
331+
332+
# Return appropriate response type.
333+
ApiResponse.new(_response)
334+
end
335+
255336
# Pauses or resumes a recording.
256337
# @param [String] account_id Required parameter: Example:
257338
# @param [String] call_id Required parameter: Example:

test/integration/test_integration.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ def test_gather_multiple_nested_verbs
262262
assert_equal(expected, actual)
263263
end
264264

265+
def test_empty_bxml_verb
266+
bxml = Bandwidth::Voice::Bxml.new()
267+
268+
expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml></Bxml>'
269+
actual = bxml.to_bxml()
270+
assert_equal(expected, actual)
271+
end
272+
273+
def test_bxml_speak_sentence_pause
274+
bxml = Bandwidth::Voice::Bxml.new()
275+
276+
speak_sentence = Bandwidth::Voice::SpeakSentence.new({
277+
:sentence => "new modify call bxml is pog",
278+
:voice => "Julie"
279+
})
280+
281+
pause = Bandwidth::Voice::Pause.new({
282+
:duration => 6
283+
})
284+
285+
bxml.push(speak_sentence)
286+
bxml.push(pause)
287+
288+
expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml><SpeakSentence voice="Julie">new modify call bxml is pog</SpeakSentence><Pause duration="6"/></Bxml>'
289+
actual = bxml.to_bxml()
290+
assert_equal(expected, actual)
291+
end
292+
265293
def test_hangup
266294
hangup = Bandwidth::Voice::Hangup.new()
267295

0 commit comments

Comments
 (0)