class Vonage::Voice::Actions::Connect

Attributes

endpoint[RW]
eventMethod[RW]
eventType[RW]
eventUrl[RW]
from[RW]
limit[RW]
machineDetection[RW]
ringbackTone[RW]
timeout[RW]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 9
def initialize(attributes = {})
  @endpoint = attributes.fetch(:endpoint)
  @from = attributes.fetch(:from, nil)
  @eventType = attributes.fetch(:eventType, nil)
  @timeout = attributes.fetch(:timeout, nil)
  @limit = attributes.fetch(:limit, nil)
  @machineDetection = attributes.fetch(:machineDetection, nil)
  @eventUrl = attributes.fetch(:eventUrl, nil)
  @eventMethod = attributes.fetch(:eventMethod, nil)
  @ringbackTone = attributes.fetch(:ringbackTone, nil)

  after_initialize!
end

Public Instance Methods

action() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 107
def action
  create_connect!(self)
end
after_initialize!() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 23
def after_initialize!
  verify_endpoint

  if self.from
    verify_from
  end

  if self.eventType
    verify_event_type
  end

  if self.limit
    verify_limit
  end

  if self.machineDetection
    verify_machine_detection
  end

  if self.eventUrl
    verify_event_url
  end

  if self.eventMethod
    verify_event_method
  end

  if self.ringbackTone
    verify_ringback_tone
  end
end
app_endpoint(endpoint_attrs) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 162
def app_endpoint(endpoint_attrs)
  {
    type: 'app',
    user: endpoint_attrs[:user]
  }
end
create_connect!(builder) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 111
def create_connect!(builder)
  ncco = [
    {
      action: 'connect',
      endpoint: [
        create_endpoint(builder)
      ]
    }
  ]

  ncco[0].merge!(from: builder.from) if builder.from
  ncco[0].merge!(eventType: builder.eventType) if builder.eventType
  ncco[0].merge!(timeout: builder.timeout) if builder.timeout
  ncco[0].merge!(limit: builder.limit) if builder.limit
  ncco[0].merge!(machineDetection: builder.machineDetection) if builder.machineDetection
  ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
  ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
  ncco[0].merge!(ringbackTone: builder.ringbackTone) if builder.ringbackTone

  ncco
end
create_endpoint(builder) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 133
def create_endpoint(builder)
  case builder.endpoint[:type]
  when 'phone'
    phone_endpoint(builder.endpoint)
  when 'app'
    app_endpoint(builder.endpoint)
  when 'websocket'
    websocket_endpoint(builder.endpoint)
  when 'sip'
    sip_endpoint(builder.endpoint)
  when 'vbc'
    vbc_endpoint(builder.endpoint)
  else
    raise ClientError.new("Invalid value for 'endpoint', please refer to the Vonage API Developer Portal https://developer.nexmo.com/voice/voice-api/ncco-reference#endpoint-types-and-values for a list of possible values")
  end
end
phone_endpoint(endpoint_attrs) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 150
def phone_endpoint(endpoint_attrs)
  hash = {
    type: 'phone',
    number: endpoint_attrs[:number]
  }

  hash.merge!(dtmfAnswer: endpoint_attrs[:dtmfAnswer]) if endpoint_attrs[:dtmfAnswer]
  hash.merge!(onAnswer: endpoint_attrs[:onAnswer]) if endpoint_attrs[:onAnswer]

  hash
end
sip_endpoint(endpoint_attrs) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 181
def sip_endpoint(endpoint_attrs)
  hash = {
    type: 'sip',
    uri: endpoint_attrs[:uri]
  }

  hash.merge!(headers: endpoint_attrs[:headers]) if endpoint_attrs[:headers]

  hash
end
vbc_endpoint(endpoint_attrs) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 192
def vbc_endpoint(endpoint_attrs)
  {
    type: 'vbc',
    extension: endpoint_attrs[:extension]
  }
end
verify_endpoint() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 55
def verify_endpoint
  case self.endpoint[:type]
  when 'phone'
    raise ClientError.new("Expected 'number' value to be in E.164 format") unless Phonelib.parse(endpoint[:number].to_i).valid?
  when 'app'
    raise ClientError.new("'user' must be defined") unless endpoint[:user]
  when 'websocket'
    raise ClientError.new("Expected 'uri' value to be a valid URI") unless URI.parse(endpoint[:uri]).kind_of?(URI::Generic)
    raise ClientError.new("Expected 'content-type' parameter to be either 'audio/116;rate=16000' or 'audio/116;rate=8000") unless endpoint[:'content-type'] == 'audio/116;rate=16000' || endpoint[:'content-type'] == 'audio/116;rate=8000'
  when 'sip'
    raise ClientError.new("Expected 'uri' value to be a valid URI") unless URI.parse(endpoint[:uri]).kind_of?(URI::Generic)
  end
end
verify_event_method() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 93
def verify_event_method
  valid_methods = ['GET', 'POST']

  raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
end
verify_event_type() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 73
def verify_event_type
  raise ClientError.new("Invalid 'eventType' value, must be 'synchronous' if defined") unless self.eventType == 'synchronous'
end
verify_event_url() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 85
def verify_event_url
  uri = URI.parse(self.eventUrl)

  raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

  self.eventUrl
end
verify_from() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 69
def verify_from
  raise ClientError.new("Invalid 'from' value, must be in E.164 format") unless Phonelib.parse(self.from.to_i).valid?
end
verify_limit() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 77
def verify_limit
  raise ClientError.new("Invalid 'limit' value, must be between 0 and 7200 seconds") unless self.limit.to_i >= 0 && self.limit.to_i <= 7200
end
verify_machine_detection() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 81
def verify_machine_detection
  raise ClientError.new("Invalid 'machineDetection' value, must be either: 'continue' or 'hangup' if defined") unless self.machineDetection == 'continue' || self.machineDetection == 'hangup'
end
verify_ringback_tone() click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 99
def verify_ringback_tone
  uri = URI.parse(self.ringbackTone)

  raise ClientError.new("Invalid 'ringbackTone' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

  self.ringbackTone
end
websocket_endpoint(endpoint_attrs) click to toggle source
# File lib/vonage/voice/actions/connect.rb, line 169
def websocket_endpoint(endpoint_attrs)
  hash = {
    type: 'websocket',
    uri: endpoint_attrs[:uri],
    :'content-type' => endpoint_attrs[:'content-type']
  }

  hash.merge!(headers: endpoint_attrs[:headers]) if endpoint_attrs[:headers]

  hash
end