Module: Dratools::Config

Defined in:
lib/dratools/config.rb

Overview

Reads advanced configuration from environment variables.

Constant Summary collapse

MAX_RECURSIVE_NON_RUN_XREFS_ENV =
'DRATOOLS_MAX_RECURSIVE_NON_RUN_XREFS'
TREE_MAX_DIRECT_RUNS_ENV =
'DRATOOLS_TREE_MAX_DIRECT_RUNS'
URL_MAX_DIRECT_RUNS_ENV =
'DRATOOLS_URL_MAX_DIRECT_RUNS'
SIZE_MAX_DIRECT_RUNS_ENV =
'DRATOOLS_SIZE_MAX_DIRECT_RUNS'
DOWNLOAD_CONNECT_TIMEOUT_ENV =
'DRATOOLS_DOWNLOAD_CONNECT_TIMEOUT'
DOWNLOAD_STALL_TIMEOUT_ENV =
'DRATOOLS_DOWNLOAD_STALL_TIMEOUT'
DOWNLOAD_STALL_SPEED_ENV =
'DRATOOLS_DOWNLOAD_STALL_SPEED'
DOWNLOAD_RETRY_COUNT_ENV =
'DRATOOLS_DOWNLOAD_RETRY_COUNT'
DOWNLOAD_RETRY_WAIT_ENV =
'DRATOOLS_DOWNLOAD_RETRY_WAIT'
DOWNLOAD_COMMAND_ENV =
'DRATOOLS_DOWNLOAD_COMMAND'
DEFAULT_MAX_RECURSIVE_NON_RUN_XREFS =
500
DEFAULT_TREE_MAX_DIRECT_RUNS =
200
DEFAULT_URL_MAX_DIRECT_RUNS =
200
DEFAULT_SIZE_MAX_DIRECT_RUNS =
200
DEFAULT_DOWNLOAD_CONNECT_TIMEOUT_SECONDS =
30
DEFAULT_DOWNLOAD_STALL_TIMEOUT_SECONDS =
60
DEFAULT_DOWNLOAD_STALL_SPEED_BYTES_PER_SECOND =
1024
DEFAULT_DOWNLOAD_RETRY_COUNT =
3
DEFAULT_DOWNLOAD_RETRY_WAIT_SECONDS =
5
SUPPORTED_DOWNLOAD_COMMANDS =
%w[curl wget aria2c].freeze
UNLIMITED_VALUE =
'unlimited'

Class Method Summary collapse

Class Method Details

.download_commandObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dratools/config.rb', line 75

def download_command
  value = ENV.fetch(DOWNLOAD_COMMAND_ENV, '').strip
  return nil if value.empty?
  return value if SUPPORTED_DOWNLOAD_COMMANDS.include?(value)

  invalid_environment_value!(
    DOWNLOAD_COMMAND_ENV,
    value,
    SUPPORTED_DOWNLOAD_COMMANDS.join(' or ')
  )
end

.download_connect_timeout_secondsObject



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

def download_connect_timeout_seconds
  positive_integer(DOWNLOAD_CONNECT_TIMEOUT_ENV, DEFAULT_DOWNLOAD_CONNECT_TIMEOUT_SECONDS)
end

.download_retry_countObject



67
68
69
# File 'lib/dratools/config.rb', line 67

def download_retry_count
  non_negative_integer(DOWNLOAD_RETRY_COUNT_ENV, DEFAULT_DOWNLOAD_RETRY_COUNT)
end

.download_retry_wait_secondsObject



71
72
73
# File 'lib/dratools/config.rb', line 71

def download_retry_wait_seconds
  positive_integer(DOWNLOAD_RETRY_WAIT_ENV, DEFAULT_DOWNLOAD_RETRY_WAIT_SECONDS)
end

.download_stall_speed_bytes_per_secondObject



63
64
65
# File 'lib/dratools/config.rb', line 63

def download_stall_speed_bytes_per_second
  positive_integer(DOWNLOAD_STALL_SPEED_ENV, DEFAULT_DOWNLOAD_STALL_SPEED_BYTES_PER_SECOND)
end

.download_stall_timeout_secondsObject



59
60
61
# File 'lib/dratools/config.rb', line 59

def download_stall_timeout_seconds
  positive_integer(DOWNLOAD_STALL_TIMEOUT_ENV, DEFAULT_DOWNLOAD_STALL_TIMEOUT_SECONDS)
end

.invalid_environment_value!(name, value, expected) ⇒ Object

Raises:



124
125
126
# File 'lib/dratools/config.rb', line 124

def invalid_environment_value!(name, value, expected)
  raise InvalidOptionError, "invalid #{name} '#{value}' (expected: #{expected})"
end

.max_recursive_non_run_xrefsObject



33
34
35
36
37
38
# File 'lib/dratools/config.rb', line 33

def max_recursive_non_run_xrefs
  positive_integer_or_unlimited(
    MAX_RECURSIVE_NON_RUN_XREFS_ENV,
    DEFAULT_MAX_RECURSIVE_NON_RUN_XREFS
  )
end

.non_negative_integer(name, default) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dratools/config.rb', line 112

def non_negative_integer(name, default)
  value = ENV.fetch(name, '').strip
  return default if value.empty?

  integer = Integer(value, 10)
  return integer unless integer.negative?

  invalid_environment_value!(name, value, 'non-negative integer')
rescue ArgumentError
  invalid_environment_value!(name, value, 'non-negative integer')
end

.positive_integer(name, default) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dratools/config.rb', line 100

def positive_integer(name, default)
  value = ENV.fetch(name, '').strip
  return default if value.empty?

  integer = Integer(value, 10)
  return integer if integer.positive?

  invalid_environment_value!(name, value, 'positive integer')
rescue ArgumentError
  invalid_environment_value!(name, value, 'positive integer')
end

.positive_integer_or_unlimited(name, default) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dratools/config.rb', line 87

def positive_integer_or_unlimited(name, default)
  value = ENV.fetch(name, '').strip
  return default if value.empty?
  return nil if value.casecmp?(UNLIMITED_VALUE)

  integer = Integer(value, 10)
  return integer if integer.positive?

  invalid_environment_value!(name, value, "positive integer or #{UNLIMITED_VALUE}")
rescue ArgumentError
  invalid_environment_value!(name, value, "positive integer or #{UNLIMITED_VALUE}")
end

.size_max_direct_runsObject



51
52
53
# File 'lib/dratools/config.rb', line 51

def size_max_direct_runs
  positive_integer_or_unlimited(SIZE_MAX_DIRECT_RUNS_ENV, DEFAULT_SIZE_MAX_DIRECT_RUNS)
end

.tree_max_direct_runsObject



40
41
42
43
44
45
# File 'lib/dratools/config.rb', line 40

def tree_max_direct_runs
  positive_integer_or_unlimited(
    TREE_MAX_DIRECT_RUNS_ENV,
    DEFAULT_TREE_MAX_DIRECT_RUNS
  )
end

.url_max_direct_runsObject



47
48
49
# File 'lib/dratools/config.rb', line 47

def url_max_direct_runs
  positive_integer_or_unlimited(URL_MAX_DIRECT_RUNS_ENV, DEFAULT_URL_MAX_DIRECT_RUNS)
end