Sha256: 9e0b4a84174034ce78e78899936af7ae5cace96842739b902bb0dc517e9d5a9c

Contents?: true

Size: 1.95 KB

Versions: 11

Compression:

Stored size: 1.95 KB

Contents

# encoding: utf-8

require 'ruby_speech'

module Adhearsion
  class CallController
    module Utility

      # Utility method for DTMF GRXML grammars
      #
      # @param [Integer] 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] 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] 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

11 entries across 11 versions & 1 rubygems

Version Path
adhearsion-2.4.0 lib/adhearsion/call_controller/utility.rb
adhearsion-2.4.0.beta3 lib/adhearsion/call_controller/utility.rb
adhearsion-2.4.0.beta2 lib/adhearsion/call_controller/utility.rb
adhearsion-2.4.0.beta1 lib/adhearsion/call_controller/utility.rb
adhearsion-2.3.5 lib/adhearsion/call_controller/utility.rb
adhearsion-2.3.4 lib/adhearsion/call_controller/utility.rb
adhearsion-2.3.3 lib/adhearsion/call_controller/utility.rb
adhearsion-2.3.2 lib/adhearsion/call_controller/utility.rb
adhearsion-2.3.1 lib/adhearsion/call_controller/utility.rb
adhearsion-2.3.0 lib/adhearsion/call_controller/utility.rb
adhearsion-2.2.1 lib/adhearsion/call_controller/utility.rb