Class: Vehicles::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/vehicles/configuration.rb

Overview

The single source of truth for every knob. Sensible defaults mean you can use the whole gem without ever touching this — ‘Vehicles.configure` is opt-in.

Vehicles.configure do |config|
  config.region  = :eu
  config.api_key = ENV["VEHICLESDB_API_KEY"]
  config.aliases = { "Chevy" => "Chevrolet" }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vehicles/configuration.rb', line 53

def initialize
  @region          = :eu
  @api_key         = nil
  @api_base_url    = "https://api.vehiclesdb.com"
  @api_timeout     = 2
  @aliases         = {}
  @data_url        = "https://cdn.jsdelivr.net/gh/vehiclesdb/vehiclesdb@latest/dist/vehicles.json"
  @cache_path      = default_cache_path
  @use_cache       = true
  @refresh_timeout = 5
end

Instance Attribute Details

#aliasesObject

Extra make aliases, merged over the built-in ones. Keys are matched forgivingly (case/diacritics-insensitive); values are canonical make names.



31
32
33
# File 'lib/vehicles/configuration.rb', line 31

def aliases
  @aliases
end

#api_base_urlObject

Base URL for the hosted VehiclesDB API. Overridable for self-hosting/testing.



23
24
25
# File 'lib/vehicles/configuration.rb', line 23

def api_base_url
  @api_base_url
end

#api_keyObject

Optional VehiclesDB API key. When set, the hosted provider activates and enriches models with years/images/segments. When nil, everything still works on the bundled data — the gem is standalone first, SDK second.



20
21
22
# File 'lib/vehicles/configuration.rb', line 20

def api_key
  @api_key
end

#api_timeoutObject

Network timeout (seconds) for hosted API calls. Kept short so a slow/missing API never blocks a request — hosted lookups degrade to the local data.



27
28
29
# File 'lib/vehicles/configuration.rb', line 27

def api_timeout
  @api_timeout
end

#cache_pathObject

Where a refreshed dataset is cached on disk. Defaults to the app’s cache dir (Rails) or the system temp dir. The refresh writes here; loads prefer it.



44
45
46
# File 'lib/vehicles/configuration.rb', line 44

def cache_path
  @cache_path
end

#data_urlObject

Where ‘Vehicles.refresh!` pulls the latest dataset. Defaults to the public VehiclesDB data repo via jsDelivr’s CDN (always-latest release tag).



40
41
42
# File 'lib/vehicles/configuration.rb', line 40

def data_url
  @data_url
end

#refresh_timeoutObject

Network timeout (seconds) for a refresh download.



51
52
53
# File 'lib/vehicles/configuration.rb', line 51

def refresh_timeout
  @refresh_timeout
end

#regionObject

Default region for queries. Today the bundled data ships :eu; the API is already region-aware so :us/:gb/etc. are additive, never breaking.



15
16
17
# File 'lib/vehicles/configuration.rb', line 15

def region
  @region
end

#use_cacheObject

Prefer a refreshed (cached) dataset over the bundled one when present. Set false to always use the bundled snapshot (fully offline/deterministic).



48
49
50
# File 'lib/vehicles/configuration.rb', line 48

def use_cache
  @use_cache
end