sipp 0.1.4 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b070afc9f31b4d758cffb81730c1cf2f66c8f77
4
- data.tar.gz: 0172f9196c6f4207143e8c18a8b2dfa5d159f761
3
+ metadata.gz: 950a78c54189d24a5d4a9159c2c83d74647b867b
4
+ data.tar.gz: 3a25d2e6be0a30821d1473db85ae7b31048cd2af
5
5
  SHA512:
6
- metadata.gz: 4f1dd007f766d6ec94dc27e07ee3f5115974a09547b4875bbe13f4625685234d378c34a9df427d25ac63e55895e64db9f0b880ed0a9631e8ea519b12c4ef5ee4
7
- data.tar.gz: ad55e279ed148f69b247afe77300d7d75bfc216720677662a5676a0db9521e6b36eb9bdd75b735e3826a2d651faac05e929b8a2ac46b55eec9b2c370d4f9b6c1
6
+ metadata.gz: a12870aa3d1ee2edba06d09015d08a9b708ab23c61cbafc62293f253f8a4217f87af052b2a41aeb3ff415fe126237364e08580bdd01a67c0037ae4ec48c965f4
7
+ data.tar.gz: 87a73c7cd626016cb6e473584d3ca2c0ad04e564089c785b79d7cf58064b46fa5252eaf59051eab50931aacac4fc24179d448968ce5c820dcb4f6036dea02adb
data/Gemfile CHANGED
@@ -5,4 +5,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
5
  # Specify your gem's dependencies in sipp.gemspec
6
6
  gemspec
7
7
 
8
- gem 'activesupport', '~> 4.1'
8
+ gem 'i18n'
9
+
data/README.md CHANGED
@@ -24,17 +24,138 @@ Or install it yourself as:
24
24
  code = SIPP::Code.new 'CCMN'
25
25
  code.to_s # "Compact - 2/4 Door - Manual Unspecified Drive - Unspecified Fuel/Power Without Air"
26
26
 
27
+ code.category.to_s # Compact
28
+ code.type.to_s # 2/4 Door
29
+ code.transmission_drive.to_s # Manual Unspecified Drive
30
+ code.transmission.to_s # Manual
31
+ code.drive.to_s # Unspecified Drive
32
+ code.transmission_manual? # true
33
+ code.transmission_auto? # false
34
+ code.fuel_ac.to_s # Unspecified Fuel/Power Without Air
35
+ code.fuel.to_s # Unspecified Fuel/Power
36
+ code.ac.to_s # No Air
27
37
  code.ac? # false
28
38
  ```
29
39
  If the code is invalid, it will return `nil` on all checks.
30
40
 
41
+ If only some of the letters are valid, it will translate them:
42
+ ```ruby
43
+ code = SIPP::Code.new '**DR'
44
+ code.valid? # false
45
+ code.to_s # #N/A - #N/A - Auto AWD - Unspecified Fuel/Power With Air
46
+ ```
47
+
31
48
  Calling `code.validate!` will throw validation errors with exceptions.
32
49
 
50
+ ### #as_json
51
+ A convenient hash that lists car capabilities.
52
+ ```ruby
53
+ SIPP::Code.new('CCDD').as_json
54
+ {
55
+ category: :compact,
56
+ type: :two_four_door,
57
+ transmission: :auto,
58
+ drive: :awd,
59
+ fuel: :diesel,
60
+ ac: :air,
61
+ code: "CCDD"
62
+ }
63
+
64
+ ```
65
+
66
+ ## I18n
67
+
68
+ **NOTE**: a simpler version without i18n is kept in `v.1.4`, you can use it as `gem 'sipp', git: 'https://github.com/austerlitz/sipp.git', tag: 'v1.4'`
69
+
70
+ For more flexibility methods like `category`, `drive` etc. return a simple
71
+ Wrapper object to provide either symbol or an i18n-sed string.
72
+ Thus, when `#to_s` is implied, it will return a translated message.
73
+ For use in code or elsewhere, use `#to_sym`:
74
+ ```ruby
75
+ if :diesel == code.fuel.to_sym
76
+ puts 'This car has a diesel engine'
77
+ end
78
+ ```
79
+
80
+ _This seems to be not an elegant solution. If you have any ideas on improving it, please make a pull request._
81
+
82
+
83
+ You can override existing or add your own translations an put them under the
84
+ `sipp` scope:
85
+ ```yaml
86
+ ua:
87
+ sipp:
88
+ category:
89
+ mini: міні
90
+ type:
91
+ convertible: кабріолет
92
+ transmission:
93
+ manual: МКПП
94
+ auto: АКПП
95
+ ```
96
+ ## Inverted
97
+ _A very early beta. But beta than nothin'_
98
+
99
+ It's also able to generate a SIPP code from car capabilities.
100
+ The capabilities are expected in the same format as produced by `#as_json`.
101
+ If something invalid is supplied it generates `'*'`.
102
+
103
+ ```ruby
104
+ SIPP::Inverted.generate({ category: :compact, type: :two_four_door, transmission: :manual, drive: :unspecified, fuel: :petrol, ac: :air})
105
+ # => 'CCMV'
106
+ SIPP::Inverted.generate({ category: :compact, type: :two_four_door, fuel: :petrol, ac: :air })
107
+ # => 'CC*V'
108
+ ```
109
+
110
+ ## Extended
111
+
112
+ Works most similar to Inverted but produces a more consistent approach to encoding car capabilities.
113
+
114
+ Generates an Extended-SIPP code from capabilities provided and translates capabilities list into an Extended-SIPP code.
115
+
116
+ Should be tried and discussed.
117
+
118
+ ```ruby
119
+ SIPP::Extended.new('CCMFDA55').as_json
120
+
121
+ {
122
+ category: :compact,
123
+ type: :two_four_door,
124
+ transmission: :manual,
125
+ drive: :front,
126
+ fuel: :diesel,
127
+ ac: :air,
128
+ doors: 5,
129
+ seats: 5,
130
+ bags_big: :unspecified,
131
+ bags_small: :unspecified,
132
+ }
133
+
134
+ SIPP::Extended.new('CBMFPN2234').as_json
135
+ {
136
+ category: :compact,
137
+ type: :two_three_door,
138
+ transmission: :manual,
139
+ drive: :front,
140
+ fuel: :petrol,
141
+ ac: :no_air,
142
+ doors: 2,
143
+ seats: 2,
144
+ bags_big: 3,
145
+ bags_small: 4,
146
+ }
147
+
148
+ ```
149
+
33
150
  ## TODO
34
- - add pseudo codes
35
- - add van codes
36
- - add i18n helpers or redo strings into symbols to be i18n-ised
37
- - add query methods for common checks (like `.diesel?` etc)
151
+ - [x] add i18n helpers or redo strings into symbols to be i18n-sed
152
+ - [x] add SIPP code generation from car capabilities
153
+ - [ ] add input validation of some kind?
154
+ - [x] add Extended SIPP codes
155
+ - [ ] add more adequate tests
156
+ - [ ] add query methods for common checks (like `.diesel?` etc)
157
+ - [ ] add pseudo codes
158
+ - [ ] add van codes
38
159
 
39
160
 
40
161
  ## Development
data/bin/console CHANGED
@@ -7,8 +7,8 @@ require "sipp"
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
10
+ require "pry"
11
+ Pry.start
12
12
 
13
- require "irb"
14
- IRB.start(__FILE__)
13
+ # require "irb"
14
+ # IRB.start(__FILE__)
@@ -0,0 +1,104 @@
1
+ en:
2
+ sipp:
3
+ category:
4
+ mini: Mini
5
+ mini_elite: Mini Elite
6
+ economy: Economy
7
+ economy_elite: Economy Elite
8
+ compact: Compact
9
+ compact_elite: Compact Elite
10
+ intermediate: Intermediate
11
+ intermediate_elite: Intermediate Elite
12
+ standard: Standard
13
+ standard_elite: Standard Elite
14
+ fullsize: Fullsize
15
+ fullsize_elite: Fullsize Elite
16
+ premium: Premium
17
+ premium_elite: Premium Elite
18
+ luxury: Luxury
19
+ luxury_elite: Luxury Elite
20
+ oversize: Oversize
21
+ special: Special
22
+
23
+ type:
24
+ two_three_door: 2-3 Door
25
+ two_four_door: 2/4 Door
26
+ four_five_door: 4-5 Door
27
+ wagon_estate: Wagon/Estate
28
+ passenger_van: Passenger Van
29
+ limousine_sedan: Limousine/Sedan
30
+ sport: Sport
31
+ convertible: Convertible
32
+ suv: SUV
33
+ open_air_all_terrain: Open Air All Terrain
34
+ special: Special
35
+ pick_up_2_door: Pick up (single/extended cab) 2 door
36
+ pick_up_4_door: Pick up (double cab) 4 door
37
+ special_offer_car: Special Offer Car
38
+ coupe: Coupe
39
+ monospace: Monospace
40
+ recreational_vehicle: Recreational Vehicle
41
+ motor_home: Motor Home
42
+ two_wheel_vehicle: 2 Wheel Vehicle
43
+ roadster: Roadster
44
+ crossover: Crossover
45
+ commercial: Commercial Van/Truck
46
+
47
+ transmission_drive:
48
+ manual_unspecified_drive: Manual Unspecified Drive
49
+ manual_4wd: Manual 4WD
50
+ manual_awd: Manual AWD
51
+ auto_unspecified_drive: Auto Unspecified Drive
52
+ auto_4wd: Auto 4WD
53
+ auto_awd: Auto AWD
54
+
55
+ transmission:
56
+ manual: Manual
57
+ auto: Auto
58
+ robot: Robot
59
+
60
+ drive:
61
+ unspecified: Unspecified
62
+ four_wd: 4WD
63
+ awd: AWD
64
+ single: Single
65
+ front: Front
66
+ rear: Rear
67
+
68
+ fuel_ac:
69
+ unspecified_air: Unspecified Fuel/Power With Air
70
+ unspecified_no_air: Unspecified Fuel/Power Without Air
71
+ diesel_air: Diesel Air
72
+ diesel_no_air: Diesel No Air
73
+ hybrid_air: Hybrid Air
74
+ hybrid_plug_in_air: Hybrid Plug in Air
75
+ electric_air: Electric (Distance < 250mi/400km) Air
76
+ electric_plus_air: Electric Plus (Distance ≥ 250mi/400km) Air
77
+ compressed_gas_air: LPG/Compressed Gas Air
78
+ compressed_gas_no_air: LPG/Compressed Gas No Air
79
+ hydrogen_air: Hydrogen Air
80
+ hydrogen_no_air: Hydrogen No Air
81
+ multi_fuel_air: Multi Fuel/Power Air
82
+ multi_fuel_no_air: Multi fuel/power No Air
83
+ petrol_air: Petrol Air
84
+ petrol_no_air: Petrol No Air
85
+ ethanol_air: Ethanol Air
86
+ ethanol_no_air: Ethanol No Air
87
+
88
+ fuel:
89
+ unspecified: Unspecified Fuel/Power
90
+ diesel: Diesel
91
+ hybrid: Hybrid
92
+ hybrid_plug_in: Hybrid Plug in
93
+ electric: Electric (Distance < 250mi/400km)
94
+ electric_plus: Electric Plus (Distance ≥ 250mi/400km)
95
+ compressed_gas: LPG/Compressed Gas
96
+ hydrogen: Hydrogen
97
+ multi_fuel: Multi fuel/power
98
+ petrol: Petrol
99
+ ethanol: Ethanol
100
+
101
+ ac:
102
+ air: Air
103
+ no_air: No Air
104
+
@@ -0,0 +1,104 @@
1
+ ru:
2
+ sipp:
3
+ category:
4
+ mini: мини
5
+ mini_elite: мини элит
6
+ economy: эконом
7
+ economy_elite: эконом элит
8
+ compact: компакт
9
+ compact_elite: компакт элит
10
+ intermediate: средний
11
+ intermediate_elite: средний элит
12
+ standard: стандартный
13
+ standard_elite: стандартный элит
14
+ fullsize: полноразмерный
15
+ fullsize_elite: полноразмерный элит
16
+ premium: премиум
17
+ premium_elite: premium элит
18
+ luxury: люкс
19
+ luxury_elite: люкс элит
20
+ oversize: оверсайз
21
+ special: специальный
22
+
23
+ type:
24
+ two_three_door: 2-3 двери
25
+ two_four_door: 2/4 двери
26
+ four_five_door: 4-5 двери
27
+ wagon_estate: универсал
28
+ passenger_van: пассажирский фургон
29
+ limousine_sedan: седан
30
+ sport: спорт
31
+ convertible: кабриолет
32
+ suv: SUV
33
+ open_air_all_terrain: внедорожник
34
+ special: специального назначения
35
+ pick_up_2_door: двухдверный пикап
36
+ pick_up_4_door: четырехдверный пикап
37
+ special_offer_car: автомобиль спецпредложения
38
+ coupe: купе
39
+ monospace: однообъемник
40
+ recreational_vehicle: автомобиль для отдыха
41
+ motor_home: дом на колесах
42
+ two_wheel_vehicle: двухколесное транспортное средство
43
+ roadster: родстер
44
+ crossover: кроссовер
45
+ commercial: коммерческий фургон
46
+
47
+ transmission_drive:
48
+ manual_unspecified_drive: МКПП, привод не указан
49
+ manual_4wd: МКПП, 4WD
50
+ manual_awd: МКПП, AWD
51
+ auto_unspecified_drive: АКПП, привод не указан
52
+ auto_4wd: АКПП, 4WD
53
+ auto_awd: АКПП, AWD
54
+
55
+ transmission:
56
+ manual: МКПП
57
+ auto: АКПП
58
+ robot: Робот
59
+
60
+ drive:
61
+ unspecified: привод не указан
62
+ four_wd: 4WD
63
+ awd: AWD
64
+ single: Монопривод
65
+ front: Передний
66
+ rear: Задний
67
+
68
+ fuel_ac:
69
+ unspecified_air: топливо не указано, кондиционер
70
+ unspecified_no_air: топливо не указано, без кондиционера
71
+ diesel_air: дизель, кондиционер
72
+ diesel_no_air: дизель, без кондиционера
73
+ hybrid_air: гибрид, кондиционер
74
+ hybrid_plug_in_air: гибрид с подзарядкой, кондиционер
75
+ electric_air: электрический (до 250 миль/400 км), кондиционер
76
+ electric_plus_air: электрический плюс (от 250 миль/400 км), кондиционер
77
+ compressed_gas_air: сжиженный газ, кондиционер
78
+ compressed_gas_no_air: сжиженный газ, без кондиционера
79
+ hydrogen_air: водород, кондиционер
80
+ hydrogen_no_air: водород, без кондиционера
81
+ multi_fuel_air: мультитопливный, кондиционер
82
+ multi_fuel_no_air: мультитопливный, без кондиционера
83
+ petrol_air: бензин, кондиционер
84
+ petrol_no_air: бензин, без кондиционера
85
+ ethanol_air: спирт, кондиционер
86
+ ethanol_no_air: спирт, без кондиционера
87
+
88
+ fuel:
89
+ unspecified: топливо не указано
90
+ diesel: дизель
91
+ hybrid: гибрид
92
+ hybrid_plug_in: гибрид с подзарядкой
93
+ electric: электрический (до 250 миль/400 км)
94
+ electric_plus: электрический плюс (от 250 миль/400 км)
95
+ compressed_gas: сжиженный газ
96
+ hydrogen: водород
97
+ multi_fuel: мультитопливный
98
+ petrol: бензин
99
+ ethanol: спирт
100
+
101
+ ac:
102
+ air: кондиционер
103
+ no_air: без кондиционера
104
+
data/lib/sipp/code.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'sipp/dictionary'
2
+ require 'sipp/localiser'
2
3
 
