Class: CardDB::Configuration
- Inherits:
-
Object
- Object
- CardDB::Configuration
- Defined in:
- lib/carddb/configuration.rb
Overview
Configuration class for CardDB client settings.
Constant Summary collapse
- DEFAULT_ENDPOINT =
Default API endpoint
'https://carddb.xtda.org/query'- DEFAULT_TIMEOUT =
Default timeouts in seconds
30- DEFAULT_OPEN_TIMEOUT =
10- DEFAULT_MAX_RETRIES =
Default retry settings
3
Instance Attribute Summary collapse
-
#allowed_games ⇒ Hash<String, Array<String>>?
Map of publisher slug to allowed game keys.
-
#allowed_publishers ⇒ Array<String>?
List of allowed publisher slugs (nil = all allowed).
-
#api_key ⇒ String?
API key for authentication.
-
#cache ⇒ Object?
Cache instance (must respond to read/write, e.g., Rails.cache or MemoryCache).
-
#cache_ttl ⇒ Integer
Default cache TTL in seconds (default: 300 = 5 minutes).
-
#cache_ttls ⇒ Hash<Symbol, Integer>
Per-resource cache TTLs in seconds Keys are resource names (:publishers, :games, :datasets, :records) Values override the default cache_ttl for that resource.
-
#default_game ⇒ String?
Default game key for queries.
-
#default_publisher ⇒ String?
Default publisher slug for queries.
-
#endpoint ⇒ String
GraphQL endpoint URL.
-
#log_level ⇒ Symbol
Log level (:debug, :info, :warn, :error).
-
#logger ⇒ Logger?
Logger instance for debug output.
-
#max_retries ⇒ Integer
Maximum number of retries on rate limit.
-
#open_timeout ⇒ Integer
Connection open timeout in seconds.
-
#retry_on_rate_limit ⇒ Boolean
Whether to automatically retry on rate limit errors.
-
#timeout ⇒ Integer
Request timeout in seconds.
Instance Method Summary collapse
-
#cache_ttl_for(resource) ⇒ Integer
Get the cache TTL for a specific resource.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#merge(overrides = {}) ⇒ Configuration
Creates a copy of this configuration with optional overrides.
-
#resolve_game(game_key) ⇒ String?
Resolves the game key, using the default if not provided.
-
#resolve_publisher(publisher_slug) ⇒ String?
Resolves the publisher slug, using the default if not provided.
-
#validate_access!(publisher_slug, game_key = nil) ⇒ void
Validates both publisher and game if provided.
-
#validate_game!(publisher_slug, game_key) ⇒ void
Validates that a game is allowed by the configuration.
-
#validate_publisher!(publisher_slug) ⇒ void
Validates that a publisher is allowed by the configuration.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/carddb/configuration.rb', line 91 def initialize @endpoint = DEFAULT_ENDPOINT @timeout = DEFAULT_TIMEOUT @open_timeout = DEFAULT_OPEN_TIMEOUT @api_key = nil @default_publisher = nil @default_game = nil @allowed_publishers = nil @allowed_games = nil @logger = nil @log_level = :info @retry_on_rate_limit = false @max_retries = DEFAULT_MAX_RETRIES @cache = nil @cache_ttl = 300 @cache_ttls = {} end |
Instance Attribute Details
#allowed_games ⇒ Hash<String, Array<String>>?
Returns Map of publisher slug to allowed game keys.
66 67 68 |
# File 'lib/carddb/configuration.rb', line 66 def allowed_games @allowed_games end |
#allowed_publishers ⇒ Array<String>?
Returns List of allowed publisher slugs (nil = all allowed).
63 64 65 |
# File 'lib/carddb/configuration.rb', line 63 def allowed_publishers @allowed_publishers end |
#api_key ⇒ String?
Returns API key for authentication.
45 46 47 |
# File 'lib/carddb/configuration.rb', line 45 def api_key @api_key end |
#cache ⇒ Object?
Returns Cache instance (must respond to read/write, e.g., Rails.cache or MemoryCache).
81 82 83 |
# File 'lib/carddb/configuration.rb', line 81 def cache @cache end |
#cache_ttl ⇒ Integer
Returns Default cache TTL in seconds (default: 300 = 5 minutes).
84 85 86 |
# File 'lib/carddb/configuration.rb', line 84 def cache_ttl @cache_ttl end |
#cache_ttls ⇒ Hash<Symbol, Integer>
Returns Per-resource cache TTLs in seconds Keys are resource names (:publishers, :games, :datasets, :records) Values override the default cache_ttl for that resource.
89 90 91 |
# File 'lib/carddb/configuration.rb', line 89 def cache_ttls @cache_ttls end |
#default_game ⇒ String?
Returns Default game key for queries.
60 61 62 |
# File 'lib/carddb/configuration.rb', line 60 def default_game @default_game end |
#default_publisher ⇒ String?
Returns Default publisher slug for queries.
57 58 59 |
# File 'lib/carddb/configuration.rb', line 57 def default_publisher @default_publisher end |
#endpoint ⇒ String
Returns GraphQL endpoint URL.
48 49 50 |
# File 'lib/carddb/configuration.rb', line 48 def endpoint @endpoint end |
#log_level ⇒ Symbol
Returns Log level (:debug, :info, :warn, :error).
72 73 74 |
# File 'lib/carddb/configuration.rb', line 72 def log_level @log_level end |
#logger ⇒ Logger?
Returns Logger instance for debug output.
69 70 71 |
# File 'lib/carddb/configuration.rb', line 69 def logger @logger end |
#max_retries ⇒ Integer
Returns Maximum number of retries on rate limit.
78 79 80 |
# File 'lib/carddb/configuration.rb', line 78 def max_retries @max_retries end |
#open_timeout ⇒ Integer
Returns Connection open timeout in seconds.
54 55 56 |
# File 'lib/carddb/configuration.rb', line 54 def open_timeout @open_timeout end |
#retry_on_rate_limit ⇒ Boolean
Returns Whether to automatically retry on rate limit errors.
75 76 77 |
# File 'lib/carddb/configuration.rb', line 75 def retry_on_rate_limit @retry_on_rate_limit end |
#timeout ⇒ Integer
Returns Request timeout in seconds.
51 52 53 |
# File 'lib/carddb/configuration.rb', line 51 def timeout @timeout end |
Instance Method Details
#cache_ttl_for(resource) ⇒ Integer
Get the cache TTL for a specific resource. Falls back to the default cache_ttl if no resource-specific TTL is configured.
114 115 116 |
# File 'lib/carddb/configuration.rb', line 114 def cache_ttl_for(resource) cache_ttls[resource.to_sym] || cache_ttl end |
#merge(overrides = {}) ⇒ Configuration
Creates a copy of this configuration with optional overrides.
177 178 179 180 181 182 183 |
# File 'lib/carddb/configuration.rb', line 177 def merge(overrides = {}) dup.tap do |config| overrides.each do |key, value| config.public_send(:"#{key}=", value) if config.respond_to?(:"#{key}=") end end end |
#resolve_game(game_key) ⇒ String?
Resolves the game key, using the default if not provided.
169 170 171 |
# File 'lib/carddb/configuration.rb', line 169 def resolve_game(game_key) game_key || default_game end |
#resolve_publisher(publisher_slug) ⇒ String?
Resolves the publisher slug, using the default if not provided.
161 162 163 |
# File 'lib/carddb/configuration.rb', line 161 def resolve_publisher(publisher_slug) publisher_slug || default_publisher end |
#validate_access!(publisher_slug, game_key = nil) ⇒ void
This method returns an undefined value.
Validates both publisher and game if provided.
152 153 154 155 |
# File 'lib/carddb/configuration.rb', line 152 def validate_access!(publisher_slug, game_key = nil) validate_publisher!(publisher_slug) if publisher_slug validate_game!(publisher_slug, game_key) if publisher_slug && game_key end |
#validate_game!(publisher_slug, game_key) ⇒ void
This method returns an undefined value.
Validates that a game is allowed by the configuration.
136 137 138 139 140 141 142 143 144 |
# File 'lib/carddb/configuration.rb', line 136 def validate_game!(publisher_slug, game_key) return if allowed_games.nil? return unless allowed_games.key?(publisher_slug) allowed = allowed_games[publisher_slug] return if allowed.include?(game_key) raise RestrictedError, "Game '#{game_key}' is not allowed for publisher '#{publisher_slug}'" end |
#validate_publisher!(publisher_slug) ⇒ void
This method returns an undefined value.
Validates that a publisher is allowed by the configuration.
123 124 125 126 127 128 |
# File 'lib/carddb/configuration.rb', line 123 def validate_publisher!(publisher_slug) return if allowed_publishers.nil? return if allowed_publishers.include?(publisher_slug) raise RestrictedError, "Publisher '#{publisher_slug}' is not in the allowed publishers list" end |