Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion edmunds.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Gem::Specification.new do |spec|

spec.add_dependency 'faraday'
spec.add_dependency 'json'
spec.add_dependency 'active_support'
# spec.add_dependency 'active_support'
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is used in the request.rb class

require 'active_support/core_ext/hash'

When I bundle it fails to install but I know I have it... I'm confused.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this up on master.

end
3 changes: 3 additions & 0 deletions lib/edmunds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
require_relative 'edmunds/vehicle/specification/option'
require_relative 'edmunds/vehicle/specification/vin_decoding'
require_relative 'edmunds/vehicle/specification/equipment'
require_relative 'edmunds/vehicle/specification/transmission'
require_relative 'edmunds/vehicle/specification/engine'
require_relative 'edmunds/vehicle/specification/drivetrain'

module Edmunds
end
4 changes: 2 additions & 2 deletions lib/edmunds/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Exception < StandardError
def initialize(response)
error = JSON.parse(response.body)

@error_type = error['error']['errorType']
@message = error['error']['message']
@error_type = error['errorType']
@message = error['message']
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/edmunds/api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Request

def initialize(path)
@path = path
@allowed_parameters = {}
@default_parameters = {}
end

def parameters
Expand All @@ -22,6 +24,7 @@ def get

def sanitized_parameters
@allowed_parameters = @allowed_parameters.with_indifferent_access

params = @raw_parameters.with_indifferent_access

# merge in default parameters
Expand All @@ -41,13 +44,15 @@ def sanitized_parameters
end

def check_required(params)
return params if params.empty?
@required_parameters.each do |param|
raise "missing required parameter #{param}" if params[param].blank?
end
params
end

def check_values(params)
return params if params.empty?
params.each do |param, value|
raise "bad parameter value #{value} for param #{param}" unless valid_param?(param, value)
end
Expand Down
38 changes: 28 additions & 10 deletions lib/edmunds/vehicle/specification/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ module Specification
module Color

class ColorsByStyle
attr_reader :colors, :count
attr_reader :exterior, :interior #, :colors, :count

def initialize(attributes)
@colors = attributes['colors'].map { |json| ColorsDetails.new(json) } if attributes.key?('colors')
@count = attributes['colorsCount']
# @colors = attributes['colors'].map { |json| ColorsDetails.new(json) } if attributes.key?('colors')
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabrouy Sorry for the dumb question but is this the result of a more recent API change?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it was different in the past. Right now doing .map over colors just loops over a couple of keys (interior and exterior) not the actual colors.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'm fine with that as long as the test works out fine.

# ['colors'] is actually a size 2 Array that contains interior colors in position 0 and exterior colors in position 1
@interior, @exterior = [], []
@interior = attributes['colors'][0]['options'].map { |json| InteriorColor.new(json) } if attributes['colors'][0].present?
@exterior = attributes['colors'][1]['options'].map { |json| ExteriorColor.new(json) } if attributes['colors'][1].present?

# @count = attributes['colorsCount'] # Could not find this key in response
end

def self.find(style_id, api_params = {})
Expand All @@ -24,17 +29,15 @@ def self.find(style_id, api_params = {})
end
end

class ColorsDetails
class Color
attr_reader :id,
:name,
:equipment_type,
:availability,
:manufacture_option_name,
:manufacture_option_code,
:category,
:attributes, #not implemented
:color_chips, #not_implemented
:fabric_Types #not implemented
:manufacture_option_code,
:color_chips,
:attributes #not implemented

def initialize(attributes)
@id = attributes['id']
Expand All @@ -43,7 +46,7 @@ def initialize(attributes)
@availability = attributes['availability']
@manufacture_option_name = attributes['manufactureOptionName']
@manufacture_option_code = attributes['manufactureOptionCode']
@category = attributes['category']
@color_chips = attributes['colorChips']
end

def self.find(color_id, api_params = {})
Expand All @@ -53,6 +56,21 @@ def self.find(color_id, api_params = {})
end
end

class InteriorColor < Color
attr_reader :fabric_types

def initialize(attributes)
super(attributes)
@fabric_types = attributes['fabricTypes']
end
end

class ExteriorColor < Color
def initialize(attributes)
super(attributes)
end
end

end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/edmunds/vehicle/specification/drivetrain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Edmunds
module Vehicle
module Specification
module Drivetrain
class Drivetrain

def initialize(name)
@name = name
end

