Class: Voice::Actions::Connect

Inherits:
Object
  • Object
show all
Defined in:
lib/vonage/voice/actions/connect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Connect

Returns a new instance of Connect.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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)
  @advanced_machine_detection = attributes.fetch(:advanced_machine_detection, nil)
  @eventUrl = attributes.fetch(:eventUrl, nil)
  @eventMethod = attributes.fetch(:eventMethod, nil)
  @ringbackTone = attributes.fetch(:ringbackTone, nil)

  after_initialize!
end

Instance Attribute Details

#advanced_machine_detectionObject

Returns the value of attribute advanced_machine_detection.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def advanced_machine_detection
  @advanced_machine_detection
end

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def endpoint
  @endpoint
end

#eventMethodObject

Returns the value of attribute eventMethod.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def eventMethod
  @eventMethod
end

#eventTypeObject

Returns the value of attribute eventType.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def eventType
  @eventType
end

#eventUrlObject

Returns the value of attribute eventUrl.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def eventUrl
  @eventUrl
end

#fromObject

Returns the value of attribute from.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def from
  @from
end

#limitObject

Returns the value of attribute limit.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def limit
  @limit
end

#machineDetectionObject

Returns the value of attribute machineDetection.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def machineDetection
  @machineDetection
end

#ringbackToneObject

Returns the value of attribute ringbackTone.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def ringbackTone
  @ringbackTone
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/vonage/voice/actions/connect.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#actionObject



135
136
137
# File 'lib/vonage/voice/actions/connect.rb', line 135

def action
  create_connect!(self)
end

#after_initialize!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vonage/voice/actions/connect.rb', line 24

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.advanced_machine_detection
    verify_advanced_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) ⇒ Object



190
191
192
193
194
195
# File 'lib/vonage/voice/actions/connect.rb', line 190

def app_endpoint(endpoint_attrs)
  {
    type: 'app',
    user: endpoint_attrs[:user]
  }
end

#create_connect!(builder) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vonage/voice/actions/connect.rb', line 139

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) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/vonage/voice/actions/connect.rb', line 161

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) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/vonage/voice/actions/connect.rb', line 178

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) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/vonage/voice/actions/connect.rb', line 209

def sip_endpoint(endpoint_attrs)
  hash = {
    type: 'sip',
    uri: endpoint_attrs[:uri]
  }

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

  hash
end

#vbc_endpoint(endpoint_attrs) ⇒ Object



221
222
223
224
225
226
# File 'lib/vonage/voice/actions/connect.rb', line 221

def vbc_endpoint(endpoint_attrs)
  {
    type: 'vbc',
    extension: endpoint_attrs[:extension]
  }
end

#verify_advanced_machine_detectionObject

Raises:



90
91
92
93
94
95
# File 'lib/vonage/voice/actions/connect.rb', line 90

def verify_advanced_machine_detection
  raise ClientError.new("Invalid 'advanced_machine_detection' value, must be a Hash") unless self.advanced_machine_detection.is_a?(Hash)
  verify_advanced_machine_detection_behavior if self.advanced_machine_detection[:behavior]
  verify_advanced_machine_detection_mode if self.advanced_machine_detection[:mode]
  verify_advanced_machine_detection_beep_timeout if self.advanced_machine_detection[:beep_timeout]
end

#verify_advanced_machine_detection_beep_timeoutObject

Raises:



105
106
107
# File 'lib/vonage/voice/actions/connect.rb', line 105

def verify_advanced_machine_detection_beep_timeout
  raise ClientError.new("Invalid 'advanced_machine_detection[:beep_timeout]' value, must be between 45 and 120") unless self.advanced_machine_detection[:beep_timeout].between?(45, 120)
end

#verify_advanced_machine_detection_behaviorObject

Raises:



97
98
99
# File 'lib/vonage/voice/actions/connect.rb', line 97

def verify_advanced_machine_detection_behavior
  raise ClientError.new("Invalid 'advanced_machine_detection[:behavior]' value, must be a `continue` or `hangup`") unless ['continue', 'hangup'].include?(self.advanced_machine_detection[:behavior])
end

#verify_advanced_machine_detection_modeObject

Raises:



101
102
103
# File 'lib/vonage/voice/actions/connect.rb', line 101

def verify_advanced_machine_detection_mode
  raise ClientError.new("Invalid 'advanced_machine_detection[:mode]' value, must be a `detect` or `detect_beep`") unless ['detect', 'detect_beep'].include?(self.advanced_machine_detection[:mode])
end

#verify_endpointObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vonage/voice/actions/connect.rb', line 60

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_methodObject

Raises:



121
122
123
124
125
# File 'lib/vonage/voice/actions/connect.rb', line 121

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_typeObject

Raises:



78
79
80
# File 'lib/vonage/voice/actions/connect.rb', line 78

def verify_event_type
  raise ClientError.new("Invalid 'eventType' value, must be 'synchronous' if defined") unless self.eventType == 'synchronous'
end

#verify_event_urlObject

Raises:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vonage/voice/actions/connect.rb', line 109

def verify_event_url
  unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
    raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
  end

  uri = URI.parse(self.eventUrl[0])

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

  self.eventUrl
end

#verify_fromObject

Raises:



74
75
76
# File 'lib/vonage/voice/actions/connect.rb', line 74

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_limitObject

Raises:



82
83
84
# File 'lib/vonage/voice/actions/connect.rb', line 82

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_detectionObject

Raises:



86
87
88
# File 'lib/vonage/voice/actions/connect.rb', line 86

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_toneObject

Raises:



127
128
129
130
131
132
133
# File 'lib/vonage/voice/actions/connect.rb', line 127

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) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/vonage/voice/actions/connect.rb', line 197

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