Module: RestRequestor::Config
- Defined in:
- lib/rest_requestor/config.rb
Constant Summary collapse
- CONFIG_TIERS =
Each tier holds candidate paths; the first found in a tier is used for that tier. Tiers are merged least-to-most-specific, so project settings override env-explicit, which override home/global settings. Settings absent from a more-specific tier are inherited from less-specific ones.
[ # Tier 1 — home/global (least specific) [ -> { ENV["XDG_CONFIG_HOME"]&.then { |d| File.join(d, "rest_requestor", "config.yml") } }, -> { File.join(Dir.home, ".config", "rest_requestor", "config.yml") }, -> { File.join(Dir.home, ".rest_requestor_config.yml") } ], # Tier 2 — explicit path via environment variable [ -> { (v = ENV["REST_REQUESTOR_CONFIG"]) && !v.empty? ? v : nil } ], # Tier 3 — project-local (most specific) [ -> { File.join(Dir.pwd, "config", "rest_requestor_config.yml") } ] ].freeze
Class Method Summary collapse
Class Method Details
.load ⇒ Object
28 29 30 31 32 33 |
# File 'lib/rest_requestor/config.rb', line 28 def self.load CONFIG_TIERS.each_with_object({}) do |tier, result| data = first_in_tier(tier) result.merge!(parse(data)) if data end end |
.parse(data) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rest_requestor/config.rb', line 35 def self.parse(data) result = {} if (raw_ua = data["user_agent"] || data[:user_agent]) sanitized = raw_ua.to_s.gsub(/[^A-Za-z0-9_\-]/, "_") result[:user_agent] = "#{sanitized}/#{VERSION}" end if (v = data["max_retries"] || data[:max_retries]) n = v.to_i result[:max_retries] = n if n > 0 end %i[open_timeout read_timeout].each do |key| if (v = data[key.to_s] || data[key]) n = v.to_f result[key] = n if n > 0 end end result end |