Class: SignalWire::SWML::Document
- Inherits:
-
Object
- Object
- SignalWire::SWML::Document
- Defined in:
- lib/signalwire/swml/document.rb
Instance Attribute Summary collapse
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#add_section(name) ⇒ Object
Add a new named section.
-
#add_verb(verb_name, config) ⇒ Object
Append a verb to the main section.
-
#add_verb_to_section(section, verb_name, config) ⇒ Object
Append a verb to an arbitrary section.
-
#get_verbs(section = 'main') ⇒ Object
Return the list of verb hashes for a section.
-
#has_section?(name) ⇒ Boolean
Check whether a section exists.
-
#initialize ⇒ Document
constructor
A new instance of Document.
-
#render ⇒ Object
Compact JSON string.
-
#render_pretty ⇒ Object
Pretty-printed JSON string.
-
#reset ⇒ Object
Reset the document to its initial empty state.
-
#to_h ⇒ Object
Produce a plain Ruby hash suitable for JSON serialization.
Constructor Details
#initialize ⇒ Document
Returns a new instance of Document.
10 11 12 13 14 |
# File 'lib/signalwire/swml/document.rb', line 10 def initialize @version = '1.0.0' @sections = { 'main' => [] } @mutex = Mutex.new end |
Instance Attribute Details
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
8 9 10 |
# File 'lib/signalwire/swml/document.rb', line 8 def sections @sections end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
8 9 10 |
# File 'lib/signalwire/swml/document.rb', line 8 def version @version end |
Instance Method Details
#add_section(name) ⇒ Object
Add a new named section. Returns true if created, false if it already exists.
24 25 26 27 28 29 30 31 32 |
# File 'lib/signalwire/swml/document.rb', line 24 def add_section(name) name = name.to_s @mutex.synchronize do return false if @sections.key?(name) @sections[name] = [] true end end |
#add_verb(verb_name, config) ⇒ Object
Append a verb to the main section.
add_verb('answer', {})
add_verb('sleep', 2000)
43 44 45 |
# File 'lib/signalwire/swml/document.rb', line 43 def add_verb(verb_name, config) add_verb_to_section('main', verb_name, config) end |
#add_verb_to_section(section, verb_name, config) ⇒ Object
Append a verb to an arbitrary section.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/signalwire/swml/document.rb', line 48 def add_verb_to_section(section, verb_name, config) section = section.to_s @mutex.synchronize do unless @sections.key?(section) raise ArgumentError, "Section '#{section}' does not exist" end @sections[section] << { verb_name.to_s => config } true end end |
#get_verbs(section = 'main') ⇒ Object
Return the list of verb hashes for a section.
61 62 63 |
# File 'lib/signalwire/swml/document.rb', line 61 def get_verbs(section = 'main') @sections.fetch(section.to_s, []).dup end |
#has_section?(name) ⇒ Boolean
Check whether a section exists.
35 36 37 |
# File 'lib/signalwire/swml/document.rb', line 35 def has_section?(name) @sections.key?(name.to_s) end |
#render ⇒ Object
Compact JSON string.
74 75 76 |
# File 'lib/signalwire/swml/document.rb', line 74 def render JSON.generate(to_h) end |
#render_pretty ⇒ Object
Pretty-printed JSON string.
79 80 81 |
# File 'lib/signalwire/swml/document.rb', line 79 def render_pretty JSON.pretty_generate(to_h) end |
#reset ⇒ Object
Reset the document to its initial empty state.
17 18 19 20 21 |
# File 'lib/signalwire/swml/document.rb', line 17 def reset @mutex.synchronize do @sections = { 'main' => [] } end end |
#to_h ⇒ Object
Produce a plain Ruby hash suitable for JSON serialization.
66 67 68 69 70 71 |
# File 'lib/signalwire/swml/document.rb', line 66 def to_h { 'version' => @version, 'sections' => @sections.transform_values(&:dup) } end |