Class: Faker::Vehicle

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/vehicle.rb

Constant Summary collapse

MILEAGE_MIN =
10_000
MILEAGE_MAX =
90_000
VIN_KEYSPACE =
%w[A B C D E F G H J K L M N P R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9].freeze
VIN_TRANSLITERATION =
{ A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9, S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9 }.freeze
VIN_WEIGHT =
[8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2].freeze
SG_CHECKSUM_WEIGHTS =
[3, 14, 2, 12, 2, 11, 1].freeze
SG_CHECKSUM_CHARS =
'AYUSPLJGDBZXTRMKHEC'

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.car_optionsArray<String>

Produces a random list of car options.

Examples:

Faker::Vehicle.car_options #=> ["DVD System", "MP3 (Single Disc)", "Tow Package", "CD (Multi Disc)", "Cassette Player", "Bucket Seats", "Cassette Player", "Leather Interior", "AM/FM Stereo", "Third Row Seats"]

Returns:



207
208
209
# File 'lib/faker/default/vehicle.rb', line 207

def car_options
  Array.new(rand(5...10)) { fetch('vehicle.car_options') }
end

.car_typeString

Produces a random car type.

Examples:

Faker::Vehicle.car_type #=> "Sedan"

Returns:



178
179
180
# File 'lib/faker/default/vehicle.rb', line 178

def car_type
  fetch('vehicle.car_types')
end

.colorString

Produces a random vehicle color.

Examples:

Faker::Vehicle.color #=> "Red"

Returns:



126
127
128
# File 'lib/faker/default/vehicle.rb', line 126

def color
  fetch('vehicle.colors')
end

.doorsInteger Also known as: door_count

Produces a random vehicle door count.

Examples:

Faker::Vehicle.doors #=> 1
Faker::Vehicle.door_count #=> 3

Returns:

  • (Integer)


234
235
236
# File 'lib/faker/default/vehicle.rb', line 234

def doors
  sample(fetch_all('vehicle.doors'))
end

.drive_typeString

Produces a random vehicle drive type.

Examples:

Faker::Vehicle.drive_type #=> "4x2/2-wheel drive"

Returns:



152
153
154
# File 'lib/faker/default/vehicle.rb', line 152

def drive_type
  fetch('vehicle.drive_types')
end

.engineString Also known as: engine_size

Produces a random engine cylinder count.

Examples:

Faker::Vehicle.engine_size #=> 6
Faker::Vehicle.engine #=> 4

Returns:



192
193
194
# File 'lib/faker/default/vehicle.rb', line 192

def engine
  "#{sample(fetch_all('vehicle.doors'))} #{fetch('vehicle.cylinder_engine')}"
end

.fuel_typeString

Produces a random vehicle fuel type.

Examples:

Faker::Vehicle.fuel_type #=> "Diesel"

Returns:



165
166
167
# File 'lib/faker/default/vehicle.rb', line 165

def fuel_type
  fetch('vehicle.fuel_types')
end

.license_plate(state_abbreviation: '') ⇒ String

Produces a random license plate number.

Examples:

Faker::Vehicle.license_plate #=> "DEP-2483"
Faker::Vehicle.license_plate(state_abbreviation: 'FL') #=> "977 UNU"

Parameters:

  • state_abbreviation (String) (defaults to: '')

    Two letter state abbreviation for license plate generation.

Returns:



283
284
285
286
287
288
# File 'lib/faker/default/vehicle.rb', line 283

def license_plate(state_abbreviation: '')
  return regexify(bothify(fetch('vehicle.license_plate'))) if state_abbreviation.empty?

  key = "vehicle.license_plate_by_state.#{state_abbreviation}"
  regexify(bothify(fetch(key)))
end

.makeString

Produces a random vehicle make.

Examples:

Faker::Vehicle.make #=> "Honda"

Returns:



68
69
70
# File 'lib/faker/default/vehicle.rb', line 68

def make
  fetch('vehicle.makes')
end

.make_and_modelString

Produces a random vehicle make and model.

Examples:

Faker::Vehicle.make_and_model #=> "Dodge Charger"

Returns:



98
99
100
101
102
# File 'lib/faker/default/vehicle.rb', line 98

def make_and_model
  m = make

  "#{m} #{model(make_of_model: m)}"
end

.manufacturerString Also known as: manufacture

Produces a random vehicle manufacturer.

Examples:

Faker::Vehicle.manufacturer #=> "Lamborghini"

Returns:



54
55
56
# File 'lib/faker/default/vehicle.rb', line 54

def manufacturer
  fetch('vehicle.manufacturer')
end

.mileage(min: MILEAGE_MIN, max: MILEAGE_MAX) ⇒ Integer Also known as: kilometrage

Produces a random mileage/kilometrage for a vehicle.

