Class: SmartlingAPI

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

Instance Method Summary collapse

Methods included from Translator::Utils

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

Constructor Details

#initialize(user_id:, user_secret:, project_id:) ⇒ SmartlingAPI

Returns a new instance of SmartlingAPI.



4
5
6
7
8
9
10
# File 'lib/nexmo_developer/app/services/smartling_api.rb', line 4

def initialize(user_id:, user_secret:, project_id:)
  @client = Smartling::File.new(
    userId: user_id,
    userSecret: user_secret,
    projectId: project_id
  )
end

Instance Method Details

#download_translated(filename:, locale:, type: :published) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nexmo_developer/app/services/smartling_api.rb', line 49

def download_translated(filename:, locale:, type: :published)
  file_uri = file_uri(filename)
  wrap_in_rescue do
    response = @client.download_translated(file_uri, locale, retrievalType: type)

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

#file_type(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/nexmo_developer/app/services/smartling_api.rb', line 33

def file_type(filename)
  case Pathname.new(filename).extname
  when '.md', '.markdown'
    'markdown'
  when '.yml', '.yaml'
    'yaml'
  else
    raise 'Unsupported file type.'
  end
end

#last_modified(filename:, locale:) ⇒ Object



44
45
46
47
# File 'lib/nexmo_developer/app/services/smartling_api.rb', line 44

def last_modified(filename:, locale:)
  file_uri = file_uri(filename)
  wrap_in_rescue { @client.last_modified(file_uri, locale) }
end

#upload(filename) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nexmo_developer/app/services/smartling_api.rb', line 12

def upload(filename)
  file_uri = file_uri(filename)
  file = Tempfile.new
  file.write Nexmo::Markdown::Pipelines::Smartling::Preprocessor.new.call(
    File.read("#{Rails.configuration.docs_base_path}/#{filename}")
  )
  file.rewind

  wrap_in_rescue do
    @client.upload(
      file.path,
      file_uri,
      file_type(filename),
      'smartling.markdown_code_notranslate': true
    )
  end
ensure
  file.close
  file.unlink
end