class POEditor::PullCommand

Public Class Methods

new(project_path = "") click to toggle source

@param project_path [String] Path of project main directory [optional] -

if present then target paths would be created
by concatenating it with paths in JSON config
# File lib/PullCommand.rb, line 13
def initialize(project_path = "")
  @project_path = project_path
end

Public Instance Methods

run(config, target_name = "") click to toggle source

Pulls POEditor translations based on passed POEditor JSON config and target name

@param config [JSON] POEditor JSON config

@param target_name [String] Target name for translations [optional] -

if absent paths would not be parameterized
# File lib/PullCommand.rb, line 24
def run(config, target_name = "")
  pull_translations(create_configuration(config, target_name))
end
run_from_path(config_path, target_name = "") click to toggle source

Pulls POEditor translations based on POEditor JSON config read from passed path and target name

@param config_path [String] Path to POEditor config

@param target_name [String] Target name for translations [optional] -

if absent paths would not be parameterized
# File lib/PullCommand.rb, line 36
def run_from_path(config_path, target_name = "")
  pull_translations(get_configuration(config_path, target_name))
end

Private Instance Methods

create_configuration(json_config, target_name) click to toggle source
# File lib/PullCommand.rb, line 70
def create_configuration(json_config, target_name)
  translations_path = File.join(@project_path, path_for_target(get_or_raise(json_config, "path"), target_name))
  path_copy_array = parse_paths_array(json_config, target_name, "path_copy")
  path_replace_array = parse_paths_array(json_config, target_name, "path_replace")
  Configuration.new(
    api_key: get_or_raise(json_config, "api_key"),
    project_id: get_or_raise(json_config, "project_id"),
    type: get_or_raise(json_config, "type"),
    tags: json_config["tags"],
    filters: json_config["filters"],
    languages: get_or_raise(json_config, "languages"),
    language_alias: json_config["language_alias"],
    path: translations_path,
    path_replace: path_replace_array,
    path_copy: path_copy_array
  )
end
get_configuration(config_path, target_name) click to toggle source

Returns {#POEditor::Configuration} based on POEditor JSON config and target name

@param config_path [String] Path to POEditor config

@param target_name [String] Target name for translations

@return [POEditor::Configuration] The export configuration

# File lib/PullCommand.rb, line 58
    def get_configuration(config_path, target_name)
      UI.puts "Reading configuration"
      unless File.exist?(config_path)
        raise POEditor::Exception.new %{\
Configuration file doesn't exist: #{config_path}.
    Specify the path using '--config'.\
        }
      end
      json_config = JSON(File.read(config_path))
      create_configuration(json_config, target_name)
    end
get_or_raise(json_config, key) click to toggle source

Returns the value of specified key from the given json instance. Raise exception when there's no key in the json.

@param json_config [JSON] @param key [String]

@return The value for the specified key from json @raise [POEditor::Exception]

# File lib/PullCommand.rb, line 96
def get_or_raise(json_config, key)
  json_config[key] or raise POEditor::Exception.new \
    "Missing configuration key: '#{key}'"
end
parse_paths_array(json_config, target_name, array_name) click to toggle source
# File lib/PullCommand.rb, line 105
def parse_paths_array(json_config, target_name, array_name)
  paths_array = json_config[array_name]
  if paths_array != nil
    paths_array.each { |language, language_filepath| paths_array[language] = File.join(@project_path,
                                                                                       path_for_target(language_filepath, target_name)) }
  end
end
path_for_target(path, target_name) click to toggle source
# File lib/PullCommand.rb, line 101
def path_for_target(path, target_name)
  path.gsub("{TARGET_NAME}", target_name)
end
pull_translations(configuration) click to toggle source

Pulls POEditor translations based on configuration

@param configuration [Configuration] Configuration object

# File lib/PullCommand.rb, line 46
def pull_translations(configuration)
  client = POEditor::Core.new(configuration)
  client.pull()
end