Class: Pipl::Vehicle
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#display ⇒ Object
Returns the value of attribute display.
-
#make ⇒ Object
Returns the value of attribute make.
-
#model ⇒ Object
Returns the value of attribute model.
-
#vehicle_type ⇒ Object
Returns the value of attribute vehicle_type.
-
#vin ⇒ Object
Returns the value of attribute vin.
-
#year ⇒ Object
Returns the value of attribute year.
Attributes inherited from Field
#current, #inferred, #last_seen, #valid_since
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Vehicle
constructor
A new instance of Vehicle.
- #is_searchable? ⇒ Boolean
- #to_hash ⇒ Object
- #validate_vin(vin) ⇒ Object
- #validate_vin_checksum(vin) ⇒ Object
Methods inherited from Field
base_params_from_hash, extra_metadata, from_hash
Methods included from Utils
alnum_chars, alpha_chars, date_to_str, extract_rate_limits, is_valid_url?, str_to_date, titleize, to_utf8
Constructor Details
#initialize(params = {}) ⇒ Vehicle
Returns a new instance of Vehicle.
740 741 742 743 744 745 746 747 748 |
# File 'lib/pipl/fields.rb', line 740 def initialize(params={}) @vin = params[:vin] @year = params[:year] @make = params[:make] @model = params[:model] @color = params[:color] @vehicle_type = params[:vehicle_type] @display = params[:display] end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def color @color end |
#display ⇒ Object
Returns the value of attribute display.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def display @display end |
#make ⇒ Object
Returns the value of attribute make.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def make @make end |
#model ⇒ Object
Returns the value of attribute model.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def model @model end |
#vehicle_type ⇒ Object
Returns the value of attribute vehicle_type.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def vehicle_type @vehicle_type end |
#vin ⇒ Object
Returns the value of attribute vin.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def vin @vin end |
#year ⇒ Object
Returns the value of attribute year.
738 739 740 |
# File 'lib/pipl/fields.rb', line 738 def year @year end |
Instance Method Details
#is_searchable? ⇒ Boolean
798 799 800 |
# File 'lib/pipl/fields.rb', line 798 def is_searchable? validate_vin(@vin) end |
#to_hash ⇒ Object
750 751 752 753 |
# File 'lib/pipl/fields.rb', line 750 def to_hash {vin: @vin, year: @year, make: @make, model: @model, color: @color, vehicle_type: @vehicle_type} .reject { |_, value| value.nil? } end |
#validate_vin(vin) ⇒ Object
755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/pipl/fields.rb', line 755 def validate_vin(vin) vin_valid = true if vin vin_valid = vin.length == 17 && !(vin.downcase.chars.to_set & %w[i o q]).any? && !%w[u z 0].include?(vin[9].downcase) && vin.match?(/\A\w+\z/) vin_valid &&= validate_vin_checksum(vin) if vin_valid end vin_valid end |
#validate_vin_checksum(vin) ⇒ Object
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
# File 'lib/pipl/fields.rb', line 767 def validate_vin_checksum(vin) vin = vin.downcase check_digit = vin[8] return false if check_digit.nil? # Handle the case when check_digit is nil replace_map = { "1" => ["a", "j"], "2" => ["b", "k", "s"], "3" => ["c", "l", "t"], "4" => ["d", "m", "u"], "5" => ["e", "n", "v"], "6" => ["f", "w"], "7" => ["g", "p", "x"], "8" => ["h", "y"], "9" => ["r", "z"] } positional_weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2] replace_map.each do |digit, replacements| replacements.each do |c| vin.gsub!(c, digit) end end checksum = vin.chars.each_with_index.reject { |_, i| i == 8 }.sum { |num, i| num.to_i * positional_weights[i] } % 11 checksum = 'x' if checksum == 10 checksum.to_s == check_digit.to_s # Convert check_digit to string for comparison end |