Class: Vinquery

Inherits:
Object
  • Object
show all
Defined in:
lib/vinquery.rb,
lib/vinquery/version.rb

Constant Summary collapse

VERSION =
'0.4.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, access_code, report_type, log = nil) ⇒ Vinquery

Returns a new instance of Vinquery.


15
16
17
18
19
20
21
# File 'lib/vinquery.rb', line 15

def initialize(url, access_code, report_type, log = nil)
  @url = url
  @access_code = access_code
  @report_type = report_type
  log ||= Logger.new(nil)
  @log = log
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.


6
7
8
# File 'lib/vinquery.rb', line 6

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.


6
7
8
# File 'lib/vinquery.rb', line 6

def errors
  @errors
end

#resultObject (readonly)

Returns the value of attribute result.


6
7
8
# File 'lib/vinquery.rb', line 6

def result
  @result
end

Class Method Details

.get(vin, options = {}) ⇒ Object


8
9
10
11
12
13
# File 'lib/vinquery.rb', line 8

def self.get(vin, options={})
  request = Vinquery.new(options[:url], options[:access_code], options[:report_type], options[:logger])
  doc = request.fetch vin
  result = request.parse doc
  request
end

Instance Method Details

#fetch(vin) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vinquery.rb', line 23

def fetch(vin)
  # use reducable=FALSE to get additional fields like fuel_type
  # use vt=true to get vehicle_type
  # http://www.vinquery.com/ws_POQCXTYNO1D/xml_v100_QA7RTS8Y.aspx?accessCode=6958785b-ac0a-4303-8b28-40e14aa836ce&vin=YourVINToDecode&reportType=0&vt=true&gvwr=true
  @uri ||= "#{@url}?accessCode=#{@access_code}&reportType=#{@report_type}&reducable=FALSE&vt=TRUE&gvwr=TRUE"
  url_s = @uri + "&vin=#{vin}"
  @log.info{"Vinquery#fetch - uri: #{url_s}"}
  url = URI.parse(url_s)
  begin
    @result = Net::HTTP.get url
    @log.debug{"Vinquery#fetch - result: #{@result}"}
  rescue Exception => e
    @log.error{"ERROR - #{e.message}"}
    @log.error{"ERROR - #{e.backtrace[0]}"}
    xml = Nokogiri::XML::Builder.new do |doc|
      doc.vin(:number => vin,:status => "FAILED") {
        doc.message(:Key => "VinQuery unavailable", :Value => "Oops, it looks like our VIN decoder is unavailable at the moment. Please try again later.")
      }
    end
    @result = xml.to_xml
  end
  @doc = Nokogiri::HTML(@result)
end

#make_vin_key(vin) ⇒ Object


78
79
80
81
# File 'lib/vinquery.rb', line 78

def make_vin_key(vin)
  key = vin.slice(0,8)
  key << vin.slice(9,2)
end

#parse(doc) ⇒ Object


47
48
49
50
51
# File 'lib/vinquery.rb', line 47

def parse(doc)
  set_attributes doc
  set_errors_hash doc
  attributes
end

#set_attributes(doc) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vinquery.rb', line 53

def set_attributes(doc)
  attributes = {}
  doc.xpath('//vehicle[1]/item').each do |item|
    attributes[item.attributes['key'].value.downcase.gsub('.', '').gsub(/ /, '_').to_sym] = item.attributes['value'].value
  end
  @log.info{"Vinquery#set_attributes - number of attributes parsed: #{attributes.size}"}
  if attributes.size > 0
    vin = doc.css('vin').first.attributes['number'].value
    attributes[:vin_key] = make_vin_key(vin)
    attributes[:vendor_result] = doc.to_xml 
  end
  @attributes = attributes
end

#set_errors_hash(doc) ⇒ Object


67
68
69
70
71
72
# File 'lib/vinquery.rb', line 67

def set_errors_hash(doc)
  @errors = []
  @valid = doc.css('vin').first.attributes['status'].value == "SUCCESS"
  doc.css('message').each{|msg| @errors << {msg.attributes['key'].value => msg.attributes['value'].value} } unless valid?
  @errors.each{|msg| @log.error{"Vinquery#set_errors_hash - error: #{msg.to_s}"}} unless @errors.empty?
end

#valid?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/vinquery.rb', line 74

def valid?
  @valid
end