Examples:

Faker::Vehicle.mileage #=> 26961
Faker::Vehicle.mileage(min: 50_000) #=> 81557
Faker::Vehicle.mileage(min: 50_000, max: 250_000) #=> 117503
Faker::Vehicle.kilometrage #=> 35378

Parameters:

  • min (Integer) (defaults to: MILEAGE_MIN)

    Specific minimum limit for mileage generation.

  • max (Integer) (defaults to: MILEAGE_MAX)

    Specific maximum limit for mileage generation.

Returns:

  • (Integer)


266
267
268
# File 'lib/faker/default/vehicle.rb', line 266

def mileage(min: MILEAGE_MIN, max: MILEAGE_MAX)
  rand_in_range(min, max)
end

.model(make_of_model: '') ⇒ String

Produces a random vehicle model.

Examples:

Faker::Vehicle.model #=> "A8"
Faker::Vehicle.model(make_of_model: 'Toyota') #=> "Prius"

Parameters:

  • make_of_model (String) (defaults to: '')

    Specific valid vehicle make.

Returns:



83
84
85
86
87
# File 'lib/faker/default/vehicle.rb', line 83

def model(make_of_model: '')
  return fetch("vehicle.models_by_make.#{make}") if make_of_model.empty?

  fetch("vehicle.models_by_make.#{make_of_model}")
end

.singapore_license_plateString

Produces a random license plate number for Singapore.

Examples:

Faker::Vehicle.singapore_license_plate #=> "SLV1854M"

Returns:



299
300
301
302
303
# File 'lib/faker/default/vehicle.rb', line 299

def singapore_license_plate
  key = 'vehicle.license_plate'
  plate_number = regexify(bothify(fetch(key)))
  "#{plate_number}#{singapore_checksum(plate_number)}"
end

.standard_specsArray<String>

Produces a random list of standard specs.

Examples:

Faker::Vehicle.standard_specs #=> ["Full-size spare tire w/aluminum alloy wheel", "Back-up camera", "Carpeted cargo area", "Silver accent IP trim finisher -inc: silver shifter finisher", "Back-up camera", "Water-repellent windshield & front door glass", "Floor carpeting"]

Returns:



220
221
222
# File 'lib/faker/default/vehicle.rb', line 220

def standard_specs
  Array.new(rand(5...10)) { fetch('vehicle.standard_specs') }
end

.styleString

Produces a random vehicle style.

Examples:

Faker::Vehicle.style #=> "ESi"

Returns:



113
114
115
# File 'lib/faker/default/vehicle.rb', line 113

def style
  fetch('vehicle.styles')
end

.transmissionString

Produces a random vehicle transmission.

Examples:

Faker::Vehicle.transmission #=> "Automanual"

Returns:



139
140
141
# File 'lib/faker/default/vehicle.rb', line 139

def transmission
  fetch('vehicle.transmissions')
end

.versionString

Produces a car version

Examples:

Faker::Vehicle.version #=> "40 TFSI Premium"

Returns:



314
315
316
# File 'lib/faker/default/vehicle.rb', line 314

def version
  fetch('vehicle.version')
end

.vinString

Produces a random vehicle VIN number.

Examples:

Faker::Vehicle.vin #=> "LLDWXZLG77VK2LUUF"

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/faker/default/vehicle.rb', line 24

def vin
  generate(:string) do |g|
    g.letter(name: :wmi, ranges: ['100'..'199', '400'..'499', '500'..'599', '700'..'799', '7A0'..'7F9'])
    g.letter(name: :vds, length: 5, ranges: [VIN_KEYSPACE])
    g.computed(name: :checksum, deps: %i[wmi vds model_year plant_code vis]) do |wmi, vds, model_year, plant_code, vis|
      checksum = "#{wmi}#{vds}0#{model_year}#{plant_code}#{vis}".chars.each_with_index.map do |char, i|
        value = (char =~ /\A\d\z/ ? char.to_i : VIN_TRANSLITERATION[char.to_sym])
        value * VIN_WEIGHT[i]
      end.inject(:+) % 11

      if checksum == 10
        'X'
      else
        checksum
      end
    end
    g.letter(name: :model_year, length: 1, ranges: [VIN_KEYSPACE - %w[U Z 0]])
    g.letter(name: :plant_code, length: 1, ranges: [VIN_KEYSPACE])
    g.int(name: :vis, length: 6)
  end
end

.yearInteger

Produces a random car year between 1 and 15 years ago.

Examples:

Faker::Vehicle.year #=> 2008

Returns:

  • (Integer)


248
249
250
# File 'lib/faker/default/vehicle.rb', line 248

def year
  Faker::Time.backward(days: rand_in_range(365, 5475), period: :all, format: '%Y').to_i
end