Module: RubySpeech::GRXML

Defined in:
lib/ruby_speech/grxml.rb,
lib/ruby_speech/grxml/tag.rb,
lib/ruby_speech/grxml/item.rb,
lib/ruby_speech/grxml/rule.rb,
lib/ruby_speech/grxml/match.rb,
lib/ruby_speech/grxml/token.rb,
lib/ruby_speech/grxml/one_of.rb,
lib/ruby_speech/grxml/element.rb,
lib/ruby_speech/grxml/grammar.rb,
lib/ruby_speech/grxml/matcher.rb,
lib/ruby_speech/grxml/ruleref.rb,
lib/ruby_speech/grxml/no_match.rb,
lib/ruby_speech/grxml/max_match.rb,
lib/ruby_speech/grxml/potential_match.rb,
ext/ruby_speech/ruby_speech.c

Defined Under Namespace

Modules: Builtins Classes: Element, Grammar, Item, Match, Matcher, MaxMatch, NoMatch, OneOf, PotentialMatch, Rule, Ruleref, Tag, Token

Constant Summary collapse

InvalidChildError =
Class.new StandardError
MissingReferenceError =
Class.new StandardError
ReferentialLoopError =
Class.new StandardError
GRXML_NAMESPACE =
'http://www.w3.org/2001/06/grammar'
URI_REGEX =
/builtin:(?<class>.*)\/(?<type>\w*)(\?)?(?<query>(\w*=\w*;?)*)?/.freeze

Class Method Summary collapse

Class Method Details

.draw(attributes = {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/ruby_speech/grxml.rb', line 23

def self.draw(attributes = {}, &block)
  document = Nokogiri::XML::Document.new
  Grammar.new(document, attributes).tap do |grammar|
    document.root = grammar.node
    block_return = grammar.eval_dsl_block &block
    grammar << block_return if block_return.is_a?(String)
  end.assert_has_matching_root_rule
end

.from_uri(uri) ⇒ RubySpeech::GRXML::Grammar

Fetch a builtin grammar by URI

Parameters:

  • uri (String)

    The builtin grammar URI of the form “builtin:dtmf/type?param=value”

Returns:

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_speech/grxml.rb', line 45

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
  raise ArgumentError, "#{type} is an invalid builtin grammar" unless Builtins.respond_to?(type)
  Builtins.send type, query
end

.import(other) ⇒ Object



32
33
34
# File 'lib/ruby_speech/grxml.rb', line 32

def self.import(other)
  Element.import other
end