Class: Nexmo::Markdown::CodeSnippet
- Inherits:
-
Object
- Object
- Nexmo::Markdown::CodeSnippet
- Includes:
- ActiveModel::Model
- Defined in:
- lib/nexmo_markdown_renderer/models/code_snippet.rb
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#document_path ⇒ Object
Returns the value of attribute document_path.
-
#navigation_weight ⇒ Object
Returns the value of attribute navigation_weight.
-
#product ⇒ Object
Returns the value of attribute product.
-
#title ⇒ Object
Returns the value of attribute title.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .all ⇒ Object
- .by_product(product) ⇒ Object
- .extract_category(path) ⇒ Object
- .extract_product(path) ⇒ Object
- .files ⇒ Object
- .generate_url(path) ⇒ Object
- .origin ⇒ Object
Instance Attribute Details
#category ⇒ Object
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_path ⇒ Object
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 |
#navigation_weight ⇒ Object
Returns the value of attribute navigation_weight.
5 6 7 |
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5 def @navigation_weight end |
#product ⇒ Object
Returns the value of attribute product.
5 6 7 |
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5 def product @product end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/nexmo_markdown_renderer/models/code_snippet.rb', line 5 def title @title end |
#url ⇒ Object
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
.all ⇒ Object
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 |
.files ⇒ Object
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 |
.origin ⇒ Object
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 |