def name(view = :long)
if view == :short
case @name
when "front wheel drive" then "FWD"
when "rear wheel drive" then "RWD"
when "all wheel drive" then "AWD"
when "four wheel drive" then "4WD"
end
else
@name
end
end
end
end
end
end
end
21 changes: 19 additions & 2 deletions lib/edmunds/vehicle/specification/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,28 @@ def self.find(style_id, api_params = {})
end

class Engine
attr_reader :id
attr_reader :code, :compression_ratio, :compressor_type, :configuration, :cylinder, :displacement, :equipment_type, :fuel_type,
:horsepower, :id, :manufacturer_engine_code, :name, :rpm, :size, :torque, :total_valves, :type, :valve

def initialize(attributes)
@code = attributes['code']
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finishing this for me.

@compression_ratio = attributes['compressionRatio']
@compressor_type = attributes['compressorType']
@configuration = attributes['configuration']
@cylinder = attributes['cylinder']
@displacement = attributes['displacement']
@equipment_type = attributes['equipmentType']
@fuel_type = attributes['fuelType']
@horsepower = attributes['horsepower']
@id = attributes['id']
#TODO
@manufacturer_engine_code = attributes['manufacturerEngineCode']
@name = attributes['name']
@rpm = attributes['rpm']
@size = attributes['size']
@torque = attributes['torque']
@total_valves = attributes['totalValves']
@type = attributes['type']
@valve = attributes['valve']
end

def self.find(engine_id, api_params = {})
Expand Down
26 changes: 21 additions & 5 deletions lib/edmunds/vehicle/specification/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@ module Specification
module Option

class OptionsByStyle
attr_reader :options,
:count
attr_reader :interior, :exterior, :roof, :interior_trim, :mechanical, :package, :additional_fees, :other

def initialize(attributes)
@options = attributes['options'].map { |json| Option.new(json) } if attributes.key?('options')
@count = attributes['optionsCount']
@interior, @exterior, @roof, @interior_trim = [], [], [], []
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable.

@mechanical, @package, @aditional_fees, @other = [], [], [], []

attributes['options'].each do |category_with_option|
category = category_with_option['category']
options = category_with_option['options']
if category == "Interior" then @interior = options.map { |json| Option.new(json) }
elsif category == "Exterior" then @exterior = options.map { |json| Option.new(json) }
elsif category == "Roof" then @roof = options.map { |json| Option.new(json) }
elsif category == "Interior Trim" then @interior_trim = options.map { |json| Option.new(json) }
elsif category == "Mechanical" then @mechanical = options.map { |json| Option.new(json) }
elsif category == "Package" then @package = options.map { |json| Option.new(json) }
elsif category == "Additional Fees" then @additional_fees = options.map { |json| Option.new(json) }
elsif category == "Other" then @other = options.map { |json| Option.new(json) }
end
end
# @options = attributes['options'].map { |json| Option.new(json) } if attributes.key?('options')
# @count = attributes['optionsCount']
end

def self.find(style_id, api_params = {})
Expand Down Expand Up @@ -46,9 +61,10 @@ def initialize(attributes)
@availability = attributes['availability']
@manufacture_option_name = attributes['manufactureOptionName']
@manufacture_option_code = attributes['manufactureOptionCode']
@equipment = attributes['equipment']
@category = attributes['category']
@attributes = attributes['attributes']
@equipment = attributes['equipment']
@price = attributes['price']
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end

def self.find(option_id, api_params = {})
Expand Down
39 changes: 27 additions & 12 deletions lib/edmunds/vehicle/specification/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,32 @@ module Vehicle
module Specification
module Style
class Style
attr_reader :id, :name, :trim, :body, :year, :make_name, :model_name

def initialize(attributes)
@id = attributes['id']
@name = attributes['name']
@trim = attributes['trim']
@body = attributes['submodel']['body']
# TODO: Not sure whether this is valuable or not to expose...
# @year = attributes["year"]["year"]
# @make_name = attributes["make"]["name"]
# @model_name = attributes["model"]["name"]
attr_reader :id, :name, :year, :make, :model, :trim, :body, :engine, :transmission, :driven_wheels,
:doors, :colors, :options, :manufacturer_code, :price, :categories, :states, :squish_vins, :mpg

def initialize(attributes, view = nil)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor this to use the new params style?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, what would be the new params style? Like adding that 'view' option inside the attributes hash?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/Sovietaced/edmunds/blob/master/lib/edmunds/vehicle/specification/make.rb#L19-L36

I have completed the refactor that supported optional params. In this case "view" is an optional parameter.

