class Adhearsion::IVRController

Attributes

completion_callback[R]
failure_callback[R]
validate_callback[R]

Public Class Methods

max_attempts(num = nil) click to toggle source

maximum number of attempts to prompt the caller for input

# File lib/adhearsion-ivr/ivr_controller.rb, line 18
def max_attempts(num = nil)
  if num
    @max_attempts = num
  else
    @max_attempts || 3
  end
end
on_complete(&block) click to toggle source

called when the caller successfully provides input

# File lib/adhearsion-ivr/ivr_controller.rb, line 51
def on_complete(&block)
  @completion_callback = block
end
on_failure(&block) click to toggle source

Called when the caller errors more than the number of allowed attempts

# File lib/adhearsion-ivr/ivr_controller.rb, line 57
def on_failure(&block)
  @failure_callback = block
end
prompts() click to toggle source

list of prompts to be played to the caller. this should have one prompt for each attempt in case there are not enough prompts, the final prompt will be re-used until the max_attempts are exceeded.

# File lib/adhearsion-ivr/ivr_controller.rb, line 13
def prompts
  @prompts ||= []
end
renderer(engine = nil) click to toggle source

renderer to use for the prompts

# File lib/adhearsion-ivr/ivr_controller.rb, line 36
def renderer(engine = nil)
  if engine
    @renderer = engine
  else
    @renderer || nil
  end
end
timeout(num = nil) click to toggle source

timeout in seconds for each menu attempt

# File lib/adhearsion-ivr/ivr_controller.rb, line 27
def timeout(num = nil)
  if num
    @timeout = num
  else
    @timeout || nil
  end
end
validate_input(&block) click to toggle source

called to verify matched input is valid - should be truthy for valid input, falsey otherwise.

# File lib/adhearsion-ivr/ivr_controller.rb, line 45
def validate_input(&block)
  @validate_callback = block
end

Public Instance Methods

completion_callback() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 183
def completion_callback
  instance_exec @result, &self.class.completion_callback if self.class.completion_callback
end
continue?() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 171
def continue?
  @errors < max_attempts
end
deliver_prompt() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 107
def deliver_prompt
  prompt = prompts[@errors] || prompts.last
  prompt = instance_exec(&prompt) if prompt.respond_to? :call
  logger.debug "Prompt: #{prompt.inspect}"

  if grammar
    ask_options = { grammar: grammar, mode: :voice }
  elsif grammar_url
    ask_options = { grammar_url: grammar_url, mode: :voice }
  else
    fail NotImplementedError, 'You must override #grammar or #grammar_url and provide an input grammar'
  end

  ask_options[:timeout] = timeout if timeout
  ask_options[:output_options] = { renderer: renderer } if renderer

  @result = ask prompt, ask_options
  logger.debug "Got result #{@result.inspect}"
  case @result.status
  when :match
    match!
  when :stop
    logger.info "Prompt was stopped forcibly. Exiting cleanly..."
  when :hangup
    logger.info "Call was hung up mid-prompt. Exiting controller flow..."
    fail Adhearsion::Call::Hangup
  when :nomatch
    nomatch!
  when :noinput
    noinput!
  else
    fail "Unrecognized result status: #{@result.status}"
  end
  @result
end
failure_callback() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 187
def failure_callback
  instance_exec &self.class.failure_callback if self.class.failure_callback
end
grammar() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 143
def grammar
  nil
end
grammar_url() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 147
def grammar_url
  nil
end
increment_errors() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 167
def increment_errors
  @errors += 1
end
max_attempts() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 155
def max_attempts
  self.class.max_attempts
end
prompts() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 151
def prompts
  self.class.prompts
end
renderer() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 163
def renderer
  self.class.renderer
end
run() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 102
def run
  @errors = 0
  deliver_prompt
end
timeout() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 159
def timeout
  self.class.timeout
end
validate_callback() click to toggle source
# File lib/adhearsion-ivr/ivr_controller.rb, line 175
def validate_callback
  if self.class.validate_callback
    instance_exec &self.class.validate_callback
  else
    true
  end
end