Class: Nexmo::Markdown::SnippetVariablesFilter

Inherits:
Banzai::Filter
  • Object
show all
Defined in:
lib/nexmo_markdown_renderer/filters/snippet_variables_filter.rb

Instance Method Summary collapse

Instance Method Details

#call(input) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nexmo_markdown_renderer/filters/snippet_variables_filter.rb', line 4

def call(input)
  input.gsub(/```snippet_variables(.+?)```/m) do |_s|
    config = YAML.safe_load(Regexp.last_match(1))

    raise 'No variables provided' unless config
    raise 'Must provide an array' unless config.is_a?(Array)

    output = <<~HEREDOC
      Key | Description
      -- | --
    HEREDOC
    config.each do |key|

      details = variables[key]
      raise "#{key} is not a valid snippet variable" unless details

      # We have some variables in the format TO_NUMBER.SMS etc, and we only want to render the first segment
      # This can be multiple segments e.g. UUID.MODIFY.VOICE will be rendered as UUID
      title = key.split('.').first

      output += <<~HEREDOC
        `#{title}` | #{details}
      HEREDOC
    end

    output
  end
end

#variablesObject



33
34
35
# File 'lib/nexmo_markdown_renderer/filters/snippet_variables_filter.rb', line 33

def variables
  @variables ||= YAML.safe_load(File.read("#{Nexmo::Markdown::Config.docs_base_path}/config/code_snippet_variables.yml"))
end