Class: Pinnacle::Wrapper::Voice::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pinnacle/wrapper/voice/client.rb

Defined Under Namespace

Classes: DefaultSocket

Constant Summary collapse

DEFAULT_RECONNECT =
{ enabled: true }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Client

Returns a new instance of Client.



55
56
57
# File 'lib/pinnacle/wrapper/voice/client.rb', line 55

def initialize(client:)
  @calls = Pinnacle::Calls::Client.new(client: client)
end

Instance Method Details

#connect(call_id:, token: nil, protocols: nil, reconnect: nil, socket: nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pinnacle/wrapper/voice/client.rb', line 86

def connect(call_id:, token: nil, protocols: nil, reconnect: nil, socket: nil)
  socket_class = socket || default_socket
  token_params = token || {}
  stream_token = @calls.create_stream_token(**token_params, id: call_id)
  create_socket = -> do
    next_token = @calls.create_stream_token(**token_params, id: call_id)
    new_socket(socket_class, next_token.stream_url, protocols)
  end

  VoiceSocket.new(
    new_socket(socket_class, stream_token.stream_url, protocols),
    create_socket: create_socket,
    reconnect: DEFAULT_RECONNECT.merge(reconnect || {})
  )
end

#connect_stream(stream_url, protocols: nil, reconnect: nil, socket: nil) ⇒ Object



102
103
104
105
106
# File 'lib/pinnacle/wrapper/voice/client.rb', line 102

def connect_stream(stream_url, protocols: nil, reconnect: nil, socket: nil)
  socket_class = socket || default_socket
  create_socket = -> { new_socket(socket_class, stream_url, protocols) }
  VoiceSocket.new(create_socket.call, create_socket: create_socket, reconnect: reconnect)
end

#create_and_connect(to:, from:, record: nil, metadata: nil, token: nil, protocols: nil, reconnect: nil, socket: nil, request_options: {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pinnacle/wrapper/voice/client.rb', line 59

def create_and_connect(
  to:,
  from:,
  record: nil,
  metadata: nil,
  token: nil,
  protocols: nil,
  reconnect: nil,
  socket: nil,
  request_options: {}
)
  call_params = { to: to, from: from }
  call_params[:record] = record unless record.nil?
  call_params[:metadata] =  unless .nil?
  call = @calls.create(**call_params, request_options: request_options)
  connection = connect(
    call_id: call.id,
    token: token,
    protocols: protocols,
    reconnect: DEFAULT_RECONNECT.merge(reconnect || {}),
    socket: socket
  )
  connection.call = call
  connection.call_id = call.id
  connection
end