Created
May 11, 2011 18:12
-
-
Save bear454/966985 to your computer and use it in GitHub Desktop.
VinLink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Depends on the "xml-simple" and "net-http-digest_auth" gems... | |
For Rails 2: | |
config/environment.rb: | |
Rails::Initializer.run do |config| | |
... | |
config.gem "xml-simple", :lib => "xmlsimple" | |
config.gem "net-http-digest_auth", :lib => "net/http/digest_auth" | |
... | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "uri" | |
require "net/http" | |
require "net/http/digest_auth" | |
require "xmlsimple" | |
class VinLink | |
VINLINK_URL = "http://service.vinlink.com" | |
attr_writer :options | |
#v = VinLink.new(:user => 'foo', :password => 'bar', :report_type => 'basic') | |
def initialize(options = {}) | |
@options = {} | |
options.each do |key, value| | |
@options[key.to_s] = value.to_s | |
end | |
end | |
#v.decode('1234567890ABCD') | |
def decode(vin, report_type = @options["report_type"] ) | |
url = URI.parse(VINLINK_URL) | |
url.user = @options["user"] | |
url.password = @options["password"] | |
http = Net::HTTP.new url.host, url.port | |
req = Net::HTTP::Get.new("/report?type=#{report_type}&vin=#{vin}") | |
res = http.request req | |
digest_auth = Net::HTTP::DigestAuth.new | |
auth = digest_auth.auth_header url, res['www-authenticate'], 'GET' | |
req.add_field 'Authorization', auth | |
@response = http.request(req) | |
if @response.code == "200" #yay | |
XmlSimple.xml_in(@response.body, 'KeyToSymbol' => true)[:report][0] | |
else | |
@response.value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment