Class: SignalWire::SWML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/signalwire/swml/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

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

#sectionsObject (readonly)

Returns the value of attribute sections.



8
9
10
# File 'lib/signalwire/swml/document.rb', line 8

def sections
  @sections
end

#versionObject (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.

Returns:

  • (Boolean)


35
36
37
# File 'lib/signalwire/swml/document.rb', line 35

def has_section?(name)
  @sections.key?(name.to_s)
end

#renderObject

Compact JSON string.



74
75
76
# File 'lib/signalwire/swml/document.rb', line 74

def render
  JSON.generate(to_h)
end

#render_prettyObject

Pretty-printed JSON string.



79
80
81
# File 'lib/signalwire/swml/document.rb', line 79

def render_pretty
  JSON.pretty_generate(to_h)
end

#resetObject

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_hObject

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