Skip to content

Commit

Permalink
add additional actions to conference (#55)
Browse files Browse the repository at this point in the history
* add additional actions to conference

- actions/play
- actions/record_stop
- actions/record_start
- actions/speak

* add more actions

- actions/dial_participant
- actions/update
- /participants
  • Loading branch information
pyrareae authored Mar 10, 2021
1 parent 73ea6f8 commit a443965
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
18 changes: 17 additions & 1 deletion lib/telnyx/conference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@ class Conference < APIResource
extend APIOperations::Create
extend APIOperations::NestedResource

ACTIONS = %w[join mute unmute hold unhold].freeze
ACTIONS = %w[join mute unmute hold unhold speak play dial_participant update].freeze

ACTIONS.each do |action|
nested_resource_class_methods action,
path: ["actions", action],
operations: [:create],
instance_methods: { create: action }
end

nested_resource_class_methods "record_start",
path: ["actions", "record_start"],
operations: [:create],
instance_methods: { create: "start_recording" }

nested_resource_class_methods "record_stop",
path: ["actions", "record_stop"],
operations: [:create],
instance_methods: { create: "stop_recording" }

nested_resource_class_methods "participants",
path: "participants",
operations: [:list],
instance_methods: { list: "participants" }

OBJECT_NAME = "conference".freeze
end
Conferences = Conference # Name change without breaking existing code
Expand Down
77 changes: 57 additions & 20 deletions test/telnyx/conference_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
module Telnyx
class ConferenceTest < Test::Unit::TestCase
setup do
@call = create_call
@conference = Conference.create call_control_id: @call.id, name: "conference!"
@conference = Conference.create call_control_id: "foobar", name: "conference!"
end
should "create conference" do
assert_requested :post, "#{Telnyx.api_base}/v2/conferences"
assert_kind_of Conference, @conference
end

should "retrieve conference" do
conference = Conference.retrieve "foobar"
assert_kind_of Conference, conference
assert_requested :get, "#{Telnyx.api_base}/v2/conferences/foobar"
end

should "list conferences" do
conferences = Conference.list

Expand All @@ -21,55 +26,87 @@ class ConferenceTest < Test::Unit::TestCase
assert_kind_of Conference, conferences.first
end

should "list participants" do
participants = @conference.participants
assert_requested :get, "#{Telnyx.api_base}/v2/conferences/#{@conference.id}/participants"
assert_kind_of ListObject, participants
end

should "have nested command instance methods" do
assert defined? @conference.join
assert defined? @conference.mute
assert defined? @conference.unmute
assert defined? @conference.unhold
assert defined? @conference.play
assert defined? @conference.start_recording
assert defined? @conference.stop_recording
assert defined? @conference.speak
assert defined? @conference.dial_participant
assert defined? @conference.update
end

context "commands" do
should "join" do
stub = stub_request(:post, format_url(@conference, "join"))
.to_return(body: JSON.generate(result: "ok"))
@conference.join
assert_requested stub
@conference.join call_control_id: "foo_bar_baz"
assert_requested :post, action_url(@conference, "join")
end

should "mute" do
stub = stub_request(:post, format_url(@conference, "mute"))
.to_return(body: JSON.generate(result: "ok"))
@conference.mute
assert_requested stub
assert_requested :post, action_url(@conference, "mute")
end

should "unmute" do
stub = stub_request(:post, format_url(@conference, "unmute"))
.to_return(body: JSON.generate(result: "ok"))
@conference.unmute
assert_requested stub
assert_requested :post, action_url(@conference, "unmute")
end

should "hold" do
stub = stub_request(:post, format_url(@conference, "hold"))
.to_return(body: JSON.generate(result: "ok"))
@conference.hold
assert_requested stub
assert_requested :post, action_url(@conference, "hold")
end

should "unhold" do
stub = stub_request(:post, format_url(@conference, "unhold"))
.to_return(body: JSON.generate(result: "ok"))
@conference.unhold
assert_requested stub
@conference.unhold call_control_ids: %w[foo bar baz]
assert_requested :post, action_url(@conference, "unhold")
end

should "play" do
@conference.play audio_url: "https://example.com/audio.mp3"
assert_requested :post, action_url(@conference, "play")
end

should "start recording" do
@conference.start_recording channels: "dual", format: "mp3"
assert_requested :post, action_url(@conference, "record_start")
end

should "stop recording" do
@conference.stop_recording
assert_requested :post, action_url(@conference, "record_stop")
end

should "speak" do
@conference.speak language: "en-US", payload: "test speech", voice: "female"
assert_requested :post, action_url(@conference, "speak")
end

should "dial participant" do
@conference.dial_participant call_control_id: "foo", to: "+12223334444", from: "+12223335555"
assert_requested :post, action_url(@conference, "dial_participant")
end

should "update" do
@conference.update call_control_id: "foo"
assert_requested :post, action_url(@conference, "update")
end
end

def create_call
Telnyx::Call.create connection_id: "12345", to: "+15550001111", from: "+15550002222"
end

def format_url(conf, action)
def action_url(conf, action)
"#{Telnyx.api_base}/v2/conferences/#{conf.id}/actions/#{action}"
end
end
Expand Down

0 comments on commit a443965

Please sign in to comment.