Class: Car

Inherits:
Object
  • Object
show all
Defined in:
lib/car.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_year, make, car_model) ⇒ Car

Returns a new instance of Car.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/car.rb', line 5

def initialize(model_year, make, car_model)
	@make = make.capitalize
	@model = car_model
	@model_year = model_year
  @base_url = "http://autos.aol.com/cars-#{make}-#{car_model}-#{model_year}"
	@overview_url = "#{@base_url}/overview"
	@trims_url = "#{@base_url}/available-trims/"
	@specs_url = "#{@base_url}/specs"
	@mpg_url = "#{@specs_url}/mpg"
	@trims = Array.new
	
	Struct.new(
   "ExteriorStats", 
   :length,
	  :body_width,
	  :body_height, 
	  :wheelbase, 
	  :ground_clearance, 
	  :curb, 
	  :gross_weight
	  )
 
Struct.new(
  "InteriorStats", 
 :front_head_room,
  :rear_head_room,
  :front_shoulder_room,
  :rear_shoulder_room,
  :front_hip_room,
  :rear_hip_room,
  :front_leg_room,
  :rear_leg_room,
  :luggage_capacity,
  :maximum_cargo_capacity,
  :standard_seating
  )

Struct.new(
  "PerformanceStats",
  :base_engine_size,
  :base_engine_type,
  :horsepower,
  :horsepower_rpm,
  :torque,
  :torque_rpm,
  :payload,
  :maximum_towing_capacity,
  :drive_type,
  :turning_radius
  )

Struct.new(
  "FuelEconomyStats",
  :fuel_tank_capacity,
  :epa_mileage_estimates
  )
  
	get_overview
	get_specs
	get_features
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



2
3
4
# File 'lib/car.rb', line 2

def base_url
  @base_url
end

#details_urlObject

Returns the value of attribute details_url.



2
3
4
# File 'lib/car.rb', line 2

def details_url
  @details_url
end

#makeObject

Returns the value of attribute make.



2
3
4
# File 'lib/car.rb', line 2

def make
  @make
end

#modelObject

Returns the value of attribute model.



2
3
4
# File 'lib/car.rb', line 2

def model
  @model
end

#model_yearObject

Returns the value of attribute model_year.



2
3
4
# File 'lib/car.rb', line 2

def model_year
  @model_year
end

#overviewObject

Returns the value of attribute overview.



2
3
4
# File 'lib/car.rb', line 2

def overview
  @overview
end

#overview_urlObject

Returns the value of attribute overview_url.



2
3
4
# File 'lib/car.rb', line 2

def overview_url
  @overview_url
end

#photo_urlObject

Returns the value of attribute photo_url.



2
3
4
# File 'lib/car.rb', line 2

def photo_url
  @photo_url
end

#specs_urlObject

Returns the value of attribute specs_url.



2
3
4
# File 'lib/car.rb', line 2

def specs_url
  @specs_url
end

#trimsObject

Returns the value of attribute trims.



2
3
4
# File 'lib/car.rb', line 2

def trims
  @trims
end

#trims_urlObject

Returns the value of attribute trims_url.



2
3
4
# File 'lib/car.rb', line 2

def trims_url
  @trims_url
end

Instance Method Details

#find_trim_by_name(trim_name) ⇒ Object



215
216
217
# File 'lib/car.rb', line 215

def find_trim_by_name(trim_name)
  @trims.select { |trim| trim_name.include? trim.trim_name   }
end

#get_featuresObject



201
202
203
204
205
206
207
208
209
# File 'lib/car.rb', line 201

def get_features
  car_page = Nokogiri::HTML(open(@trims_url))
  
  available_tables = car_page.xpath("//table[@class = 'availableTable']")
  available_tables.each do |table|
    trim_text  = table.xpath(".//div[@class = 'headrow']/a")[0].text
    trim = find_trim_by_name(trim_text)
  end
end

#get_overviewObject



67
68
69
70
71
72
73
74
75
# File 'lib/car.rb', line 67

def get_overview
	car_page = Nokogiri::HTML(open(@overview_url))
  
  if car_page.xpath("//title").text == "Site Map - AOL Autos"
    raise ModelDoesNotExist
  end
  
  @overview = car_page.xpath("//div[@class = 'rv_box']").text
