class ToptranslationCli::Pull

Public Class Methods

new() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 14
def initialize
  ToptranslationCli.configuration.load

  @pastel = Pastel.new
  @spinner_settings = { success_mark: @pastel.green('+'), error_mark: @pastel.red('-') }
  @spinner = TTY::Spinner.new("[#{@pastel.yellow(':spinner')}] :title", @spinner_settings)
end
run() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 10
def self.run
  new.run
end

Public Instance Methods

run() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 22
def run
  changed = changed_files(remote_files, local_files)
  download_files(changed)
rescue RestClient::BadRequest
  @spinner.error(@pastel.red('invalid access token')) if @spinner.spinning?
  exit 1
rescue RestClient::NotFound
  @spinner.error(@pastel.red('project not found')) if @spinner.spinning?
  exit 1
end

Private Instance Methods

changed_files(remote_files, local_files) click to toggle source
# File lib/toptranslation_cli/pull.rb, line 35
def changed_files(remote_files, local_files)
  @spinner.update(title: 'Checking for changed files...')
  files = remote_files.reject do |file|
    local_files[file[:path]] == file[:sha1]
  end
  @spinner.auto_spin
  @spinner.success(@pastel.green("found #{files.count} changed file(s)"))
  files
end
download_files(files) click to toggle source
# File lib/toptranslation_cli/pull.rb, line 45
def download_files(files)
  return if files.empty?

  bar = TTY::ProgressBar.new('Downloading [:bar] :percent [:current/:total]', total: files.count)
  bar.render

  files.each_in_threads(8) do |file|
    file[:document].download(file[:locale].code, path: file[:path])
    bar.synchronize { bar.log(file[:path]) }
    bar.advance
  end
end
file_to_download(document, translation) click to toggle source
# File lib/toptranslation_cli/pull.rb, line 90
def file_to_download(document, translation)
  {
    path: path(document, translation.locale),
    document: document,
    sha1: translation.sha1,
    locale: translation.locale
  }
end
local_files() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 58
def local_files
  @spinner.update(title: 'Checking local files...')
  @spinner.auto_spin
  files = FileFinder.local_files(project)
  @spinner.success(@pastel.green("found #{files.count} file(s)"))
  files
end
path(document, locale) click to toggle source
# File lib/toptranslation_cli/pull.rb, line 66
def path(document, locale)
  document.path.gsub('{locale_code}', locale.code)
end
project() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 74
def project
  @project ||= ToptranslationCli.connection.projects.find(ToptranslationCli.configuration.project_identifier)
end
project_locales() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 70
def project_locales
  @project_locales ||= project.locales
end
remote_files() click to toggle source
# File lib/toptranslation_cli/pull.rb, line 78
def remote_files
  @spinner.update(title: 'Checking remote files...')
  @spinner.auto_spin
  files = project&.documents&.flat_map do |document|
    document.translations.map do |translation|
      file_to_download(document, translation)
    end
  end
  @spinner.success(@pastel.green("found #{files.count} file(s)"))
  files
end