Skip to content

Instantly share code, notes, and snippets.

@mbklein
Created January 19, 2011 01:18
Show Gist options
  • Save mbklein/785504 to your computer and use it in GitHub Desktop.
Save mbklein/785504 to your computer and use it in GitHub Desktop.
Sinatra web service for OS X to speak (via the "say" command) whatever is posted
#!/usr/bin/env ruby
require 'rubygems'
require 'haml'
require 'sinatra'
require 'rack/conneg'
use(Rack::Conneg) { |conneg|
conneg.set :accept_all_extensions, false
conneg.set :fallback, :html
conneg.provide([:html,:json,:xml])
}
before do
if negotiated?
content_type negotiated_type
end
@voice = params[:voice] || 'Victoria'
@voices = {
'Female' => ['Agnes', 'Kathy', 'Princess', 'Vicki', 'Victoria'],
'Male' => ['Alex', 'Bruce', 'Fred', 'Junior', 'Ralph'],
'Novelty' => ['Albert', 'Bad News', 'Bahh', 'Bells', 'Boing', 'Bubbles', 'Cellos',
'Deranged', 'Good News', 'Hysterical', 'Pipe Organ', 'Trinoids',
'Whisper', 'Zarvox']
}
end
get '/say' do
respond_to do |wants|
wants.html { haml :form }
wants.other { error 400, "Bad Request" }
end
end
post '/say' do
@text = params[:content]
if @voices.values.flatten.include?(@voice) and not @text.nil?
IO.popen("say -v '#{@voice}'", 'w') { |say|
say.write(@text)
}
end
respond_to do |wants|
wants.html { haml :form }
wants.json { "{ 'status' => 200, 'message' => 'OK' }" }
wants.xml { "<response code='200'>OK</response>" }
end
end
__END__
@@ form
%html
%head
%title Say It!
%body
%form{:method=>'POST'}
%textarea#content{:name => 'content', :cols => '40', :rows => '20'}= @text
%br
%select#voice{:name => 'voice', :size => 1}
- @voices.keys.sort.each do |category|
%optgroup{:label => category}
- @voices[category].each do |voice|
%option{:value => voice, :selected => (@voice == voice)}= voice
%button{:type => 'submit'} Say It!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment