Class: Nexmo::Markdown::CodeSnippet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



5
6
7
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5

def category
  @category
end

#document_pathObject

Returns the value of attribute document_path.



5
6
7
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5

def document_path
  @document_path
end

Returns the value of attribute navigation_weight.



5
6
7
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5

def navigation_weight
  @navigation_weight
end

#productObject

Returns the value of attribute product.



5
6
7
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5

def product
  @product
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5

def title
  @title
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5

def url
  @url
end

Class Method Details

.allObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



7
8
9
10
11
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
# 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

.filesObject



62
63
64
65
66
67
# 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) ⇒ Object



33
34
35
# 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

.originObject



69
70
71
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 69

def self.origin
  "#{Nexmo::Markdown::Config.docs_base_path}/_documentation"
end