ispeech 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ispeech::Config, 'When created' do
4
+
5
+ default_config = YAML.load_file('config/ispeech.yml')
6
+ default_config['api_key'].should_not == nil
7
+
8
+ context 'with a given api_key' do
9
+ it 'should report those and other default values' do
10
+ config = Ispeech::Config.new(default_config['api_key'])
11
+ config.target_url.should == URI.parse(Ispeech::Config::DEFAULT_TARGET_URL)
12
+ config.api_key.should == default_config['api_key']
13
+ end
14
+ end
15
+
16
+ context 'from a config file' do
17
+ it 'should default to the local config/ispeech.yml' do
18
+ config = Ispeech::Config.read
19
+ config.api_key.should == default_config['api_key']
20
+ end
21
+
22
+ it 'should accept an arbitary config file argument' do
23
+ config = Ispeech::Config.read(test_config)
24
+ config.api_key.should_not be_nil
25
+ config.api_key.should_not == default_config['api_key']
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'ispeech/mock'
3
+
4
+ describe Ispeech::Mock do
5
+
6
+ before(:each) do
7
+ Ispeech::Mock.enable!
8
+ end
9
+
10
+ after(:each) do
11
+ Ispeech::VoiceService.expect_ok_response
12
+ Ispeech::Mock.disable!
13
+ end
14
+
15
+ context 'When mock is on' do
16
+ text_string = 'This is a test'
17
+ service = Ispeech::VoiceService.new
18
+
19
+ it 'it should default to giving an working response without net access' do
20
+ response = service.generate_sound(text_string)
21
+
22
+ file = response.download_to_tempfile
23
+ file.should be_kind_of(Tempfile)
24
+ file.rewind
25
+ contents = file.read
26
+ file.close
27
+ contents.should == File.read(RESPONSE_TEST_FILE)
28
+
29
+ WebMock.should_not have_requested(:any, service.config.target_url.to_s)
30
+ end
31
+
32
+ it 'it should give an error response when that expectation has been set' do
33
+ Ispeech::VoiceService.expect_error_response
34
+
35
+ expect do
36
+ service.generate_sound(text_string)
37
+ end.to raise_error(Ispeech::Mock::FAKE_ERROR)
38
+
39
+ WebMock.should_not have_requested(:any, service.config.target_url.to_s)
40
+ end
41
+
42
+ it 'once set to an error and set back to ok should not raise an error' do
43
+ Ispeech::VoiceService.expect_error_response
44
+ Ispeech::VoiceService.expect_ok_response
45
+
46
+ expect do
47
+ service.generate_sound(text_string)
48
+ end.to_not raise_error
49
+
50
+ WebMock.should_not have_requested(:any, service.config.target_url.to_s)
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'cgi'
3
+
4
+ describe Ispeech::Response do
5
+
6
+ it 'should throw an error when created with bogus parameters' do
7
+ expect do
8
+ Ispeech::Response.new('take this!')
9
+ end.to raise_error(Ispeech::Error, Ispeech::Response::ERROR_UNEXPECTED_RESPONSE.message)
10
+ end
11
+
12
+ it 'should download and return a tempfile with the object contained in the response' do
13
+ service_url = URI.parse('http://somewhere.com/api')
14
+ stub_ok_response_for_url(service_url)
15
+ service_response = Net::HTTP.get_response(service_url)
16
+
17
+ response = Ispeech::Response.new(service_response)
18
+
19
+ file = response.download_to_tempfile
20
+ file.should be_kind_of(Tempfile)
21
+ file.rewind
22
+ contents = file.read
23
+ file.close
24
+ contents.should == File.read(RESPONSE_TEST_FILE)
25
+ end
26
+
27
+ it 'should raise an error when file download fails with non 200 response' do
28
+ expect do
29
+ Ispeech::Response.new(Net::HTTPNotFound.new("Body?", 404, "Something went wrong."))
30
+ end.to raise_error(Ispeech::Error)
31
+ end
32
+
33
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ispeech::VoiceService do
4
+
5
+ text_string = 'This is a test'
6
+
7
+ context 'When create without parameters' do
8
+ service = Ispeech::VoiceService.new
9
+
10
+ it 'should use the default config' do
11
+ service.config.should be_kind_of(Ispeech::Config)
12
+ service.config.should == Ispeech.config
13
+ end
14
+
15
+ it 'should allow generation of MP3 sound' do
16
+ if USE_MOCKS
17
+ stub_ok_response(service)
18
+ end
19
+
20
+ sound_url = service.generate_sound(text_string, :language => :en)
21
+ sound_url.should be_kind_of(Ispeech::Response)
22
+ end
23
+ end
24
+
25
+ it 'should throw an error when created with nil config' do
26
+ expect do
27
+ service = Ispeech::VoiceService.new(nil)
28
+ end.to raise_error(Ispeech::Error, Ispeech::VoiceService::ERROR_MISSING_CONFIG.message)
29
+ end
30
+
31
+ context 'When created with config with invalid api_key' do
32
+ service = Ispeech::VoiceService.new(Ispeech::Config.read(test_config))
33
+
34
+ it 'should raise the reported access error' do
35
+ if USE_MOCKS
36
+ stub_invalid_access_response(service)
37
+ end
38
+
39
+ expect do
40
+ service.generate_sound(text_string, :language => :en)
41
+ end.to raise_error(Ispeech::ServiceError, /Code: 1, Message: Invalid API key/)
42
+ end
43
+ end
44
+
45
+ context 'When generating sound' do
46
+ service = Ispeech::VoiceService.new
47
+
48
+ it 'should allow low quality setting' do
49
+ if USE_MOCKS
50
+ stub_ok_response(service).with(:body => hash_including(:voice => DEFAULT_ENGLISH_FEMALE_TEST_VOICES.first))
51
+ service.generate_sound(text_string, :speaker => DEFAULT_ENGLISH_FEMALE_TEST_VOICES.first, :quality => :low)
52
+ else
53
+ pending("Without mock, the returned file quality needs to be confirmed")
54
+ end
55
+ end
56
+
57
+ it 'should allow custom voice setting' do
58
+ if USE_MOCKS
59
+ stub_ok_response(service).with(:body => hash_including(:voice => 'johnny'))
60
+ voice = Ispeech::Voice.new('johnny', :male, 'en')
61
+ service.generate_with_voice(text_string, voice)
62
+ else
63
+ pending("Without mock, the returned file quality needs to be confirmed")
64
+ end
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ispeech::Voice do
4
+
5
+ context 'When created' do
6
+ speaker_name = 'tom'
7
+ test_voice = Ispeech::Voice.new(speaker_name, Ispeech::Voice::GENDER_FEMALE)
8
+
9
+ it 'with name and gender should report default values' do
10
+ test_voice.speaker.should == speaker_name
11
+ test_voice.gender.should == Ispeech::Voice::GENDER_FEMALE
12
+ test_voice.quality.should == Ispeech::Voice::QUALITY_HIGH
13
+ test_voice.languages.should be_kind_of(Set)
14
+ test_voice.languages.should be_empty
15
+ test_voice.language.should be_nil
16
+ test_voice.id.should == "#{speaker_name}"
17
+ end
18
+
19
+ it 'should allow flipping quality setting' do
20
+ test_voice.low_quality!
21
+ test_voice.quality.should == Ispeech::Voice::QUALITY_LOW
22
+ test_voice.high_quality!
23
+ test_voice.quality.should == Ispeech::Voice::QUALITY_HIGH
24
+ end
25
+ end
26
+
27
+ context 'With a set of defined voices' do
28
+ it 'should throw an error if the voice does not exist' do
29
+ expect do
30
+ Ispeech::Voice.named_voice('Birkir')
31
+ end.to raise_error(Ispeech::Error, "Voice does not exist.")
32
+ end
33
+
34
+ it 'should return the active voice map' do
35
+ Ispeech::Voice.map.should == Ispeech::Voices::PER_LANGUAGE
36
+ end
37
+
38
+ context 'should find a voice' do
39
+ it 'given an existing speaker name' do
40
+ voice = Ispeech::Voice.named_voice(DEFAULT_ENGLISH_FEMALE_TEST_VOICES.first)
41
+ voice.speaker.should == DEFAULT_ENGLISH_FEMALE_TEST_VOICES.first
42
+ voice.language.should == 'en'
43
+ voice.gender.should == Ispeech::Voice::GENDER_FEMALE
44
+ voice.languages.to_a.should == ['en']
45
+ voice.id.should == DEFAULT_ENGLISH_FEMALE_TEST_VOICES.first
46
+ end
47
+
48
+ it 'given an existing speaker name in lower case' do
49
+ voice = Ispeech::Voice.named_voice('bruno')
50
+ voice.speaker.should == 'Bruno'
51
+ voice.gender.should == Ispeech::Voice::GENDER_MALE
52
+ voice.language.should == 'fr'
53
+ end
54
+
55
+ it 'given an existing speaker name as a symbol' do
56
+ voice = Ispeech::Voice.named_voice(:rosa)
57
+ voice.speaker.should == 'Rosa'
58
+ voice.gender.should == Ispeech::Voice::GENDER_FEMALE
59
+ voice.language.should == 'es'
60
+ end
61
+ end
62
+
63
+ context 'should extract a voice when given an option that' do
64
+ it 'specifies nothing, defaulting to English' do
65
+ voice = Ispeech::Voice.extract_from_options
66
+ (DEFAULT_ENGLISH_FEMALE_TEST_VOICES + DEFAULT_ENGLISH_MALE_TEST_VOICES).should include(voice.speaker)
67
+ end
68
+
69
+ it 'specifies a speaker' do
70
+ voice = Ispeech::Voice.extract_from_options(:speaker => 'antoine')
71
+ voice.speaker.should == 'Antoine'
72
+ voice.gender.should == Ispeech::Voice::GENDER_MALE
73
+ voice.language.should == 'fr'
74
+ end
75
+
76
+ it 'specifies a language' do
77
+ voice = Ispeech::Voice.extract_from_options(:language => :es)
78
+ voice.speaker.should == 'Rosa'
79
+ end
80
+
81
+ it 'specifies a language and no gender' do
82
+ voice = Ispeech::Voice.extract_from_options(:language => :fr)
83
+ voice.should_not be_nil
84
+ ['Antoine', 'Bruno'].should include(voice.speaker)
85
+ end
86
+
87
+ it 'specifies a language and gender' do
88
+ voice = Ispeech::Voice.extract_from_options(:language => :es, :gender => Ispeech::Voice::GENDER_FEMALE)
89
+ voice.speaker.should == 'Rosa'
90
+
91
+ voice = Ispeech::Voice.extract_from_options(:language => :es, :gender => Ispeech::Voice::GENDER_MALE)
92
+ voice.should be_nil
93
+ end
94
+ end
95
+ end
96
+
97
+ context 'Allows for voices to be overriden or changed' do
98
+ after(:each) do
99
+ Ispeech::Voice.reset_map
100
+ end
101
+
102
+ it 'should allow removing specific entries' do
103
+ Ispeech::Voice.map[:en][:male].first.should == DEFAULT_ENGLISH_MALE_TEST_VOICES.first
104
+ Ispeech::Voice.override_map(:en => nil)
105
+ Ispeech::Voice.map[:en].should be_nil
106
+ end
107
+
108
+ it 'should allow mapping one entry to another' do
109
+ Ispeech::Voice.map[:en][:male].first.should == DEFAULT_ENGLISH_MALE_TEST_VOICES.first
110
+ Ispeech::Voice.override_map(:en => :fr)
111
+ Ispeech::Voice.map[:en][:male].first.should == 'Antoine'
112
+ end
113
+
114
+ it 'should allow replacing an entry' do
115
+ Ispeech::Voice.map[:en][:male].first.should == DEFAULT_ENGLISH_MALE_TEST_VOICES.first
116
+ Ispeech::Voice.override_map(:en => {:male => ['Johnny']})
117
+ Ispeech::Voice.map[:en][:male].should == ['Johnny']
118
+ Ispeech::Voice.map[:en][:female].first.should == DEFAULT_ENGLISH_FEMALE_TEST_VOICES.first
119
+ end
120
+
121
+ it 'should allow adding an entry' do
122
+ Ispeech::Voice.map[:is].should be_nil
123
+ Ispeech::Voice.override_map(:is => {:male => ['Birkir'], :female => ['Harpa']})
124
+ Ispeech::Voice.map[:is][:male].first.should == 'Birkir'
125
+ Ispeech::Voice.map[:is][:female].first.should == 'Harpa'
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ispeech do
4
+ it 'should reference a config file' do
5
+ config = Ispeech.config
6
+
7
+ config.should_not be_nil
8
+ config.should be_a_kind_of(Ispeech::Config)
9
+ end
10
+
11
+ it 'should allow setting of the config file' do
12
+ default_config = Ispeech.config
13
+
14
+ begin
15
+ test_config = Ispeech::Config.read(test_config)
16
+
17
+ Ispeech.config = test_config
18
+ Ispeech.config.should == test_config
19
+ ensure
20
+ Ispeech.config = default_config
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,69 @@
1
+ RESPONSE_TEST_FILE = File.join('spec', 'test_data', 'test_file.mp3')
2
+ DEFAULT_ENGLISH_FEMALE_TEST_VOICES = ["usenglishfemale", "auenglishfemale"]
3
+ DEFAULT_ENGLISH_MALE_TEST_VOICES = ["usenglishmale"]
4
+ USE_MOCKS = true
5
+ CONFIG_DIR = 'config'
6
+ CONFIG_FILE = File.join(CONFIG_DIR, 'ispeech.yml')
7
+ DEFAULT_CONFIG_FILE = <<-YAML
8
+ api_key: 'developerdemokeydeveloperdemokey'
9
+ YAML
10
+
11
+ def create_missing_config_file
12
+ unless File.exists?(CONFIG_FILE)
13
+ Dir.mkdir(CONFIG_DIR)
14
+ File.open(CONFIG_FILE, 'w') do |f|
15
+ f.write(DEFAULT_CONFIG_FILE)
16
+ end
17
+ end
18
+ end
19
+
20
+ create_missing_config_file
21
+
22
+ require File.join(File.dirname(__FILE__), '..', 'init')
23
+ require 'bundler'
24
+ Bundler.require(:development)
25
+ require 'webmock/rspec'
26
+
27
+ if USE_MOCKS
28
+ WebMock.disable_net_connect!
29
+ else
30
+ WebMock.allow_net_connect!
31
+ end
32
+
33
+ def silence_warnings
34
+ begin
35
+ old_verbose, $VERBOSE = $VERBOSE, nil
36
+ yield
37
+ ensure
38
+ $VERBOSE = old_verbose
39
+ end
40
+ end
41
+
42
+ # Some test voices.
43
+ silence_warnings do
44
+ Ispeech::Voices::PER_LANGUAGE = {
45
+ :en => {:female => DEFAULT_ENGLISH_FEMALE_TEST_VOICES, :male => DEFAULT_ENGLISH_MALE_TEST_VOICES},
46
+ :es => {:female => ["Rosa"], :male => []},
47
+ :fr => {:female => [], :male => ["Antoine", "Bruno"]},
48
+ }
49
+ end
50
+
51
+ # Helper methods
52
+
53
+ def test_config
54
+ File.join('spec', 'test_data', 'test_config.yml')
55
+ end
56
+
57
+ def stub_invalid_access_response(service)
58
+ body_string = "result=error&code=1&message=Invalid+API+key"
59
+ stub_request(:any, service.config.target_url.to_s).to_return(:headers => {:content_length => body_string.size}, :body => body_string, :status => 202)
60
+ end
61
+
62
+ def stub_ok_response_for_url(target_url)
63
+ body_string = File.new(RESPONSE_TEST_FILE)
64
+ stub_request(:any, target_url.to_s).to_return(:headers => {:content_length => body_string.size}, :body => body_string, :status => 200)
65
+ end
66
+
67
+ def stub_ok_response(service)
68
+ stub_ok_response_for_url(service.config.target_url)
69
+ end
@@ -0,0 +1 @@
1
+ api_key: 'TEST_KEY'
Binary file
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ispeech
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Birkir A. Barkarson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: backports
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.6.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.0
78
+ description: Ruby interface to Ispeech's API for generating speech from text. More
79
+ info at http://www.ispeech.org/api/
80
+ email:
81
+ - birkirb@stoicviking.net
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENCE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - init.rb
92
+ - ispeech.gemspec
93
+ - lib/ispeech.rb
94
+ - lib/ispeech/config.rb
95
+ - lib/ispeech/constants.rb
96
+ - lib/ispeech/error.rb
97
+ - lib/ispeech/mock.rb
98
+ - lib/ispeech/response.rb
99
+ - lib/ispeech/voice.rb
100
+ - lib/ispeech/voice_service.rb
101
+ - lib/ispeech/voices/default.rb
102
+ - script/create_voices.rb
103
+ - script/test_voices.rb
104
+ - spec/ispeech/config_spec.rb
105
+ - spec/ispeech/mock_spec.rb
106
+ - spec/ispeech/response_spec.rb
107
+ - spec/ispeech/voice_service_spec.rb
108
+ - spec/ispeech/voice_spec.rb
109
+ - spec/ispeech_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/test_data/test_config.yml
112
+ - spec/test_data/test_file.mp3
113
+ homepage: https://github.com/birkirb/ispeech
114
+ licenses: []
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project: ispeech
133
+ rubygems_version: 1.8.24
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Generate speech from the ispeech text to voice service.
137
+ test_files: []
138
+ has_rdoc: