Class: Translator::SmartlingDownloader

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

Instance Attribute Summary collapse

Class Method 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(file_uris: nil) ⇒ SmartlingDownloader

Returns a new instance of SmartlingDownloader.



11
12
13
# File 'lib/nexmo_developer/app/services/translator/smartling_downloader.rb', line 11

def initialize(file_uris: nil)
  @file_uris = file_uris || file_uris_from_smartling
end

Instance Attribute Details

#file_urisObject (readonly)

Returns the value of attribute file_uris.



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

def file_uris
  @file_uris
end

Class Method Details

.call(attrs = {}) ⇒ Object



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

def self.call(attrs = {})
  new(attrs).call
end

Instance Method Details

#callObject

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/nexmo_developer/app/services/translator/smartling_downloader.rb', line 15

def call
  raise ArgumentError, "The 'file_uris' parameter cannot be empty" unless @file_uris

  @file_uris.map do |uri|
    locales = get_file_status(file_uri: uri)
    next unless locales

    locales.map { |locale| download_file(locale: locale, file_uri: uri) }
  end
end

#download_file(locale:, file_uri:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/nexmo_developer/app/services/translator/smartling_downloader.rb', line 36

def download_file(locale:, file_uri:)
  doc = ::Translator::Smartling::ApiRequestsGenerator.download_file(
    locale: locale,
    file_uri: file_uri
  )

  return unless doc

  save_file(doc, locale, file_uri)
end

#file_uris_from_smartlingObject



26
27
28
# File 'lib/nexmo_developer/app/services/translator/smartling_downloader.rb', line 26

def file_uris_from_smartling
  ::Translator::Smartling::ApiRequestsGenerator.file_uris
end

#get_file_status(file_uri:) ⇒ Object



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

def get_file_status(file_uri:)
  ::Translator::Smartling::ApiRequestsGenerator.get_file_status(
    file_uri: file_uri
  )
end

#save_file(doc, locale, file_uri) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/nexmo_developer/app/services/translator/smartling_downloader.rb', line 47

def save_file(doc, locale, file_uri)
  locale = locale_without_region(locale.to_s)
  folder = storage_folder(file_uri, locale)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
  File.open(file_path(file_uri, locale), 'w+') do |file|
    file.binmode
    file.write(Nexmo::Markdown::Pipelines::Smartling::Download.call(doc))
  end
end