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
# 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)

  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

#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



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

def action
  create_input!(self)
end

#after_initialize!Object



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

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
end

#create_input!(builder) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vonage/voice/actions/input.rb', line 103

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
end

#validate_dtmfObject

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vonage/voice/actions/input.rb', line 45

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:



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

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:



85
86
87
88
89
90
91
# File 'lib/vonage/voice/actions/input.rb', line 85

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

#validate_speechObject

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vonage/voice/actions/input.rb', line 61

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:



38
39
40
41
42
43
# File 'lib/vonage/voice/actions/input.rb', line 38

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