Skip to content

Commit

Permalink
add Tts.server_url(another_url) method to set anther tts server host.
Browse files Browse the repository at this point in the history
  • Loading branch information
qichunren committed Oct 31, 2011
1 parent 6a7e52e commit 13217cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
19 changes: 14 additions & 5 deletions lib/tts.rb
Expand Up @@ -5,13 +5,19 @@ def to_valid_fn fn
end


module Tts
module Tts
@@default_url = "http://translate.google.com/translate_tts"
def self.server_url url=nil
return @@default_url if url.nil?
@@default_url = url
end

def to_url lang
require 'uri'
langs = ["zh", "en", "it", "fr"]
#raise "Not accepted language, accpeted are #{langs * ","}" unless langs.include? lang
base = "http://translate.google.com/translate_tts?tl=#{lang}&q=#{URI.escape self}"
end
base = "#{Tts.server_url}?tl=#{lang}&q=#{URI.escape self}"
end

def to_file lang, file_name=nil
require 'open-uri'
Expand All @@ -20,7 +26,7 @@ def to_file lang, file_name=nil
ua = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24"
begin
content = open(url, "User-Agent" => ua).read

File.open(file_name, "wb") do |f|
f.puts content
end
Expand All @@ -30,8 +36,11 @@ def to_file lang, file_name=nil
end

end
end
end

class String



include Tts
end
37 changes: 26 additions & 11 deletions spec/tts_spec.rb
Expand Up @@ -3,43 +3,45 @@
require "rspec"

describe "to_valid_fn method" do
# Tts.server_url "http://127.0.0.1:3001/translate_tts"

# fn.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_')
it "should replace * with _" do
to_valid_fn("hello*nice").should == "hello_nice"
to_valid_fn("hello*nice*hello").should == "hello_nice_hello"
end

it "should replace / with _" do
to_valid_fn("hello/nice").should == "hello_nice"
to_valid_fn("hello/nice/hello").should == "hello_nice_hello"
end

it "should replace / with _" do
to_valid_fn("hello:nice").should == "hello_nice"
to_valid_fn("hello:nice:hello").should == "hello_nice_hello"
end

it "should replace / with _" do
to_valid_fn("hello?nice").should == "hello_nice"
to_valid_fn("hello?nice?hello").should == "hello_nice_hello"
end

it "should replace / with _" do
to_valid_fn("hello|nice").should == "hello_nice"
to_valid_fn("hello|nice?hello").should == "hello_nice_hello"
end
end

end

describe 'to_url method' do
it "to_url should return a correct string" do
"hello".to_url("en").should == "http://translate.google.com/translate_tts?tl=en&q=hello"
end

it "to_url should return a correct string with chinese char" do
"人民广场".to_url("zh").should == "http://translate.google.com/translate_tts?tl=zh&q=%E4%BA%BA%E6%B0%91%E5%B9%BF%E5%9C%BA"
end

end

describe 'to_file method' do
Expand All @@ -48,17 +50,30 @@
File.exist?("hello.mp3").should be_true
File.delete("hello.mp3")
end

it "to_file should generate a mp3 file with given name for a correct string" do
"hello".to_file("en", "my_hello.mp3")
File.exist?("my_hello.mp3").should be_true
File.delete("my_hello.mp3")
end

it "to_file should generate a mp3 file for a correct chinese string" do
"人民广场到了".to_file("zh")
File.exist?("人民广场到了.mp3").should be_true
File.delete("人民广场到了.mp3")
end


end

describe 'set a server url' do
before(:all) do
Tts.server_url "http://127.0.0.1:3001/translate_tts"
end

it "to_url should return a correct string" do
"hello".to_url("en").should == "http://127.0.0.1:3001/translate_tts?tl=en&q=hello"
end



end

0 comments on commit 13217cb

Please sign in to comment.