3
4
  module SIPP
4
5
  class Code
@@ -27,91 +28,97 @@ module SIPP
27
28
 
28
29
  def category
29
30
  validate_category
30
- CATEGORY[code[0]]
31
+ Category.new CATEGORY[code[0]]
31
32
  rescue CategoryError => e
32
33
  nil
33
34
  end
34
35
 
35
36
  def type
36
37
  validate_type
37
- TYPE[code[1]]
38
+ Type.new TYPE[code[1]]
38
39
  rescue TypeError => e
39
40
  nil
40
41
  end
41
42
 
42
43
  def transmission_drive
43
44
  validate_transmission_drive
44
- TRANSMISSION_DRIVE[code[2]]
45
+ TransmissionDrive.new TRANSMISSION_DRIVE[code[2]]
45
46
  rescue TransmissionDriveError => e
46
47
  nil
47
48
  end
48
49
 
49
50
  def transmission
50
51
  validate_transmission_drive
51
- TRANSMISSION[code[2]]
52
+ Transmission.new TRANSMISSION[code[2]]
52
53
  rescue TransmissionDriveError => e
53
54
  nil
54
55
  end
55
56
 
56
57
  def transmission_manual?
57
58
  validate_transmission_drive
58
- 'Manual' == transmission ? true : false # TODO fix magic string
59
+ :manual == transmission.to_sym ? true : false
59
60
  rescue TransmissionDriveError => e
