Skip to content

Commit

Permalink
Merge b319b70 into 77ffd0e
Browse files Browse the repository at this point in the history
  • Loading branch information
chewi committed Dec 6, 2017
2 parents 77ffd0e + b319b70 commit 0d44540
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_speech/ssml/break.rb
Expand Up @@ -36,7 +36,7 @@ def strength=(s)
# @return [Float]
#
def time
read_attr :time, :to_f
get_time_attribute :time
end

##
Expand Down
14 changes: 13 additions & 1 deletion lib/ruby_speech/ssml/element.rb
Expand Up @@ -23,9 +23,21 @@ def to_doc

private

def get_time_attribute(key)
value = read_attr(key)

if value.nil?
nil
elsif value.end_with?('ms')
value.to_f / 1000
else
value.to_f
end
end

def set_time_attribute(key, value)
raise ArgumentError, "You must specify a valid #{key} (positive float value in seconds)" unless value.is_a?(Numeric) && value >= 0
self[key] = "#{value}s"
self[key] = value == value.round ? "#{value.to_i}s" : "#{(value * 1000).to_i}ms"
end
end # Element
end # SSML
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby_speech/ssml/prosody.rb
Expand Up @@ -134,7 +134,8 @@ def rate=(v)
# @return [Integer]
#
def duration
read_attr :duration, :to_i
value = get_time_attribute :duration
value.round if value
end

##
Expand Down
34 changes: 27 additions & 7 deletions spec/ruby_speech/ssml/break_spec.rb
Expand Up @@ -21,14 +21,28 @@ module SSML
end

describe "from a document" do
let(:document) { '<break strength="strong" time="3"/>' }

subject { Element.import document }

it { should be_instance_of Break }
context 'with time of 3' do
let(:document) { '<break strength="strong" time="3"/>' }

its(:strength) { should == :strong }
its(:time) { should == 3 }
it { should be_instance_of Break }

its(:strength) { should == :strong }
its(:time) { should eql 3.0 }
end

context 'with time of 4s' do
let(:document) { '<break time="4s"/>' }

its(:time) { should eql 4.0 }
end

context 'with time of 5555ms' do
let(:document) { '<break time="5555ms"/>' }

its(:time) { should eql 5.555 }
end
end

describe "#strength" do
Expand All @@ -51,10 +65,16 @@ module SSML
end

describe "#time" do
context "with a valid value" do
context "with a valid whole seconds value of 3" do
before { subject.time = 3 }

its(:time) { should == 3 }
its(:time) { should eql 3.0 }
end

context "with a valid fractional seconds value of 3.5" do
before { subject.time = 3.5 }

its(:time) { should eql 3.5 }
end

context "with a negative value" do
Expand Down
6 changes: 3 additions & 3 deletions spec/ruby_speech/ssml/prosody_spec.rb
Expand Up @@ -16,7 +16,7 @@ module SSML
its(:contour) { should == 'something' }
its(:range) { should == '20Hz' }
its(:rate) { should == 2 }
its(:duration) { should == 10 }
its(:duration) { should eql 10 }
its(:volume) { should == :loud }
end

Expand All @@ -35,7 +35,7 @@ module SSML
its(:contour) { should == 'something' }
its(:range) { should == '20Hz' }
its(:rate) { should == 2 }
its(:duration) { should == 10 }
its(:duration) { should eql 10 }
its(:volume) { should == :loud }
end

Expand Down Expand Up @@ -174,7 +174,7 @@ module SSML
context "with a valid value" do
before { subject.duration = 3 }

its(:duration) { should == 3 }
its(:duration) { should eql 3 }
end

context "with a negative value" do
Expand Down

0 comments on commit 0d44540

Please sign in to comment.