Class: Adhearsion::CallController::Output::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/call_controller/output/formatter.rb

Instance Method Summary collapse

Instance Method Details

#detect_type(output) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ RubySpeech::SSML::Speak

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



56
57
58
59
60
61
62
# 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 = {}) ⇒ Object



90
91
92
93
94
95
# 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 = {}) ⇒ Object



97
98
99
100
101
# 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) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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 = {}) ⇒ Object



84
85
86
87
88
# 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 = {}) ⇒ Object



64
65
66
# 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 = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Boolean



43
44
45
46
47
# File 'lib/adhearsion/call_controller/output/formatter.rb', line 43

def uri?(string)
  !! URI.parse(string).scheme
rescue URI::BadURIError, URI::InvalidURIError
  false
end