Class: Nexmo::Markdown::DocFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo_markdown_renderer/services/doc_finder.rb

Defined Under Namespace

Classes: Doc, MissingDoc

Constant Summary collapse

EXCLUSIONS =
['.', '..', ::I18n.default_locale.to_s].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dictionaryObject

Returns the value of attribute dictionary.



10
11
12
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 10

def dictionary
  @dictionary
end

.pathsObject

Returns the value of attribute paths.



9
10
11
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 9

def paths
  @paths
end

Class Method Details

.build_doc(root:, language:, key:) ⇒ Object

rubocop:enable Metrics/ParameterLists



58
59
60
61
62
63
64
65
66
67
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 58

def self.build_doc(root:, language:, key:)
  if root.start_with?('app/views')
    DocFinder::Doc.new(path: dictionary.fetch(key) && key, available_languages: ['en'])
  else
    available_languages = dictionary.fetch(key)
    available_language = available_languages.fetch(language, ::I18n.default_locale.to_s)

    DocFinder::Doc.new(path: build_doc_path(root, key, available_language), available_languages: available_languages.keys)
  end
end

.build_doc_path(root, doc, language) ⇒ Object



79
80
81
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 79

def self.build_doc_path(root, doc, language)
  doc.gsub(root, "#{root}/#{language}")
end

.build_key(root:, document:, product: nil, format: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 69

def self.build_key(root:, document:, product: nil, format: nil)
  path = if Pathname.new(document).extname.blank?
    [root, product, document].compact.join("/").concat(".#{format}")
  else
    [root, product, document].compact.join("/")
  end

  path.gsub(%r{\/\/\/|\/\/}, '/')
end

.code_languages_for_tutorial(path:) ⇒ Object



125
126
127
128
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 125

def self.code_languages_for_tutorial(path:)
  key = strip_root_and_language(root: '/', document: path, language: ::I18n.default_locale)
  dictionary.keys.select { |k| k.include?(key) }
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



83
84
85
86
87
88
89
90
91
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 83

def self.configure
  self.paths = []
  self.dictionary = Hash.new { |hash, key| hash[key] = {} }

  yield(self)

  load_english
  load_languages
end

.find(root:, document:, language: nil, product: nil, code_language: nil, format: 'md', strip_root_and_language: false) ⇒ Object

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 14

def self.find(root:, document:, language: nil, product: nil, code_language: nil, format: 'md', strip_root_and_language: false)
  if strip_root_and_language
    document = strip_root_and_language(root: root, language: "(#{language}|#{::I18n.default_locale})", document: document)
  end
  begin
    if code_language.present?
      linkable_code_language(
        root: root,
        language: language.to_s,
        product: product,
        document: document,
        code_language: code_language,
        format: format
      )
    else
      non_linkable(
        root: root,
        language: language.to_s,
        product: product,
        document: document,
        format: format
      )
    end
  rescue KeyError => e
    raise MissingDoc, e.message
  end
end

.linkable_code_language(root:, language:, document:, product: nil, code_language: nil, format: nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 42

def self.linkable_code_language(root:, language:, document:, product: nil, code_language: nil, format: nil)
  key = [
    build_key(root: root, product: product, document: "#{document}/#{code_language}", format: format),
    build_key(root: root, product: product, document: document, format: format),
  ].select { |k| dictionary.key?(k) }.first

  build_doc(root: root, language: language, key: key)
end

.load_englishObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 93

def self.load_english
  paths.each do |path|
    if defined?(Rails::Application) && path.start_with?("#{Rails.root}/app/views")
      Dir["#{path}/**/*.*"].each do |file|
        dictionary[file.gsub("#{Rails.root}/", '')][::I18n.default_locale.to_s] = ::I18n.default_locale.to_s
      end
    else
      path_with_locale = "#{path}/#{::I18n.default_locale}"
      Dir["#{path_with_locale}/**/{*.*,.config.yml}"].each do |file|
        key = "#{path}/#{file.gsub("#{path_with_locale}/", '')}"
        dictionary[key][::I18n.default_locale.to_s] = ::I18n.default_locale.to_s
      end
    end
  end
end

.load_languagesObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 109

def self.load_languages
  paths.each do |path|
    Dir.foreach(path).reject { |d| EXCLUSIONS.include? d }.each do |language|
      Dir.glob("#{path}/#{language}/**/{*.*,.config.yml}").each do |file|
        doc_name = strip_root_and_language(root: path, language: language, document: file)
        key = "#{path}/#{doc_name}"
        dictionary[key][language] = language
      end
    end
  end
end

.non_linkable(root:, language:, document:, product: nil, format: nil) ⇒ Object



51
52
53
54
55
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 51

def self.non_linkable(root:, language:, document:, product: nil, format: nil)
  key = build_key(root: root, product: product, document: document, format: format)

  build_doc(root: root, language: language, key: key)
end

.strip_root_and_language(root:, language:, document:) ⇒ Object



121
122
123
# File 'lib/nexmo_markdown_renderer/services/doc_finder.rb', line 121

def self.strip_root_and_language(root:, language:, document:)
  document.sub(%r{#{root}\/}, '').sub(%r{#{language}\/}, '')
end