class PolyglotCli::Command::Push

Public Class Methods

init(options) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 8
def self.init(options)
  new(options).call
end
new(options) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 12
def initialize(options)
  @options = options
end

Public Instance Methods

call() click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 16
def call
  if server_changed?
    prompt.error('Changes have been made on the server. Please pull before pushing.')
  else
    languages(option_locale).each do |language|
      prompt.say("Processing push for #{language.name}...")
      process(language)
    end
    update_config
    success
  end
end

Private Instance Methods

create_translation(value, key_name, key_id, language_id) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 59
def create_translation(value, key_name, key_id, language_id)
  prompt.ok("Creating translation for key: #{key_name}...")
  PolyglotCli::Resource::Translation.with_subdomain(subdomain).token(token).create(
    value: value,
    translation_key_id: key_id,
    language_id: language_id
  )
end
create_translation_key(name) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 55
def create_translation_key(name)
  PolyglotCli::Resource::TranslationKey.with_subdomain(subdomain).token(token).create(name: name, project_id: project_id).id
end
local_translations(locale) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 89
def local_translations(locale)
  denest(PolyglotCli::IO::Locale.read(locale_path, locale)[locale])
end
locale_path() click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 101
def locale_path
  option_path || config[:locale_path]
end
option_locale() click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 93
def option_locale
  @options.locale
end
option_path() click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 97
def option_path
  @options.path
end
process(language) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 31
def process(language)
  local = local_translations(language.locale)
  # Update translations
  remote_translations(language).each do |translation|
    process_update(translation, local)
  end

  # Create translation keys and translations
  local.each do |key, value|
    process_create(key, value, language.id)
  end
end
process_create(key, value, language_id) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 44
def process_create(key, value, language_id)
  key_id = translation_keys[key]

  if key_id.nil?
    prompt.ok("Creating translation key: #{key}...")
    key_id = create_translation_key(key)
  end

  create_translation(value, key, key_id, language_id)
end
process_update(translation, local) click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 68
def process_update(translation, local)
  new_value = local.delete(translation.translation_key.name)
  return if new_value == translation.value

  prompt.ok("Updating translation for key: #{translation.translation_key.name}...")
  translation.update(value: new_value)
end
translation_keys() click to toggle source
# File lib/polyglot_cli/commands/push.rb, line 76
def translation_keys
  return @translation_keys if @translation_keys.present?
  prompt.say('Getting translation keys...')
  @translation_keys =
    PolyglotCli::Resource::TranslationKey
    .with_subdomain(subdomain)
    .token(token)
    .depaginate(project_id: project_id)
    .each_with_object({}) do |translation_key, hash|
      hash[translation_key.name] = translation_key.id
    end
end