Class: Fizzy::Config
- Inherits:
-
Object
- Object
- Fizzy::Config
- Defined in:
- lib/fizzy/config.rb
Overview
Configuration for the Fizzy API client.
Constant Summary collapse
- DEFAULT_BASE_URL =
Default values
"https://fizzy.do"- DEFAULT_TIMEOUT =
30- DEFAULT_MAX_RETRIES =
3- DEFAULT_BASE_DELAY =
1.0- DEFAULT_MAX_JITTER =
0.1- DEFAULT_MAX_PAGES =
10_000
Instance Attribute Summary collapse
-
#base_delay ⇒ Float
Initial backoff delay in seconds.
-
#base_url ⇒ String
API base URL.
-
#max_jitter ⇒ Float
Maximum jitter to add to delays in seconds.
-
#max_pages ⇒ Integer
Maximum pages to fetch in paginated requests.
-
#max_retries ⇒ Integer
Maximum retry attempts for GET requests.
-
#timeout ⇒ Integer
Request timeout in seconds.
Class Method Summary collapse
-
.from_env ⇒ Config
Creates a Config from environment variables.
-
.from_file(path) ⇒ Config
Loads configuration from a JSON file, with environment overrides.
-
.global_config_dir ⇒ String
Returns the default global config directory.
Instance Method Summary collapse
-
#initialize(base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, base_delay: DEFAULT_BASE_DELAY, max_jitter: DEFAULT_MAX_JITTER, max_pages: DEFAULT_MAX_PAGES) ⇒ Config
constructor
Creates a new configuration with the given options.
-
#load_from_env ⇒ self
Loads environment variable overrides into this config.
Constructor Details
#initialize(base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, base_delay: DEFAULT_BASE_DELAY, max_jitter: DEFAULT_MAX_JITTER, max_pages: DEFAULT_MAX_PAGES) ⇒ Config
Creates a new configuration with the given options.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fizzy/config.rb', line 55 def initialize( base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, base_delay: DEFAULT_BASE_DELAY, max_jitter: DEFAULT_MAX_JITTER, max_pages: DEFAULT_MAX_PAGES ) @base_url = normalize_url(base_url) @timeout = timeout @max_retries = max_retries @base_delay = base_delay @max_jitter = max_jitter @max_pages = max_pages unless @base_url == normalize_url(DEFAULT_BASE_URL) || localhost?(@base_url) Fizzy::Security.require_https!(@base_url, "base URL") end validate! end |
Instance Attribute Details
#base_delay ⇒ Float
Returns initial backoff delay in seconds.
31 32 33 |
# File 'lib/fizzy/config.rb', line 31 def base_delay @base_delay end |
#base_url ⇒ String
Returns API base URL.
22 23 24 |
# File 'lib/fizzy/config.rb', line 22 def base_url @base_url end |
#max_jitter ⇒ Float
Returns maximum jitter to add to delays in seconds.
34 35 36 |
# File 'lib/fizzy/config.rb', line 34 def max_jitter @max_jitter end |
#max_pages ⇒ Integer
Returns maximum pages to fetch in paginated requests.
37 38 39 |
# File 'lib/fizzy/config.rb', line 37 def max_pages @max_pages end |
#max_retries ⇒ Integer
Returns maximum retry attempts for GET requests.
28 29 30 |
# File 'lib/fizzy/config.rb', line 28 def max_retries @max_retries end |
#timeout ⇒ Integer
Returns request timeout in seconds.
25 26 27 |
# File 'lib/fizzy/config.rb', line 25 def timeout @timeout end |
Class Method Details
.from_env ⇒ Config
Creates a Config from environment variables.
Environment variables:
-
FIZZY_BASE_URL: API base URL
-
FIZZY_TIMEOUT: Request timeout in seconds
-
FIZZY_MAX_RETRIES: Maximum retry attempts
84 85 86 87 88 89 90 |
# File 'lib/fizzy/config.rb', line 84 def self.from_env new( base_url: ENV.fetch("FIZZY_BASE_URL", DEFAULT_BASE_URL), timeout: ENV.fetch("FIZZY_TIMEOUT", DEFAULT_TIMEOUT).to_i, max_retries: ENV.fetch("FIZZY_MAX_RETRIES", DEFAULT_MAX_RETRIES).to_i ) end |
.from_file(path) ⇒ Config
Loads configuration from a JSON file, with environment overrides.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fizzy/config.rb', line 96 def self.from_file(path) data = JSON.parse(File.read(path)) config = new( base_url: data["base_url"] || DEFAULT_BASE_URL, timeout: data["timeout"] || DEFAULT_TIMEOUT, max_retries: data["max_retries"] || DEFAULT_MAX_RETRIES ) config.load_from_env config rescue Errno::ENOENT from_env end |
.global_config_dir ⇒ String
Returns the default global config directory.
122 123 124 125 |
# File 'lib/fizzy/config.rb', line 122 def self.global_config_dir config_dir = ENV["XDG_CONFIG_HOME"] || File.join(Dir.home, ".config") File.join(config_dir, "fizzy") end |
Instance Method Details
#load_from_env ⇒ self
Loads environment variable overrides into this config.
111 112 113 114 115 116 117 118 |
# File 'lib/fizzy/config.rb', line 111 def load_from_env @base_url = normalize_url(ENV["FIZZY_BASE_URL"]) if ENV["FIZZY_BASE_URL"] @timeout = ENV["FIZZY_TIMEOUT"].to_i if ENV["FIZZY_TIMEOUT"] @max_retries = ENV["FIZZY_MAX_RETRIES"].to_i if ENV["FIZZY_MAX_RETRIES"] Fizzy::Security.require_https!(@base_url, "base URL") unless localhost?(@base_url) validate! self end |