class Adhearsion::CallController::Output::Formatter

Public Instance Methods

detect_type(output) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 28
def detect_type(output)
  case output
  when Date, Time, DateTime
    :time
  when Numeric, /^\d+$/
    :numeric
  when /^[\d\*\#]+$/
    :characters
  when /^\//, ->(string) { uri? string }
    :audio
  else
    :text
  end
end
ssml_for(*args) click to toggle source

Generates SSML for the argument and options passed, using automatic detection Directly returns the argument if it is already an SSML document

@param [String, Hash, RubySpeech::SSML::Speak] args the argument with options as accepted by the play_ methods, or an SSML document @return [RubySpeech::SSML::Speak] an SSML document

# File lib/adhearsion/call_controller/output/formatter.rb, line 56
def ssml_for(*args)
  return args[0] if args.size == 1 && args[0].is_a?(RubySpeech::SSML::Speak)
  argument, options = args.flatten
  options ||= {}
  type = detect_type argument
  send "ssml_for_#{type}", argument, options
end
ssml_for_audio(argument, options = {}) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 90
def ssml_for_audio(argument, options = {})
  fallback = (options || {}).delete :fallback
  RubySpeech::SSML.draw do
    audio(:src => argument) { fallback }
  end
end
ssml_for_characters(argument, options = {}) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 97
def ssml_for_characters(argument, options = {})
  RubySpeech::SSML.draw do
    say_as(interpret_as: 'characters') { string argument.to_s }
  end
end
ssml_for_collection(collection) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 10
def ssml_for_collection(collection)
  collection = collection.compact
  raise NoDocError if collection.empty?

  collection.inject RubySpeech::SSML.draw do |doc, argument|
    doc + case argument
    when Hash
      ssml_for argument.delete(:value), argument
    when RubySpeech::SSML::Speak
      argument
    when lambda { |a| a.respond_to? :each }
      ssml_for_collection argument
    else
      ssml_for argument
    end
  end
end
ssml_for_numeric(argument, options = {}) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 84
def ssml_for_numeric(argument, options = {})
  RubySpeech::SSML.draw do
    say_as(:interpret_as => 'cardinal') { argument.to_s }
  end
end
ssml_for_text(argument, options = {}) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 64
def ssml_for_text(argument, options = {})
  RubySpeech::SSML.draw { argument }
end
ssml_for_time(argument, options = {}) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 68
def ssml_for_time(argument, options = {})
  interpretation = case argument
  when Date then 'date'
  when Time then 'time'
  end

  format = options.delete :format
  strftime = options.delete :strftime

  time_to_say = strftime ? argument.strftime(strftime) : argument.to_s

  RubySpeech::SSML.draw do
    say_as(:interpret_as => interpretation, :format => format) { time_to_say }
  end
end
uri?(string) click to toggle source
# File lib/adhearsion/call_controller/output/formatter.rb, line 43
def uri?(string)
  !! URI.parse(string).scheme
rescue URI::BadURIError, URI::InvalidURIError
  false
end