Module: Ahnsay

Defined in:
lib/ahnsay.rb,
lib/ahnsay/plugin.rb,
lib/ahnsay/version.rb,
lib/ahnsay/controller_methods.rb

Defined Under Namespace

Modules: ControllerMethods Classes: Plugin

Constant Summary collapse

FORMAT_TABLE =
{
  "h" => "hour_12h",
  "H" => "hour_24h",
  "m" => "minutes",
  "s" => "seconds",
  "d" => "day",
  "w" => "weekday",
  "M" => "month",
  "Y" => "year",
  "p" => "am_pm",
  "a" => "at"
}
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.file_for_day(day) ⇒ Object

Expects days from 0 to 6 where 0 is Sunday and 6 is Monday



143
144
145
# File 'lib/ahnsay.rb', line 143

def file_for_day(day)
  sound_path("day-#{day}.ul")
end

.file_for_month(month) ⇒ Object

Expects months from 1 to 12 where 1 is January. Files are actually numbered 0 to 11



151
152
153
# File 'lib/ahnsay.rb', line 151

def file_for_month(month)
  sound_path("mon-#{month.to_i - 1}.ul")
end

.multi_path(path) ⇒ Object

Massages the file path depending on platform



193
194
195
196
197
# File 'lib/ahnsay.rb', line 193

def multi_path(path)
  path = path.chomp(File.extname(path)) if Adhearsion.config.punchblock.platform == :asterisk
  path = "file://" + path if Adhearsion.config.punchblock.platform == :xmpp
  path
end

.parse_am_pm(time) ⇒ Object

Parses a “p” character Returns AM or PM sound files



128
129
130
# File 'lib/ahnsay.rb', line 128

def parse_am_pm(time)
  time.strftime("%P") == "am" ? [sound_path("a-m.ul")] : [sound_path("p-m.ul")]
end

.parse_at(time) ⇒ Object

Parses a “a” character Returns a “at” connection



136
137
138
# File 'lib/ahnsay.rb', line 136

def parse_at(time)
  [sound_path("at.ul")]
end

.parse_day(time) ⇒ Object

Parses a “d” character from the main methods Returns the numbers for the day.



95
96
97
# File 'lib/ahnsay.rb', line 95

def parse_day(time)
  sounds_for_number(time.strftime("%-d"))
end

.parse_hour_12h(time) ⇒ Object

Parses an “h” character from the main methods Returns the time in AM time.



62
63
64
# File 'lib/ahnsay.rb', line 62

def parse_hour_12h(time)
  sounds_for_number(time.strftime("%l"))
end

.parse_hour_24h(time) ⇒ Object

Parses an “H” character from the main methods Returns the time in 24 time.



70
71
72
# File 'lib/ahnsay.rb', line 70

def parse_hour_24h(time)
  sounds_for_number(time.strftime("%k"))
end

.parse_minutes(time) ⇒ Object

Parses a “m” character from the main methods Returns the numbers for the minutes



111
112
113
114
# File 'lib/ahnsay.rb', line 111

def parse_minutes(time)
  minutes = time.strftime("%M").to_i
  minutes == 0 ? [sound_path("oclock.ul")] : sounds_for_number(minutes)
end

.parse_month(time) ⇒ Object

Parses an “M” character from the main methods Returns the month in an array.



87
88
89
# File 'lib/ahnsay.rb', line 87

def parse_month(time)
  [file_for_month(time.strftime("%-m"))]
end

.parse_seconds(time) ⇒ Object

Parses a “s” character from the main methods Returns the numbers for the seconds



120
121
122
# File 'lib/ahnsay.rb', line 120

def parse_seconds(time)
  sounds_for_number(time.strftime("%S"))
end

.parse_weekday(time) ⇒ Object

Parses a “w” character from the main methods Returns the weekday sound.



103
104
105
# File 'lib/ahnsay.rb', line 103

def parse_weekday(time)
  [file_for_day(time.strftime("%w"))]
end

.parse_year(time) ⇒ Object

Parses a “Y” character from the main methods Returns the numbers for the year in 4 digit format



78
79
80
81
# File 'lib/ahnsay.rb', line 78

def parse_year(time)
  year = time.strftime("%Y").to_i
  sounds_for_number(year / 100) + sounds_for_number(year % 100)
end

.sound_path(name) ⇒ Object

Gets the path for a sound file based on configuration and platform



186
187
188
# File 'lib/ahnsay.rb', line 186

def sound_path(name)
  multi_path(File.join(Adhearsion.config.ahnsay.sounds_dir, name))
end

.sounds_for_digits(num) ⇒ Object

Returns the sounds for the single digits composing a number



50
51
52
53
54
55
56
# File 'lib/ahnsay.rb', line 50

def sounds_for_digits(num)
  result = []
  num.to_s.each_char do |c|
    result << sound_path("#{c}.ul")
  end
  result
end

.sounds_for_number(number) ⇒ Object

Breaks a number down into components. Supports numbers up to 9999



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ahnsay.rb', line 159

def sounds_for_number(number)
  number = number.to_i
  result = []
  thousands = (number / 1000).floor
  if thousands > 0
    result << "#{thousands}.ul" << "thousand.ul"
  end
  rest = number % 1000
  hundreds = (rest / 100).floor
  if hundreds > 0
    result << "#{hundreds}.ul" << "hundred.ul"
  end
  rest = rest % 100
  if rest < 19
    result << "#{rest}.ul"
  else
    tens = (rest / 10).floor
    units = rest % 10
    result << "#{tens}0.ul" 
    result << "#{units}.ul" if units > 0
  end
  result.map {|r| sound_path(r) }
end

.sounds_for_time(time, args = {}) ⇒ Object

Main entry point Requires a Time object and a parameter Hash, currently :format Returns an array of sound files with the requested output Example: sound_files_for_time(Time.now, format: “dMYaHm”

format:

h: 12h hour
H: 24h hour
m: minutes
s: seconds
d: day in number
w: weekday name
M: month name
Y: year as "twenty twelve"
p: AM or PM indicator
a: the "at" word


38
39
40
41
42
43
44
45
# File 'lib/ahnsay.rb', line 38

def sounds_for_time(time, args={})
  format = args.delete(:format) || 'dMYaHm'
  result = []
  format.each_char do |c|
    result += send("parse_#{FORMAT_TABLE[c]}".to_sym, time)
  end
  result
end