class Nexmo::Markdown::Concept

Constants

FILES
ORIGIN

Attributes

description[RW]
document_path[RW]
ignore_in_list[RW]
navigation_weight[RW]
product[RW]
title[RW]
url[RW]

Public Class Methods

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

    frontmatter = YAML.safe_load(document)

    Nexmo::Markdown::Concept.new({
      title: frontmatter['title'],
      description: frontmatter['description'],
      navigation_weight: frontmatter['navigation_weight'] || 999,
      ignore_in_list: frontmatter['ignore_in_list'],
      product: product,
      document_path: document_path,
      url: generate_url(document_path, language),
    })
  end

  blocks.sort_by(&:navigation_weight)
end
by_name(names, language) click to toggle source
# File lib/nexmo_markdown_renderer/models/concept.rb, line 15
def self.by_name(names, language)
  matches = all(language).select do |block|
    concept = "#{block.product}/#{block.filename}"
    match = names.include?(concept)
    names.delete(concept) if match
    match
  end

  raise "Could not find concepts: #{names.join(', ')}" unless names.empty?
  matches
end
by_product(product, language) click to toggle source
# File lib/nexmo_markdown_renderer/models/concept.rb, line 27
def self.by_product(product, language)
  all(language).select do |block|
    block.product == product
  end
end
extract_product(path) click to toggle source
# File lib/nexmo_markdown_renderer/models/concept.rb, line 62
def self.extract_product(path)
  # Remove the prefix
  path = path.gsub!(%r{#{ORIGIN}\/[a-z]{2}\/}, '')

  # Each file is in the form guides/<title>.md, so let's remove the last two segments
  parts = path.split('/')
  parts = parts[0...-2]

  # What's left once we remove the start and end of the path is our product name. This could be any number
  # of parts, but it's generally 1-2
  parts.join('/')
end
files(language) click to toggle source
# File lib/nexmo_markdown_renderer/models/concept.rb, line 75
def self.files(language)
  FILES.each_with_object([]) do |file, array|
    document = file.gsub("#{ORIGIN}/#{::I18n.default_locale}/", '')
    array << Nexmo::Markdown::DocFinder.find(root: ORIGIN, document: document, language: language).path
  end
end
generate_url(path, language) click to toggle source
# File lib/nexmo_markdown_renderer/models/concept.rb, line 58
def self.generate_url(path, language)
  '/' + path.gsub("#{ORIGIN}/#{language}/", '').gsub('.md', '')
end

Public Instance Methods

filename() click to toggle source
# File lib/nexmo_markdown_renderer/models/concept.rb, line 33
def filename
  Pathname(document_path).basename.to_s.gsub('.md', '')
end