Class: IGMarkets::CLI::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/cli/config_file.rb

Overview

Helper class for working with the YAML config files supported by the command-line client. A config file has a ‘profiles` root key and every subkey of this root key specifies the name of a profile. The values underneath each subkey are then interpreted as command-line arguments. The profile to use is chosen using the `–profiles` command-line argument.

See ‘README.md` for further details.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = {}) ⇒ ConfigFile

Initializes this config file with the passed content.

Parameters:

  • content (Hash) (defaults to: {})


13
14
15
16
17
18
19
20
21
# File 'lib/ig_markets/cli/config_file.rb', line 13

def initialize(content = {})
  @content = content || {}

  @profiles = (@content['profiles'] || {}).transform_values do |profile_arguments|
    profile_arguments.map do |argument, value|
      "--#{argument}=#{value}"
    end
  end
end

Class Method Details

.find(*config_files) ⇒ ConfigFile

Takes a list of potential config files and returns a IGMarkets::CLI::ConfigFile instance for the first one that exists.

Returns:



39
40
41
42
43
44
45
# File 'lib/ig_markets/cli/config_file.rb', line 39

def self.find(*config_files)
  config_file = config_files.detect do |filename|
    File.exist? filename
  end

  new(config_file && YAML.load_file(config_file))
end

Instance Method Details

#prepend_profile_arguments_to_argv(argv) ⇒ Object

Inserts the arguments specified in this config file into the passed arguments array.

Parameters:

  • argv (Array<String>)

    The array of command-line arguments to alter.



26
27
28
29
30
31
32
33
34
# File 'lib/ig_markets/cli/config_file.rb', line 26

def prepend_profile_arguments_to_argv(argv)
  profile = selected_profile argv

  insert_index = argv.index do |argument|
    argument[0] == '-'
  end || -1

  argv.insert insert_index, *@profiles.fetch(profile, [])
end