Class: Voice::Actions::Input

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Input

Returns a new instance of Input.



8
9
10
11
12
13
14
15
16
17
# File 'lib/vonage/voice/actions/input.rb', line 8

def initialize(attributes = {})
  @type = attributes.fetch(:type)
  @dtmf = attributes.fetch(:dtmf, nil)
  @speech = attributes.fetch(:speech, nil)
  @eventUrl = attributes.fetch(:eventUrl, nil)
  @eventMethod = attributes.fetch(:eventMethod, nil)
  @mode = attributes.fetch(:mode, nil)

  after_initialize!
end

Instance Attribute Details

#dtmfObject

Returns the value of attribute dtmf.



6
7
8
# File 'lib/vonage/voice/actions/input.rb', line 6

def dtmf
  @dtmf
end

#eventMethodObject

Returns the value of attribute eventMethod.



6
7
8
# File 'lib/vonage/voice/actions/input.rb', line 6

def eventMethod
  @eventMethod
end

#eventUrlObject

Returns the value of attribute eventUrl.



6
7
8
# File 'lib/vonage/voice/actions/input.rb', line 6

def eventUrl
  @eventUrl
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/vonage/voice/actions/input.rb', line 6

def mode
  @mode
end

#speechObject

Returns the value of attribute speech.



6
7
8
# File 'lib/vonage/voice/actions/input.rb', line 6

def speech
  @speech
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/vonage/voice/actions/input.rb', line 6

def type
  @type
end

Instance Method Details

#actionObject



114
115
116
# File 'lib/vonage/voice/actions/input.rb', line 114

def action
  create_input!(self)
end

#after_initialize!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vonage/voice/actions/input.rb', line 19

def after_initialize!
  validate_type

  if self.dtmf
    validate_dtmf
  end

  if self.speech
    validate_speech
  end

  if self.eventUrl
    validate_event_url
  end

  if self.eventMethod
    validate_event_method
  end

  if self.mode
    validate_mode
  end
end

#create_input!(builder) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vonage/voice/actions/input.rb', line 118

def create_input!(builder)
  ncco = [
    {
      action: 'input',
      type: builder.type
    }
  ]

  ncco[0].merge!(dtmf: builder.dtmf) if builder.dtmf
  ncco[0].merge!(speech: builder.speech) if builder.speech
  ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
  ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
  ncco[0].merge!(mode: builder.mode) if builder.mode

  ncco
end

#validate_dtmfObject

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vonage/voice/actions/input.rb', line 50

def validate_dtmf
  raise ClientError.new("Expected 'dtmf' to be included in 'type' parameter if 'dtmf' options specified") unless self.type.include?('dtmf')

  if self.dtmf[:timeOut]
    raise ClientError.new("Expected 'timeOut' to not be more than 10 seconds") if self.dtmf[:timeOut] > 10
  end

  if self.dtmf[:maxDigits]
    raise ClientError.new("Expected 'maxDigits' to not be more than 22") if self.dtmf[:maxDigits] > 22
  end

  if self.dtmf[:submitOnHash]
    raise ClientError.new("Invalid 'submitOnHash' value, must be a Boolean") unless self.dtmf[:submitOnHash] == true || self.dtmf[:submitOnHash] == false
  end
end

#validate_event_methodObject

Raises:



102
103
104
105
106
# File 'lib/vonage/voice/actions/input.rb', line 102

def validate_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

#validate_event_urlObject

Raises:



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vonage/voice/actions/input.rb', line 90

def validate_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

#validate_modeObject

Raises:



108
109
110
111
112
# File 'lib/vonage/voice/actions/input.rb', line 108

def validate_mode
  valid_modes = ['asyncronous']

  raise ClientError.new("Invalid 'mode' value, must be asyncronous'") unless valid_modes.include?(self.mode)
end

#validate_speechObject

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vonage/voice/actions/input.rb', line 66

def validate_speech
  raise ClientError.new("Expected 'speech' to be included in 'type' parameter if 'speech' options specified") unless self.type.include?('speech')

  if self.speech[:uuid]
    raise ClientError.new("Invalid 'uuid' value, must be an Array containing a single call leg ID element") unless self.speech[:uuid].is_a?(Array)
  end

  if self.speech[:endOnSilence]
    raise ClientError.new("Expected 'endOnSilence' to not be more than 10 seconds") unless self.speech[:endOnSilence] <= 10 && self.speech[:endOnSilence] >= 0
  end

  if self.speech[:context]
    raise ClientError.new("Expected 'context' to be an Array of strings") unless self.speech[:context].is_a?(Array)
  end

  if self.speech[:startTimeout]
    raise ClientError.new("Expected 'startTimeout' to not be more than 10 seconds") unless self.speech[:startTimeout] <= 10 && self.speech[:startTimeout] >= 0
  end

  if self.speech[:maxDuration]
    raise ClientError.new("Expected 'maxDuration' to not be more than 60 seconds") unless self.speech[:maxDuration] <= 60 && self.speech[:maxDuration] >= 0
  end
end

#validate_typeObject

Raises:



43
44
45
46
47
48
# File 'lib/vonage/voice/actions/input.rb', line 43

def validate_type
  valid_types = ['dtmf', 'speech']

  raise ClientError.new("Invalid 'type', must be an Array of at least one String") unless self.type.is_a?(Array)
  raise ClientError.new("Invalid 'type' value, must be 'dtmf', 'speech' or both 'dtmf' and 'speech'") if (valid_types & self.type).empty?
end