Class: Nexmo::Markdown::Concept

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/nexmo_markdown_renderer/models/concept.rb

Constant Summary collapse

ORIGIN =
"#{Nexmo::Markdown::Config.docs_base_path}/_documentation".freeze
FILES =
[
  Dir.glob("#{ORIGIN}/#{::I18n.default_locale}/**/guides/**/*.md"),
  Dir.glob("#{ORIGIN}/#{::I18n.default_locale}/**/concepts/**/*.md"),
].flatten

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def description
  @description
end

#document_pathObject

Returns the value of attribute document_path.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def document_path
  @document_path
end

#ignore_in_listObject

Returns the value of attribute ignore_in_list.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def ignore_in_list
  @ignore_in_list
end

Returns the value of attribute navigation_weight.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def navigation_weight
  @navigation_weight
end

#productObject

Returns the value of attribute product.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def product
  @product
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def title
  @title
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 13

def url
  @url
end

Class Method Details

.all(language) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
31
# 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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



75
76
77
78
79
80
# 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) ⇒ Object



58
59
60
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 58

def self.generate_url(path, language)
  '/' + path.gsub("#{ORIGIN}/#{language}/", '').gsub('.md', '')
end

Instance Method Details

#filenameObject



33
34
35
# File 'lib/nexmo_markdown_renderer/models/concept.rb', line 33

def filename
  Pathname(document_path).basename.to_s.gsub('.md', '')
end