class FuelEcoAPI

Public Class Methods

fetch_fuel_economy(vehicle_code, driving_type) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 49
def self.fetch_fuel_economy(vehicle_code, driving_type)
  driving_type_converter = {'city' => 'city08U', 'highway' => 'highway08', 'combo' => 'comb08' }
  driving_type = driving_type_converter[driving_type]

  data_response = fetch_vehicle(vehicle_code)

  data_response['vehicle'][driving_type]
end
fetch_vehicle(vehicle_code) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 40
def self.fetch_vehicle(vehicle_code)
  url = URI("https://www.fueleconomy.gov/ws/rest/vehicle/#{vehicle_code}")
  data_response = http_response(url)
end
makes(year) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 17
def self.makes(year)
  url = URI("https://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=#{year}")
  data_response = http_response(url)

  # [{"text"=>"Acura", "value"=>"Acura"}] -> ["Acura"]
  year_make_model_options_values(data_response)
end
models(year, make) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 25
def self.models(year, make)
  url = URI("https://www.fueleconomy.gov/ws/rest/vehicle/menu/model?year=#{year}&make=#{make}")
  data_response = http_response(url)

  # [{"text"=>"Avalon", "value"=>"Avalon"}] -> ["Avalon"]
  year_make_model_options_values(data_response)
end
vehicle_options(year, make, model) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 33
def self.vehicle_options(year, make, model)
  url = URI("https://www.fueleconomy.gov/ws/rest/vehicle/menu/options?year=#{year}&make=#{make}&model=#{model}")
  data_response = http_response(url)

  data_response['menuItems']['menuItem']
end
verify_vehicle_code(vehicle_code) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 45
def self.verify_vehicle_code(vehicle_code)
  fetch_vehicle(vehicle_code).has_key?('vehicle')
end
years() click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 9
def self.years
  url = URI('https://www.fueleconomy.gov/ws/rest/vehicle/menu/year')
  data_response = http_response(url)

  #[{"text"=>"2022", "value"=>"2022"}] -> ["2022"]
  year_make_model_options_values(data_response)
end

Private Class Methods

http_response(url) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 60
def self.http_response(url)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(url)
  @response = http.request(request)
  Crack::XML.parse(@response.read_body)
end
year_make_model_options_values(hash) click to toggle source
# File lib/guzzler/fuel_eco_api.rb, line 69
def self.year_make_model_options_values(hash)
  if hash['menuItems']['menuItem'].is_a?(Array)
    hash['menuItems']['menuItem'].map {|hash| hash['value']}
  else
    [hash['menuItems']['menuItem']['value']]
  end
end