class Nexmo::Markdown::CodeSnippet

Attributes

category[RW]
document_path[RW]
navigation_weight[RW]
product[RW]
title[RW]
url[RW]

Public Class Methods

all() click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 13
def self.all
  blocks = files.map do |document_path|
    document = File.read(document_path)
    product = extract_product(document_path)

    frontmatter = YAML.safe_load(document)

    Nexmo::Markdown::CodeSnippet.new({
      title: frontmatter['title'],
      navigation_weight: frontmatter['navigation_weight'] || 999,
      product: product,
      document_path: document_path,
      category: extract_category(document_path),
      url: generate_url(document_path),
    })
  end

  blocks.sort_by(&:navigation_weight)
end
by_product(product) click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 7
def self.by_product(product)
  all.select do |block|
    block.product == product
  end
end
extract_category(path) click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 47
def self.extract_category(path)
  # Remove the prefix
  path = path.gsub(%r{#{origin}/\w{2}/}, '')

  # Each file is in the form code-snippets/<title>.md, so let's capture everything after code-snippets
  path = path.gsub(%r{.*/code-snippets/(.*)$}, '\1')

  parts = path.split('/')
  parts = parts[0...-1]

  return nil if parts.empty?

  parts.join('/').tr('-', ' ').humanize
end
extract_product(path) click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 37
def self.extract_product(path)
  # Remove the prefix
  path = path.gsub!(%r{#{origin}/\w{2}/}, '')

  # Each file is in the form code-snippets/<title>.md, so let's remove everything after code-snippets
  path = path.gsub(%r{/code-snippets/.*}, '')

  path
end
files() click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 62
def self.files
  root = "#{origin}/#{::I18n.default_locale}"
  Dir.glob("#{root}/**/code-snippets/**/*.md").map do |path|
    DocFinder.find(root: origin, document: path.gsub(root, ''), language: ::I18n.locale).path
  end
end
generate_url(path) click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 33
def self.generate_url(path)
  '/' + path.gsub(%r{#{origin}/\w{2}/}, '').gsub('.md', '')
end
origin() click to toggle source
# File lib/nexmo_markdown_renderer/models/code_snippet.rb, line 69
def self.origin
  "#{Nexmo::Markdown::Config.docs_base_path}/_documentation"
end