end

#get_specsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/car.rb', line 77

def get_specs
  car_page = Nokogiri::HTML(open(@specs_url))
  
  if car_page.xpath("//title").text == "Site Map - AOL Autos"
    raise ModelDoesNotExist
  end
  trim_list = car_page.xpath("//div[@class = 'sliding_columns_container']")

  category_name_table = car_page.xpath("//div[@class = 'ymm_data_table']/ul[@class = 'data_column']/li/span")

  i = 0    
  exterior_stats_names = []
  interior_stats_names = []
  performance_stats_names = []
  mpg_stats_names = []
  sub_category_name = ""
  category_name = category_name_table[i].text
  if category_name == "Exterior"
    i+=1
    until sub_category_name == "Interior"
      if sub_category_name != "";exterior_stats_names << sub_category_name; end
      sub_category_name = category_name_table[i].text
      i+=1
    end
    i+=1
    sub_category_name = category_name_table[i].text
    until sub_category_name == "Performance"
      interior_stats_names << sub_category_name
      sub_category_name = category_name_table[i].text
      i+=1
    end
    i+=1
    sub_category_name = category_name_table[i].text
    until sub_category_name == "MPG"
      performance_stats_names << sub_category_name
      sub_category_name = category_name_table[i].text
      i+=1
    end
    i+=1
    sub_category_name = category_name_table[i].text
    while i < category_name_table.size
      sub_category_name = category_name_table[i].text
      mpg_stats_names << sub_category_name
      i+=1
    end
  end
  
  interior_stats_names = unspacify(interior_stats_names)
  exterior_stats_names = unspacify(exterior_stats_names)
  performance_stats_names = unspacify(performance_stats_names)
  mpg_stats_names = unspacify(mpg_stats_names)
  total_stats = interior_stats_names.size + exterior_stats_names.size 
                + performance_stats_names.size + mpg_stats_names.size

  exterior = trim_list[0].xpath("./ul[@class = 'data_column']")
  num_trims = exterior.size
  
  # create array to hold all of the cars trim variations
  @trims = Array.new

  exterior.each do |ext|
    trim = Trim.new
    trim.trim_name = ext.xpath("./li[@class='header']/span/a").text
    @trims << trim
  end

  #visually track progress of fetching
  pbar = ProgressBar.new("Getting specs", total_stats * @trims.size )
  
  #exterior statistics for table
  exterior.each_with_index do |exterior_stats, index|
    stats = exterior_stats.xpath("./li/span")
    stat_index = 1
    exterior_stats_names.each do |stat_name|
      unless stat_name == ""
        @trims[index].exterior[stat_name.to_sym] = stats[stat_index].text
      end
      stat_index += 1
      pbar.inc
    end
  end

  #fetch interior statistics from table    
  interior = trim_list[1].xpath("./ul[@class = 'data_column']")
  interior.each_with_index do |interior_stats, index|
    stats = interior_stats.xpath("./li/span")
    stat_index = 1
    interior_stats_names.each do |stat_name|
      unless stat_name == ""
        @trims[index].interior[stat_name.to_sym] = stats[stat_index].text
      end
      stat_index += 1
      pbar.inc
    end
  end
  
  #fetch performance data from table
  performance = trim_list[2].xpath("./ul[@class = 'data_column']")
  performance.each_with_index do |performance_stats, index|
    stats = performance_stats.xpath("./li/span")
    stat_index = 1
    performance_stats_names.each do |stat_name|
      unless stat_name == ""
        @trims[index].performance[stat_name.to_sym] = stats[stat_index].text
      end
      stat_index += 1
      pbar.inc
    end
  end
  
  #fetch fuel economy data from table
  fuel_economy = trim_list[3].xpath("./ul[@class = 'data_column']")
  fuel_economy.each_with_index do |fuel_economy_stats, index|
    stats = fuel_economy_stats.xpath("./li/span")
    @trims[index].fuel_economy.fuel_tank_capacity = stats[1].text
    @trims[index].fuel_economy.epa_mileage_estimates = stats[2].text
    pbar.inc
  end
  
  pbar.finish

end


211
212
213
# File 'lib/car.rb', line 211

def print_trims
  @trims.each { |trim| puts "#{trim.trim_name}\n"  }
end