Class: Mkwebook::Config

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/mkwebook/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_options = {}) ⇒ Config

Returns a new instance of Config.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mkwebook/config.rb', line 8

def initialize(cli_options = {})
  super(nil)
  @cli_options = cli_options
  @file = find_mkwebook_yaml
  if @file && File.exist?(@file)
    @config = load(@file)
    __setobj__(@config)
  else
    __setobj__(self)
  end
end

Instance Attribute Details

#cli_optionsObject

Returns the value of attribute cli_options.



6
7
8
# File 'lib/mkwebook/config.rb', line 6

def cli_options
  @cli_options
end

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/mkwebook/config.rb', line 6

def config
  @config
end

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/mkwebook/config.rb', line 6

def file
  @file
end

Instance Method Details

#authentication?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mkwebook/config.rb', line 38

def authentication?
  config.dig(:authentication, :cookies).present? || config.dig(:authentication, :local_storage).present?
end

#concurrent?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/mkwebook/config.rb', line 34

def concurrent?
  config[:concurrency].present?
end

#find_mkwebook_yamlObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mkwebook/config.rb', line 42

def find_mkwebook_yaml
  dir = Dir.pwd
  while dir != '/'
    file = File.join(dir, 'mkwebook.yaml')
    return file if File.exist?(file)

    file = File.join(dir, 'mkwebook.yml')
    return file if File.exist?(file)

    dir = File.dirname(dir)
  end
end

#force_single_threaded?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/mkwebook/config.rb', line 55

def force_single_threaded?
  @cli_options[:pause] || @cli_options[:pause_on_error] || @cli_options[:single_thread]
end

#load(config_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mkwebook/config.rb', line 20

def load(config_file)
  default_config = {
    'browser' => {
      'headless' => true,
    },
    'concurrency': 1
  }
  config = YAML.load_file(config_file)
  config = default_config.deep_merge(config).deep_transform_keys! { |k| k.to_s.underscore.to_sym }
  config[:concurrency] = 1 if force_single_threaded?
  config[:browser][:headless] = false if @cli_options[:headmode]
  config
end