60
61
  nil
61
62
  end
62
63
 
63
64
  def transmission_auto?
64
65
  validate_transmission_drive
65
- 'Auto' == transmission ? true : false # TODO fix magic string
66
+ :auto == transmission.to_sym ? true : false
66
67
  rescue TransmissionDriveError => e
67
68
  nil
68
69
  end
69
70
 
70
71
  def drive
71
72
  validate_transmission_drive
72
- DRIVE[code[2]]
73
+ Drive.new DRIVE[code[2]]
73
74
  rescue TransmissionDriveError => e
74
75
  nil
75
76
  end
76
77
 
77
78
  def fuel_ac
78
79
  validate_fuel_ac
79
- FUEL_AC[code[3]]
80
+ FuelAC.new FUEL_AC[code[3]]
80
81
  rescue FuelACError => e
81
82
  nil
82
83
  end
83
84
 
84
85
  def fuel
85
86
  validate_fuel_ac
86
- FUEL[code[3]]
87
+ Fuel.new FUEL[code[3]]
87
88
  rescue FuelACError => e
88
89
  nil
89
90
  end
90
91
 
91
92
  def ac
92
93
  validate_fuel_ac
93
- AC[code[3]]
94
+ Ac.new AC[code[3]]
94
95
  rescue FuelACError => e
95
96
  nil
96
97
  end
97
98
 
98
99
  def ac?
99
100
  validate_fuel_ac
100
- 'Air' == ac ? true : false # TODO fix magic string
101
+ :air == ac.to_sym ? true : false
101
102
  rescue FuelACError => e
102
103
  nil
103
104
  end
104
105
 
106
+ SEPARATOR = ' - '
107
+
105
108
  def to_s
106
- [category, type, transmission_drive, fuel_ac].map{|c| c.nil? ? '#N/A' : c}.join(' - ')
109
+ [category, type, transmission_drive, fuel_ac].map{|c| c.nil? ? '#N/A' : c}.join(SEPARATOR)
107
110
  rescue CodeError, CategoryError, TypeError, TransmissionDriveError, FuelACError
108
111
  ''
109
112
  end
110
113
 
114
+ def as_json(options = nil)
115
+ [category, type, transmission, drive, fuel, ac].compact.map(&:as_json).inject(&:merge).merge({code: @code})
116
+ end
117
+
111
118
  private
112
119
 
113
120
  def validate_code
114
- if @code.blank? || 4 != @code.length
121
+ if !@code || @code.empty? || 4 != @code.length
115
122
  raise CodeError, :invalid_code
116
123
  else
117
124
  true
@@ -119,7 +126,7 @@ module SIPP
119
126
  end
120
127
 
121
128
  def validate_category
122
- if @code[0].blank? || !CATEGORY.keys.include?(@code[0])
129
+ if !@code[0] || @code[0].empty? || !CATEGORY.keys.include?(@code[0])
123
130
  raise CategoryError, :invalid_category
124
131
  else
125
132
  true
@@ -127,7 +134,7 @@ module SIPP
127
134
  end
128
135
 
129
136
  def validate_type
130
- if @code[1].blank? || !TYPE.keys.include?(@code[1])
137
+ if !@code[1] || @code[1].empty? || !TYPE.keys.include?(@code[1])
131
138
  raise TypeError, :invalid_type
132
139
  else
133
140
  true
@@ -135,7 +142,7 @@ module SIPP
135
142
  end
136
143
 
137
144
  def validate_transmission_drive
138
- if @code[2].blank? || !TRANSMISSION_DRIVE.keys.include?(@code[2])
145
+ if !@code[2] || @code[2].empty? || !TRANSMISSION_DRIVE.keys.include?(@code[2])
139
146
  raise TransmissionDriveError, :invalid_transmission_drive
140
147
  else
141
148
  true
@@ -143,7 +150,7 @@ module SIPP
143
150
  end
144
151
 
145
152
  def validate_fuel_ac
146
- if @code[3].blank? || !FUEL_AC.keys.include?(@code[3])
153
+ if !@code[3] || @code[3].empty? || !FUEL_AC.keys.include?(@code[3])
147
154
  raise FuelACError, :invalid_fuel_ac
148
155
  else
149
156
  true
@@ -1,138 +1,138 @@
1
1
  module SIPP
2
2
  module Dictionary
