class ZendeskPolly::Greeting

Constants

AUDIO_CONTENT_TYPE
CATEGORIES
OUTPUT_FORMAT

Attributes

category[RW]
client[R]
mp3_file[RW]
name[RW]
text[RW]
voice[RW]

Public Class Methods

new(client, options = {}) click to toggle source
# File lib/zendesk_polly/greeting.rb, line 26
def initialize(client, options = {})
  @client     = client
  @text       = options[:text]
  raise ArgumentError.new("text is required") unless text
  
  @voice      = options[:voice] || 'Joanna'
  @name       = options[:name] || encoded_text
  @category   = options[:category] || :ivr
  @category   = category.to_sym
  @mp3_file = "/tmp/#{encoded_text}.mp3" if options[:text]
  synthesize_speech
end

Public Instance Methods

encoded_text() click to toggle source
# File lib/zendesk_polly/greeting.rb, line 39
def encoded_text
  @et ||= Digest::MD5.hexdigest(text)
end
play() click to toggle source
# File lib/zendesk_polly/greeting.rb, line 43
def play
  Launchy.open("file://#{mp3_file}")
end
save() click to toggle source
# File lib/zendesk_polly/greeting.rb, line 47
def save
  placeholder = client.zendesk.greetings.create!(name: name, category_id: CATEGORIES[category])
  upload(placeholder.id)
end
synthesize_speech() click to toggle source
# File lib/zendesk_polly/greeting.rb, line 52
def synthesize_speech
  client.polly.synthesize_speech({
    voice_id: voice,
    text: text,
    output_format: OUTPUT_FORMAT,
    response_target: mp3_file
  })
end
upload(greeting_id) click to toggle source
# File lib/zendesk_polly/greeting.rb, line 61
def upload(greeting_id)
  raise ArgumentError.new("voice was not generated") unless mp3_file
  client.zendesk.greetings.update!(
    id: greeting_id,
    upload_attributes: { uploaded_data: Faraday::UploadIO.new(mp3_file, AUDIO_CONTENT_TYPE) }
  )
end