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



137
138
139
# File 'lib/vonage/voice/actions/connect.rb', line 137

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



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

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

#create_connect!(builder) ⇒ Object



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

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



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

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



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

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



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/vonage/voice/actions/connect.rb', line 211

def sip_endpoint(endpoint_attrs)
  hash = {
    type: 'sip'
  }

  hash.merge!(uri: endpoint_attrs[:uri]) if endpoint_attrs[:uri]
  hash.merge!(user: endpoint_attrs[:user]) if endpoint_attrs[:user]
  hash.merge!(domain: endpoint_attrs[:domain]) if endpoint_attrs[:domain]
  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



225
226
227
228
229
230
# File 'lib/vonage/voice/actions/connect.rb', line 225

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

#verify_advanced_machine_detectionObject

Raises:



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

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:



107
108
109
# File 'lib/vonage/voice/actions/connect.rb', line 107

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:



99
100
101
# File 'lib/vonage/voice/actions/connect.rb', line 99

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:



103
104
105
# File 'lib/vonage/voice/actions/connect.rb', line 103

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
73
74
# 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) if endpoint[:uri]
    raise ClientError.new("`uri` must not be combined with `user` and `domain`") if endpoint[:uri] && (endpoint[:user] || endpoint[:domain])
    raise ClientError.new("You must provide both `user` and `domain`") if (endpoint[:user] && !endpoint[:domain]) || (endpoint[:domain] && !endpoint[:user])
  end
end

#verify_event_methodObject

Raises:



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

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:



80
81
82
# File 'lib/vonage/voice/actions/connect.rb', line 80

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

#verify_event_urlObject

Raises:



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

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:



76
77
78
# File 'lib/vonage/voice/actions/connect.rb', line 76

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:



84
85
86
# File 'lib/vonage/voice/actions/connect.rb', line 84

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:



88
89
90
# File 'lib/vonage/voice/actions/connect.rb', line 88

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:



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

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



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

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