Class: Translator::FileTranslator

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/nexmo_developer/app/services/translator/file_translator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#file_path, #file_uri, #locale_with_region, #locale_without_region, #storage_folder

Constructor Details

#initialize(doc_path) ⇒ FileTranslator

Returns a new instance of FileTranslator.



7
8
9
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 7

def initialize(doc_path)
  @doc_path = doc_path
end

Instance Attribute Details

#doc_pathObject (readonly)

Returns the value of attribute doc_path.



5
6
7
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 5

def doc_path
  @doc_path
end

Instance Method Details

#frequencyObject



36
37
38
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 36

def frequency
  @frequency ||= frontmatter['translation_frequency'] || product_translation_frequency
end

#frontmatterObject



28
29
30
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 28

def frontmatter
  @frontmatter ||= YAML.safe_load(File.read(full_path))
end

#full_pathObject



32
33
34
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 32

def full_path
  @full_path ||= "#{Rails.configuration.docs_base_path}/#{doc_path}"
end

#productObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 48

def product
  @product ||= begin
    products = YAML.safe_load(File.open("#{Rails.configuration.docs_base_path}/config/products.yml"))
    product = products['products'].detect do |p|
      product_from_path.starts_with?(p['path']) || frontmatter['products']&.include?(p['path'])
    end

    raise ArgumentError, "Unable to match document with products list in config/products.yml for #{full_path}" unless product

    product
  end
end

#product_from_pathObject



40
41
42
43
44
45
46
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 40

def product_from_path
  @product_from_path ||= begin
    Pathname.new(
      doc_path.gsub(%r{(_documentation|_tutorials|_use_cases)/#{I18n.default_locale}/}, '')
    ).dirname.to_s
  end
end

#product_translation_frequencyObject

Raises:

  • (ArgumentError)


61
62
63
64
65
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 61

def product_translation_frequency
  raise ArgumentError, "Expected a 'translation_frequency' attribute for #{product['name']} but none found" unless product['translation_frequency']

  product['translation_frequency']
end

#translation_requestsObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 11

def translation_requests
  @translation_requests ||= begin
    I18n.available_locales.reject { |l| l.to_s == I18n.default_locale.to_s }.map do |locale|
      Translator::TranslationRequest.new(
        locale: locale_with_region(locale),
        frequency: frequency,
        file_uri: uri,
        file_path: full_path
      )
    end
  end
end

#uriObject



24
25
26
# File 'lib/nexmo_developer/app/services/translator/file_translator.rb', line 24

def uri
  @uri ||= file_uri(doc_path)
end