Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created March 23, 2009 05:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jnunemaker/83431 to your computer and use it in GitHub Desktop.
Save jnunemaker/83431 to your computer and use it in GitHub Desktop.
# Quick wrapper for undocumented and unsupported MyMileMarker.com API
require 'rubygems'
gem 'httparty', '>= 0.3.1'
require 'httparty'
module MyMileMarker
class Client
include HTTParty
def initialize(email, password)
@email, @password = email, password
end
def get(path, options={})
options.merge!({:basic_auth => {:username => @email, :password => @password}})
self.class.get("http://mymilemarker.com#{path}", options)
end
def vehicles
get('/vehicles.xml')['vehicles']
end
def vehicle(vehicle_id_or_slug)
Vehicle.new(self, vehicle_id_or_slug).info
end
def histories(vehicle_id_or_slug)
Vehicle.new(self, vehicle_id_or_slug).histories
end
def fuel_economy(vehicle_id_or_slug)
Vehicle.new(self, vehicle_id_or_slug).fuel_economy
end
def mileage(vehicle_id_or_slug)
Vehicle.new(self, vehicle_id_or_slug).mileage
end
end
class Vehicle
attr_reader :identifier, :client
def initialize(client, identifier)
@client, @identifier = client, identifier
end
def info
client.get("/vehicles/#{identifier}.xml")['vehicle']
end
def histories
client.get("/vehicles/#{identifier}/histories.xml?all=true")['histories']
end
def fuel_economy
client.get("/vehicles/#{identifier}/reports/fuel_economy.xml")['chart']['point']
end
def mileage
client.get("/vehicles/#{identifier}/reports/mileage.xml")['chart']['point']
end
end
end
if $0 == __FILE__
client = MyMileMarker::Client.new('email@domain.com', 'secret')
p client.vehicles
p '*'*50
############################
# Individual Vehicle Stuff #
############################
p client.vehicle('ford-mustang')
p '*'*50
p client.histories('ford-mustang')
p '*'*50
p client.fuel_economy('ford-mustang')
p '*'*50
p client.mileage('ford-mustang')
p '*'*50
##############################
# Or You Can Do It Like This #
##############################
vehicle = MyMileMarker::Vehicle.new(client, 'ford-mustang')
p vehicle.histories
p '*'*50
p vehicle.fuel_economy
p '*'*50
p vehicle.mileage
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment