Class: POEditor::PullCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/poeditor/commands/pull_command.rb

Instance Method Summary collapse

Instance Method Details

#get_configuration(argv) ⇒ POEditor::Configuration

Returns Configuration from the given system arguments.

Parameters:

  • argv (Array<String>)

    System arguments

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/poeditor/commands/pull_command.rb', line 31

def get_configuration(argv)
  config_path = get_configuration_file_path(argv)
  unless File.exist?(config_path)
    raise POEditor::Exception.new %{\
Configuration file doesn't exist: #{config_path}.
Try creating `poeditor.yml` or specifying the path using '--config'.\
    }
  end
  yaml = YAML.load(File.read(config_path))
  Configuration.new(
    api_key: get_or_raise(yaml, "api_key"),
    project_id: get_or_raise(yaml, "project_id"),
    type: get_or_raise(yaml, "type"),
    tags: yaml["tags"],
    filters: yaml["filters"],
    languages: get_or_raise(yaml, "languages"),
    language_alias: yaml["language_alias"],
    path: get_or_raise(yaml, "path"),
    path_replace: yaml["path_replace"],
  )
end

#get_configuration_file_path(argv) ⇒ String

Detects and returns the location of ‘poeditor.yml` file from the given system arguments.

Parameters:

  • argv (Array<String>)

    System arguments

Returns:

  • (String)

    The detected path of ‘poeditor.yml` file



17
18
19
20
21
22
23
24
# File 'lib/poeditor/commands/pull_command.rb', line 17

def get_configuration_file_path(argv)
  config_index = argv.index("-c") || argv.index("--config")
  if config_index
    config_path = argv[config_index + 1]
  else
    config_path = "poeditor.yml"
  end
end

#get_or_raise(yaml, key) ⇒ Object

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

Parameters:

  • yaml (YAML)
  • key (String)

Returns:

  • The value for the specified key from yaml

Raises:



61
62
63
64
# File 'lib/poeditor/commands/pull_command.rb', line 61

def get_or_raise(yaml, key)
  yaml[key] or raise POEditor::Exception.new \
    "Missing configuration key: '#{key}'"
end

#run(argv) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/poeditor/commands/pull_command.rb', line 3

def run(argv)
  UI.puts "Reading configuration"
  configuration = get_configuration(argv)
  UI.puts configuration
  client = POEditor::Core.new(configuration)
  client.pull()
end