3
3
  CATEGORY = {
4
- 'M' => 'Mini',
5
- 'N' => 'Mini Elite',
6
- 'E' => 'Economy',
7
- 'H' => 'Economy Elite',
8
- 'C' => 'Compact',
9
- 'D' => 'Compact Elite',
10
- 'I' => 'Intermediate',
11
- 'J' => 'Intermediate Elite',
12
- 'S' => 'Standard',
13
- 'R' => 'Standard Elite',
14
- 'F' => 'Fullsize',
15
- 'G' => 'Fullsize Elite',
16
- 'P' => 'Premium',
17
- 'U' => 'Premium Elite',
18
- 'L' => 'Luxury',
19
- 'W' => 'Luxury Elite',
20
- 'O' => 'Oversize',
21
- 'X' => 'Special',
4
+ 'M' => :mini,
5
+ 'N' => :mini_elite,
6
+ 'E' => :economy,
7
+ 'H' => :economy_elite,
8
+ 'C' => :compact,
9
+ 'D' => :compact_elite,
10
+ 'I' => :intermediate,
11
+ 'J' => :intermediate_elite,
12
+ 'S' => :standard,
13
+ 'R' => :standard_elite,
14
+ 'F' => :fullsize,
15
+ 'G' => :fullsize_elite,
16
+ 'P' => :premium,
17
+ 'U' => :premium_elite,
18
+ 'L' => :luxury,
19
+ 'W' => :luxury_elite,
20
+ 'O' => :oversize,
21
+ 'X' => :special,
22
22
  }
23
23
 
24
24
  TYPE = {
25
- 'B' => '2-3 Door',
26
- 'C' => '2/4 Door',
27
- 'D' => '4-5 Door',
28
- 'W' => 'Wagon/Estate',
29
- 'V' => 'Passenger Van',
30
- 'L' => 'Limousine/Sedan',
31
- 'S' => 'Sport',
32
- 'T' => 'Convertible',
33
- 'F' => 'SUV',
34
- 'J' => 'Open Air All Terrain',
35
- 'X' => 'Special',
36
- 'P' => 'Pick up (single/extended cab) 2 door',
37
- 'Q' => 'Pick up (double cab) 4 door',
38
- 'Z' => 'Special Offer Car',
39
- 'E' => 'Coupe',
40
- 'M' => 'Monospace',
41
- 'R' => 'Recreational Vehicle',
42
- 'H' => 'Motor Home',
43
- 'Y' => '2 Wheel Vehicle',
44
- 'N' => 'Roadster',
45
- 'G' => 'Crossover',
46
- 'K' => 'Commercial Van/Truck',
25
+ 'B' => :two_three_door,
26
+ 'C' => :two_four_door,
27
+ 'D' => :four_five_door,
28
+ 'W' => :wagon_estate,
29
+ 'V' => :passenger_van,
30
+ 'L' => :limousine_sedan,
31
+ 'S' => :sport,
32
+ 'T' => :convertible,
33
+ 'F' => :suv,
34
+ 'J' => :open_air_all_terrain,
35
+ 'X' => :special,
36
+ 'P' => :pick_up_2_door,
37
+ 'Q' => :pick_up_4_door,
38
+ 'Z' => :special_offer_car,
39
+ 'E' => :coupe,
40
+ 'M' => :monospace,
41
+ 'R' => :recreational_vehicle,
42
+ 'H' => :motor_home,
43
+ 'Y' => :two_wheel_vehicle,
44
+ 'N' => :roadster,
45
+ 'G' => :crossover,
46
+ 'K' => :commercial,
47
47
  }
48
48
 
49
49
  TRANSMISSION_DRIVE = {
50
- 'M' => 'Manual Unspecified Drive',
51
- 'N' => 'Manual 4WD',
52
- 'C' => 'Manual AWD',
53
- 'A' => 'Auto Unspecified Drive',
54
- 'B' => 'Auto 4WD',
55
- 'D' => 'Auto AWD',
50
+ 'M' => :manual_unspecified_drive,
51
+ 'N' => :manual_4wd,
52
+ 'C' => :manual_awd,
53
+ 'A' => :auto_unspecified_drive,
54
+ 'B' => :auto_4wd,
55
+ 'D' => :auto_awd,
56
56
  }
