Class: Vehicles::Configuration

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

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   # optional default continent filter
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.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vehicles/configuration.rb', line 63

def initialize
  @region          = nil # no continent filter by default (global dataset)
  @api_key         = nil
  @api_base_url    = "https://api.vehiclesdb.com"
  @api_timeout     = 2
  @aliases         = {}
  @other_label     = "Other"
  @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

#aliasesHash[String, String]

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

Returns:

  • (Hash[String, String])


34
35
36
# File 'lib/vehicles/configuration.rb', line 34

def aliases
  @aliases
end

#api_base_urlString

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

Returns:

  • (String)


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

def api_base_url
  @api_base_url
end

#api_keyString?

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.

Returns:

  • (String, nil)


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

def api_key
  @api_key
end

#api_timeoutInteger

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.

Returns:

  • (Integer)


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

def api_timeout
  @api_timeout
end

#cache_pathString

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.

Returns:

  • (String)


54
55
56
# File 'lib/vehicles/configuration.rb', line 54

def cache_path
  @cache_path
end

#data_urlString

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

Returns:

  • (String)


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

def data_url
  @data_url
end

#other_labelObject

Label for the "Other / not in the list" escape-hatch option, used by the include_other: helpers and the allow_other: validators. Defaults to "Other"; set it to your UI language (e.g. "Otro", "Autre") so the option reads naturally. Matching is forgiving: the canonical slug "other" is always accepted too, so a stored "Other" validates even if you later relabel it.



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

def other_label
  @other_label
end

#refresh_timeoutInteger

Network timeout (seconds) for a refresh download.

Returns:

  • (Integer)


61
62
63
# File 'lib/vehicles/configuration.rb', line 61

def refresh_timeout
  @refresh_timeout
end

#regionSymbol

Optional default CONTINENT filter for make/model queries (:eu/:na/:as/:sa/:oc/:af). nil (the default) means "no filter — the whole global dataset". Set it to scope an app to one market without passing region: on every call. (Pre-1.0 this defaulted to :eu when the data was EU-only; the global dataset makes "everything" the honest default.)

Returns:

  • (Symbol)


18
19
20
# File 'lib/vehicles/configuration.rb', line 18

def region
  @region
end

#use_cacheBoolean

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

Returns:

  • (Boolean)


58
59
60
# File 'lib/vehicles/configuration.rb', line 58

def use_cache
  @use_cache
end