Instead of instantiating the object and storing the "view" enum I would just handle engine, transmission, driven_wheels if there are found in the hash.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh gotcha! 👍

@id = attributes.try(:[], 'id')
@name = attributes.try(:[], 'name')
@year = attributes.try(:[], 'year').try(:[], 'year')
@make = Edmunds::Vehicle::Specification::Make::Make.new(attributes.try(:[], 'make')) if attributes.try(:[], 'make')
@model = Edmunds::Vehicle::Specification::Model::Model.new(attributes.try(:[], 'model')) if attributes.try(:[], 'model')
@trim = attributes.try(:[], 'trim')
@body = attributes.try(:[], 'submodel').try(:[], 'body')

if view == 'full'
@engine = Edmunds::Vehicle::Specification::Engine::Engine.new(attributes.try(:[], 'engine')) if attributes.try(:[], 'engine')
@transmission = Edmunds::Vehicle::Specification::Transmission::Transmission.new(attributes.try(:[], 'transmission')) if attributes.try(:[], 'transmission')
@driven_wheels = Edmunds::Vehicle::Specification::Drivetrain::Drivetrain.new(attributes.try(:[], 'drivenWheels')) if attributes.try(:[], 'drivenWheels')
@doors = attributes.try(:[], 'numOfDoors')
@options = Edmunds::Vehicle::Specification::Option::OptionsByStyle.new(attributes)
@colors = Edmunds::Vehicle::Specification::Color::ColorsByStyle.new(attributes)
@manufacturer_code = attributes.try(:[], 'manufacturerCode')
@price = attributes.try(:[], 'price')
@categories = attributes.try(:[], 'categories')
@states = attributes.try(:[], 'states')
@squish_vins = attributes.try(:[], 'squishVins')
@mpg = attributes.try(:[], 'MPG')
end
end

def self.find(id, api_params = {})
Expand All @@ -37,7 +52,7 @@ def self.find(id, api_params = {})
end

attributes = JSON.parse(response.body)
new(attributes)
new(attributes, api_params[:view])
end
end

Expand Down
21 changes: 21 additions & 0 deletions lib/edmunds/vehicle/specification/transmission.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Edmunds
module Vehicle
module Specification
module Transmission
class Transmission
attr_reader :id, :name, :equipment_type, :availability, :automatic_type, :transmission_type, :number_of_speeds

def initialize(attributes)
@id = attributes['id']
@name = attributes['name']
@equipment_type = attributes['equipmentType']
@availability = attributes['availability']
@automatic_type = attributes['automaticType']
@transmission_type = attributes['transmissionType']
@number_of_speeds = attributes['numberOfSpeeds']
end
end
end
end
end
end
24 changes: 17 additions & 7 deletions lib/edmunds/vehicle/specification/vin_decoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Basic
attr_reader :make, :model, :year

def initialize(attributes)
@make = attributes['make']['name']
@model = attributes['model']['name']
@year = attributes['year']
@make = attributes.try(:[], 'make').try(:[], 'name')
@model = attributes.try(:[], 'model').try(:[], 'name')
@year = attributes.try(:[], 'year')
end

# Get vehicle make, model, year, type, fuel type, number of cylinders and list of styles by decoding the vehicle's VIN.
Expand All @@ -32,14 +32,24 @@ def self.find(vin, api_params = {})
end

class Full
attr_reader :make, :model, :years, :mpg_highway, :mpg_city
attr_reader :make,
:model,
:engine,
:transmission,
:categories,
:years,
:trim,
:description,
:mpg_highway,
:mpg_city

def initialize(attributes)
@make = Edmunds::Vehicle::Specification::Make::Make.new(attributes['make'])
@model = Edmunds::Vehicle::Specification::Model::Model.new(attributes['model'])
@years = attributes['years'].map { |json| Edmunds::Vehicle::Specification::ModelYear::ModelYear.new(json) } if attributes.key?('years')
@mpg_highway = attributes['MPG']['highway']
@mpg_city = attributes['MPG']['city']
@categories = attributes.fetch('categories', {})
@years = attributes.fetch('years', []).map { |json| Edmunds::Vehicle::Specification::ModelYear::ModelYear.new(json) } if attributes.key?('years')
@mpg_highway = attributes.fetch('MPG', {}).fetch('highway', {})
@mpg_city = attributes.fetch('MPG', {}).fetch('city', {})
end

# Get all vehicle details from make, model, year and fuel type to list of options, features and standard equipment. All this information is returned by decoding the vehicle's VIN.
Expand Down