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.



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

def initialize
  @region          = nil # no continent filter by default (global dataset)
  @api_key         = nil
  @api_base_url    = "https://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])


36
37
38
# File 'lib/vehicles/configuration.rb', line 36

def aliases
  @aliases
end

#api_base_urlString

ORIGIN of the hosted VehiclesDB API — scheme + host only, no path (endpoint paths carry their own /v1 prefix, and URI.join would drop a path suffix here anyway). Overridable for self-hosting/testing.

Returns:

  • (String)


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

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)


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

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)


56
57
58
# File 'lib/vehicles/configuration.rb', line 56

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)


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

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.



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

def other_label
  @other_label
end

#refresh_timeoutInteger

Network timeout (seconds) for a refresh download.

Returns:

  • (Integer)


63
64
65
# File 'lib/vehicles/configuration.rb', line 63

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)


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

def use_cache
  @use_cache
end