57
- TRANSMISSION = {
58
- 'M' => 'Manual',
59
- 'N' => 'Manual',
60
- 'C' => 'Manual',
61
- 'A' => 'Auto',
62
- 'B' => 'Auto',
63
- 'D' => 'Auto',
57
+ TRANSMISSION = {
58
+ 'M' => :manual,
59
+ 'N' => :manual,
60
+ 'C' => :manual,
61
+ 'A' => :auto,
62
+ 'B' => :auto,
63
+ 'D' => :auto,
64
64
  }
65
65
 
66
66
  DRIVE = {
67
- 'M' => 'Unspecified',
68
- 'N' => '4WD',
69
- 'C' => 'AWD',
70
- 'A' => 'Unspecified',
71
- 'B' => '4WD',
72
- 'D' => 'AWD',
67
+ 'M' => :unspecified,
68
+ 'N' => :four_wd,
69
+ 'C' => :awd,
70
+ 'A' => :unspecified,
71
+ 'B' => :four_wd,
72
+ 'D' => :awd,
73
73
  }
74
74
 
75
75
  FUEL_AC = {
76
- 'R' => 'Unspecified Fuel/Power With Air',
77
- 'N' => 'Unspecified Fuel/Power Without Air',
78
- 'D' => 'Diesel Air',
79
- 'Q' => 'Diesel No Air',
80
- 'H' => 'Hybrid Air',
81
- 'I' => 'Hybrid Plug in Air',
82
- 'E' => 'Electric (Distance < 250mi/400km) Air',
83
- 'C' => 'Electric Plus (Distance ≥ 250mi/400km) Air',
84
- 'L' => 'LPG/Compressed Gas Air',
85
- 'S' => 'LPG/Compressed Gas No Air',
86
- 'A' => 'Hydrogen Air',
87
- 'B' => 'Hydrogen No Air',
88
- 'M' => 'Multi Fuel/Power Air',
89
- 'F' => 'Multi fuel/power No Air',
90
- 'V' => 'Petrol Air',
91
- 'Z' => 'Petrol No Air',
92
- 'U' => 'Ethanol Air',
93
- 'X' => 'Ethanol No Air',
76
+ 'R' => :unspecified_air,
77
+ 'N' => :unspecified_no_air,
78
+ 'D' => :diesel_air,
79
+ 'Q' => :diesel_no_air,
80
+ 'H' => :hybrid_air,
81
+ 'I' => :hybrid_plug_in_air,
82
+ 'E' => :electric_air,
83
+ 'C' => :electric_plus_air,
84
+ 'L' => :compressed_gas_air,
85
+ 'S' => :compressed_gas_no_air,
86
+ 'A' => :hydrogen_air,
87
+ 'B' => :hydrogen_no_air,
88
+ 'M' => :multi_fuel_air,
89
+ 'F' => :multi_fuel_no_air,
90
+ 'V' => :petrol_air,
91
+ 'Z' => :petrol_no_air,
92
+ 'U' => :ethanol_air,
93
+ 'X' => :ethanol_no_air
94
94
  }
95
95
 
96
96
  FUEL = {
97
- 'R' => 'Unspecified Fuel/Power',
98
- 'N' => 'Unspecified Fuel/Power',
99
- 'D' => 'Diesel',
100
- 'Q' => 'Diesel',
101
- 'H' => 'Hybrid',
102
- 'I' => 'Hybrid Plug in',
103
- 'E' => 'Electric (Distance < 250mi/400km)',
104
- 'C' => 'Electric Plus (Distance ≥ 250mi/400km)',
105
- 'L' => 'LPG/Compressed Gas',
106
- 'S' => 'LPG/Compressed Gas',
107
- 'A' => 'Hydrogen',
108
- 'B' => 'Hydrogen',
109
- 'M' => 'Multi Fuel/Power',
110
- 'F' => 'Multi fuel/power',
111
- 'V' => 'Petrol',
112
- 'Z' => 'Petrol',
113
- 'U' => 'Ethanol',
114
- 'X' => 'Ethanol',
97
+ 'R' => :unspecified,
98
+ 'N' => :unspecified,
99
+ 'D' => :diesel,
100
+ 'Q' => :diesel,
101
+ 'H' => :hybrid,
102
+ 'I' => :hybrid_plug_in,
103
+ 'E' => :electric,
104
+ 'C' => :electric_plus,
105
+ 'L' => :compressed_gas,
106
+ 'S' => :compressed_gas,
107
+ 'A' => :hydrogen,
108
+ 'B' => :hydrogen,
109
+ 'M' => :multi_fuel,
110
+ 'F' => :multi_fuel,
111
+ 'V' => :petrol,
112
+ 'Z' => :petrol,
113
+ 'U' => :ethanol,
114
+ 'X' => :ethanol
115
115
  }
116
116
 
117
117
  AC = {
118
- 'R' => 'Air',
119
- 'N' => 'No Air',
120
- 'D' => 'Air',
121
- 'Q' => 'No Air',
122
- 'H' => 'Air',
123
- 'I' => 'Air',
124
- 'E' => 'Air',
125
- 'C' => 'Air',
126
- 'L' => 'Air',
127
- 'S' => 'No Air',
128
- 'A' => 'Air',
129
- 'B' => 'No Air',
130
- 'M' => 'Air',
131
- 'F' => 'No Air',
132
- 'V' => 'Air',
133
- 'Z' => 'No Air',
134
- 'U' => 'Air',
135
- 'X' => 'No Air',
118
+ 'R' => :air,
119
+ 'N' => :no_air,
120
+ 'D' => :air,
121
+ 'Q' => :no_air,
122
+ 'H' => :air,
123
+ 'I' => :air,
124
+ 'E' => :air,
125
+ 'C' => :air,
126
+ 'L' => :air,
127
+ 'S' => :no_air,
128
+ 'A' => :air,
129
+ 'B' => :no_air,
130
+ 'M' => :air,
131
+ 'F' => :no_air,
132
+ 'V' => :air,
133
+ 'Z' => :no_air,
134
+ 'U' => :air,
135
+ 'X' => :no_air
136
136
  }
137
137
  end
138
138
  end
@@ -0,0 +1,8 @@
1
+ module SIPP
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SIPP
4
+ initializer 'SIPP', before: :load_config_initializers do
5
+ Rails.application.config.i18n.load_path += Dir["#{config.root}/config/locales/**/*.yml"]
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,109 @@
1
+ require 'sipp/extended_dictionary'
2
+ module SIPP
3
+
4
+ class Extended
5
+ include ExtendedDictionary
6
+
7
+ def self.generate(capabilities)
8
+ category = CATEGORY[capabilities[:category]]
9
+ type = TYPE[capabilities[:type]]
10
+ transmission = TRANSMISSION[capabilities[:transmission]]
11
+ drive = DRIVE[capabilities[:drive]]
12
+ fuel = FUEL[capabilities[:fuel]]
13
+ ac = AC[capabilities[:ac]]
14
+ capabilities[:bags_big] = capabilities.delete(:bags) unless capabilities[:bags].nil?
15
+ %i[doors seats bags_big bags_small].each do |cap|
16
+ capabilities[cap] = nil if :unspecified == capabilities[cap]
17
+ end
18
+
19
+ [
20
+ category, type, transmission, drive, fuel, ac,
21
+ capabilities[:doors],
22
+ capabilities[:seats],
23
+ (capabilities[:bags_big] || capabilities[:bags]),
24
+ capabilities[:bags_small],
25
+ ].map{|cap| cap || '*'}.join
26
+ end
27
+
28
+
29
+ attr_reader :code
30
+ def initialize(code = nil)
31
+ @code = code.to_s.strip.upcase
32
+ .ljust(10, '*') # pad everything absent as :unspecified
33
+ end
34
+
35
+
36
+
37
+ def category
38
+ Category.new CATEGORY.invert[code[0]]
39
+ end
40
+
41
+ def type
42
+ Type.new TYPE.invert[code[1]]
43
+ end
44
+
45
+ def transmission
46
+ Transmission.new TRANSMISSION.invert[code[2]]
47
+ end
48
+ def drive
49
+ Drive.new DRIVE.invert[code[3]]
50
+ end
51
+ def fuel
52
+ Fuel.new FUEL.invert[code[4]]
53
+ end
54
+ def ac
55
+ Ac.new AC.invert[code[5]]
56
+ end
57
+ def doors
58
+ result = code[6]&.to_i
59
+ if result.zero?
60
+ :unspecified
61
+ else
62
+ result
63
+ end
64
+ end
65
+ def seats
66
+ result = code[7]&.to_i
67
+ if result.zero?
68
+ :unspecified
69
+ else
70
+ result
71
+ end
72
+ end
73
+ def bags
74
+ result = code[8]&.to_i
75
+ if result.zero?
76
+ :unspecified
77
+ else
78
+ result
79
+ end
80
+ end
81
+ alias_method :bags_big, :bags
82
+ def bags_small
83
+ result = code[9]&.to_i
84
+ if result.zero?
85
+ :unspecified
86
+ else
87
+ result
88
+ end
89
+ end
90
+
91
+ def to_s
92
+ [
93
+ category, type, transmission, drive, fuel, ac,
94
+ doors, seats, bags_big, bags_small,
95
+ ].join(Code::SEPARATOR)
96
+ end
97
+
98
+ def as_json(options = nil)
99
+
100
+ [category, type, transmission, drive, fuel, ac].compact.map(&:as_json).inject(&:merge).merge(
101
+ {
102
+ doors: doors,
103
+ seats: seats,
104
+ bags_big: bags,
105
+ bags_small: bags_small
106
+ })
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,57 @@
1
+ module SIPP
2
+ WILDCARDS = {
3
+ nil => '*',
4
+ :unspecified => '*',
5
+ }
6
+ module ExtendedDictionary
7
+ # SIPP-compatible category
8
+ CATEGORY = SIPP::Code::CATEGORY.invert.merge(WILDCARDS)
9
+
10
+ # SIPP-compatible body type
11
+ TYPE = SIPP::Code::TYPE.invert.merge(WILDCARDS)
12
+
13
+ # Transmission type. SIPP-compatible but extended with robot/tiptronic (I wonder if anyone needs it)
14
+ TRANSMISSION = {
15
+ manual: 'M',
16
+ auto: 'A',
17
+ robot: 'R', # variations of robot/tiptronic etc
18
+ }.merge(WILDCARDS)
19
+
20
+ # Drive type. Incompatible with SIPP because the original SIPP is completely weird and more transmission-oriented
21
+ DRIVE = {
22
+ single: 'S',
23
+ front: 'F',
24
+ rear: 'R',
25
+ all: 'A', # AWD
26
+ fwd: 'X', # 4x4
27
+ }.merge(WILDCARDS)
28
+
29
+ # Fuel type. Mostly compatible with SIPP but you know... Made more human-guessable
30
+ FUEL = {
31
+ compressed_gas: "G",
32
+ diesel: "D",
33
+ electric: "E",
34
+ electric_plus: "C",
35
+ ethanol: "U",
36
+ hybrid: "H",
37
+ hybrid_plug_in: "I",
38
+ hydrogen: "A",
39
+ multi_fuel: "M",
40
+ petrol: "P",
41
+ }.merge(WILDCARDS)
42
+
43
+ # Most probably is subject to change in the future
44
+ # Surprisingly is compatible with SIPP
45
+ AC = {
46
+ multi_zone: 'M',
47
+ present: 'A',
48
+ absent: 'N',
49
+ false => 'N',
50
+ true => 'A',
51
+ air: 'A',
52
+ no_air: 'N',
53
+ }.merge(WILDCARDS)
54
+
55
+ end
56
+ end
57
+
@@ -0,0 +1,15 @@
1
+ require 'sipp/inverted_dictionary'
2
+ module SIPP
3
+ class Inverted
4
+ include InvertedDictionary
5
+
6
+ def self.generate(capabilities)
7
+ category = CATEGORY[capabilities[:category]]
8
+ type = TYPE[capabilities[:type]]
9
+ transmission_drive = TRANSMISSION_DRIVE[[capabilities[:transmission], (capabilities[:drive] || :unspecified)]]
10
+ fuel_ac = FUEL_AC[[(capabilities[:fuel] || :unspecified), capabilities[:ac]]]
11
+
12
+ [category, type, transmission_drive, fuel_ac].map{|cap| cap || '*'}.join
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ module SIPP
2
+ module InvertedDictionary
3
+ CATEGORY = SIPP::Code::CATEGORY.invert
4
+
5
+ TYPE = SIPP::Code::TYPE.invert
6
+
7
+ TRANSMISSION_DRIVE = {
8
+ [:manual, :unspecified] => "M",
9
+ [:manual, :four_wd] => "N",
10
+ [:manual, :awd] => "C",
11
+ [:auto, :unspecified] => "A",
12
+ [:auto, :four_wd] => "B",
13
+ [:auto, :awd] => "D"
14
+ }
15
+
16
+ FUEL_AC = {
17
+ [:unspecified, :air] => "R",
18
+ [:unspecified, :no_air] => "N",
19
+ [:diesel, :air] => "D",
20
+ [:diesel, :no_air] => "Q",
21
+ [:hybrid, :air] => "H",
22
+ [:hybrid_plug_in, :air] => "I",
23
+ [:electric, :air] => "E",
24
+ [:electric_plus, :air] => "C",
25
+ [:compressed_gas, :air] => "L",
26
+ [:compressed_gas, :no_air] => "S",
27
+ [:hydrogen, :air] => "A",
28
+ [:hydrogen, :no_air] => "B",
29
+ [:multi_fuel, :air] => "M",
30
+ [:multi_fuel, :no_air] => "F",
31
+ [:petrol, :air] => "V",
32
+ [:petrol, :no_air] => "Z",
33
+ [:ethanol, :air] => "U",
34
+ [:ethanol, :no_air] => "X",
35
+ }
36
+ end
37
+ end
38
+
@@ -0,0 +1,8 @@
1
+ module SIPP
2
+ module Localiser
3
+ def translate(scope, item)
4
+ I18n.translate("sipp.#{scope}.#{item}")
5
+ end
6
+ alias_method :t, :translate
7
+ end
8
+ end
data/lib/sipp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SIPP
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.7"
3
3
  end
data/lib/sipp.rb CHANGED
@@ -1,15 +1,60 @@
1
1
  require "sipp/version"
2
2
 
3
+ require "sipp/localiser"
3
4
  module SIPP
4
- class Error < StandardError; end
5
- class CodeError < StandardError; end
6
- class CategoryError < StandardError; end
7
- class TypeError < StandardError; end
8
- class TransmissionDriveError < StandardError; end
9
- class FuelACError < StandardError; end
10
- # Your code goes here...
11
- end
5
+ Error = Class.new StandardError
6
+ CodeError = Class.new Error
7
+ CategoryError = Class.new Error
8
+ TypeError = Class.new Error
9
+ TransmissionDriveError = Class.new Error
10
+ FuelACError = Class.new Error
11
+
12
+ class SIPPCodeWrapper
13
+ include Localiser
14
+
15
+ def initialize(symbol)
16
+ @sym = symbol
17
+ end
18
+
19
+ def to_s
20
+ t klass_name, @sym
21
+ end
22
+
23
+ def klass_name
24
+ self.class.name.split('::').last
25
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
26
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
27
+ .tr(' ', '_')
28
+ .downcase
29
+ end
12
30
 
13
- require 'active_support/core_ext/object'
31
+ def to_sym
32
+ @sym
33
+ end
34
+
35
+ def as_json
36
+ { klass_name.to_sym => @sym }
37
+ end
38
+ end
39
+
40
+ Category = Class.new SIPPCodeWrapper
41
+ Type = Class.new SIPPCodeWrapper
42
+ TransmissionDrive = Class.new SIPPCodeWrapper
43
+ Transmission = Class.new SIPPCodeWrapper
44
+ Drive = Class.new SIPPCodeWrapper
45
+ FuelAC = Class.new SIPPCodeWrapper
46
+ Fuel = Class.new SIPPCodeWrapper
47
+ Ac = Class.new SIPPCodeWrapper
48
+ end
14
49
  require "sipp/code"
15
- require "sipp/dictionary"
50
+ require "sipp/dictionary"
51
+ require "sipp/inverted"
52
+ require "sipp/inverted_dictionary"
53
+ require "sipp/extended"
54
+ require "sipp/extended_dictionary"
55
+ require 'i18n'
56
+ I18n.load_path += Dir[File.expand_path("config/locales") + "/*.yml"]
57
+ # I18n.available_locales = [:en, :ru]
58
+ if defined?(Rails)
59
+ require 'sipp/engine'
60
+ end
data/sipp.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 1.17"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "pry"
29
30
 
30
- spec.add_dependency "activesupport", "~> 4.1"
31
+ spec.add_dependency "i18n"
31
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sipp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Buslaev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-04 00:00:00.000000000 Z
11
+ date: 2022-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,19 +53,33 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: activesupport
56
+ name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
60
67
  - !ruby/object:Gem::Version
61
- version: '4.1'
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: i18n
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
62
76
  type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - "~>"
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '4.1'
82
+ version: '0'
69
83
  description: Translates car SIPP codes into something meaningful
70
84
  email:
71
85
  - max@buslaev.net
@@ -83,9 +97,17 @@ files:
83
97
  - Rakefile
84
98
  - bin/console
85
99
  - bin/setup
100
+ - config/locales/en.yml
101
+ - config/locales/ru.yml
86
102
  - lib/sipp.rb
87
103
  - lib/sipp/code.rb
88
104
  - lib/sipp/dictionary.rb
105
+ - lib/sipp/engine.rb
106
+ - lib/sipp/extended.rb
107
+ - lib/sipp/extended_dictionary.rb
108
+ - lib/sipp/inverted.rb
109
+ - lib/sipp/inverted_dictionary.rb
110
+ - lib/sipp/localiser.rb
89
111
  - lib/sipp/version.rb
90
112
  - sipp.gemspec
91
113
  homepage: https://github.com/austerlitz/sipp