Class: Vinquery
- Inherits:
-
Object
- Object
- Vinquery
- Defined in:
- lib/vinquery.rb,
lib/vinquery/version.rb
Constant Summary collapse
- VERSION =
'0.4.1'
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(vin) ⇒ Object
-
#initialize(url, access_code, report_type, log = nil) ⇒ Vinquery
constructor
A new instance of Vinquery.
- #make_vin_key(vin) ⇒ Object
- #parse(doc) ⇒ Object
- #set_attributes(doc) ⇒ Object
- #set_errors_hash(doc) ⇒ Object
- #valid? ⇒ Boolean
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/vinquery.rb', line 6 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/vinquery.rb', line 6 def errors @errors end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
6 7 8 |
# File 'lib/vinquery.rb', line 6 def result @result end |
Class Method Details
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.}"} @log.error{"ERROR - #{e.backtrace[0]}"} xml = Nokogiri::XML::Builder.new do |doc| doc.vin(:number => vin,:status => "FAILED") { doc.(: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
74 75 76 |
# File 'lib/vinquery.rb', line 74 def valid? @valid end |