Skip to content

Commit

Permalink
[CS] Proper error handling when looking up grammars by URI
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Sep 30, 2013
1 parent 5b8e584 commit 83b50e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/ruby_speech/grxml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.import(other)
Element.import other
end

URI_REGEX = /builtin:dtmf\/(?<type>\w*)(\?)?(?<query>(\w*=\w*;?)*)?/.freeze
URI_REGEX = /builtin:(?<class>.*)\/(?<type>\w*)(\?)?(?<query>(\w*=\w*;?)*)?/.freeze

#
# Fetch a builtin grammar by URI
Expand All @@ -42,12 +42,16 @@ def self.import(other)
#
def self.from_uri(uri)
match = uri.match(URI_REGEX)
raise ArgumentError, "Only builtin grammars are supported" unless match
raise ArgumentError, "Only DTMF builtins are supported" unless match[:class] == 'dtmf'
type = match[:type]
query = {}
match[:query].split(';').each do |s|
key, value = s.split('=')
query[key] = value
end
grammar = Builtins.send match[:type], query
raise ArgumentError, "#{type} is an invalid builtin grammar" unless Builtins.respond_to?(type)
Builtins.send type, query
end
end # GRXML
end # RubySpeech
28 changes: 24 additions & 4 deletions spec/ruby_speech/grxml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@
module RubySpeech
describe GRXML do
describe ".from_uri" do
it "should fetch a simple builtin grammar by type" do
subject.from_uri("builtin:dtmf/phone").should == GRXML::Builtins.phone
context "with a builtin URI" do
it "should fetch a simple builtin grammar by type" do
subject.from_uri("builtin:dtmf/phone").should == GRXML::Builtins.phone
end

it "should fetch a parameterized builtin grammar" do
subject.from_uri("builtin:dtmf/boolean?y=3;n=4").should == GRXML::Builtins.boolean(y: 3, n: 4)
end

context "for speech" do
it "should raise ArgumentError" do
expect { subject.from_uri("builtin:speech/phone") }.to raise_error(ArgumentError, /Only DTMF/)
end
end

context "that doesn't exist" do
it "should raise ArgumentError" do
expect { subject.from_uri("builtin:dtmf/foobar") }.to raise_error(ArgumentError, /invalid/)
end
end
end

it "should fetch a parameterized builtin grammar" do
subject.from_uri("builtin:dtmf/boolean?y=3;n=4").should == GRXML::Builtins.boolean(y: 3, n: 4)
context "with an http URI" do
it "should raise ArgumentError" do
expect { subject.from_uri("http://foo.com/grammar.grxml") }.to raise_error(ArgumentError, /builtin/)
end
end
end

Expand Down

0 comments on commit 83b50e4

Please sign in to comment.