Sha256: d908f126ae32b6d70ae8c2413ed3cc49d70870f1699ba28cc2bb729a49248b28

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

# encoding: utf-8

require 'ruby_speech'

module Adhearsion
  class CallController
    module Utility

      # Utility method for DTMF GRXML grammars
      #
      # @param [Integer] digits Number of digits to accept in the grammar.
      # @return [RubySpeech::GRXML::Grammar] A grammar suitable for use in SSML prompts
      #
      # @private
      #
      def grammar_digits(digits = 1)
        RubySpeech::GRXML.draw :mode => 'dtmf', :root => 'inputdigits' do
          rule id: 'inputdigits', scope: 'public' do
            item repeat: digits.to_s do
              one_of do
                0.upto(9) { |d| item { d.to_s } }
              end
            end
          end
        end
      end

      # Utility method to create a single-digit grammar to accept only some digits
      #
      # @param [String] digits String representing the digits to accept
      # @return [RubySpeech::GRXML::Grammar] A grammar suitable for use in SSML prompts
      #
      # @private
      #
      def grammar_accept(digits = '0123456789#*')
        allowed_digits = '0123456789#*'
        gram_digits = digits.chars.select { |x| allowed_digits.include? x }

        RubySpeech::GRXML.draw :mode => 'dtmf', :root => 'inputdigits' do
          rule id: 'inputdigits', scope: 'public' do
            one_of do
              gram_digits.each { |d| item { d.to_s } }
            end
          end
        end
      end

      #
      # Parses a DTMF tone string
      #
      # @param [String] dtmf the tone string to be parsed
      # @return [String] the digits/*/# without any separation
      #
      # @private
      #
      def parse_dtmf(dtmf)
        return if dtmf.nil?
        dtmf.split(' ').inject '' do |final, digit|
          final << parse_dtmf_digit(digit)
        end
      end

      # @private
      def parse_dtmf_digit(digit)
        case tone = digit.split('-').last
        when 'star'
          '*'
        when 'pound'
          '#'
        else
          tone
        end
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
adhearsion-2.6.4 lib/adhearsion/call_controller/utility.rb
adhearsion-2.6.3 lib/adhearsion/call_controller/utility.rb
adhearsion-2.6.2 lib/adhearsion/call_controller/utility.rb
adhearsion-2.6.1 lib/adhearsion/call_controller/utility.rb
adhearsion-2.6.0 lib/adhearsion/call_controller/utility.rb
adhearsion-2.5.4 lib/adhearsion/call_controller/utility.rb
adhearsion-2.5.3 lib/adhearsion/call_controller/utility.rb
adhearsion-2.5.2 lib/adhearsion/call_controller/utility.rb
adhearsion-2.5.0 lib/adhearsion/call_controller/utility.rb