Class: Vinquery
- Inherits:
-
Object
- Object
- Vinquery
- Defined in:
- lib/vinquery.rb,
lib/vinquery/version.rb
Constant Summary collapse
- VERSION =
'0.5.0'
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
- #create_exception ⇒ Object
- #fetch ⇒ Object
-
#initialize(vin, url, access_code, report_type, log = nil) ⇒ Vinquery
constructor
A new instance of Vinquery.
- #make_vin_key(vin) ⇒ Object
- #parse ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(vin, url, access_code, report_type, log = nil) ⇒ Vinquery
Returns a new instance of Vinquery.
16 17 18 19 20 21 22 23 24 |
# File 'lib/vinquery.rb', line 16 def initialize(vin, url, access_code, report_type, log = nil) @vin = vin @url = url @errors = [] @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.
7 8 9 |
# File 'lib/vinquery.rb', line 7 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/vinquery.rb', line 7 def errors @errors end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/vinquery.rb', line 7 def result @result end |
Class Method Details
Instance Method Details
#create_exception ⇒ Object
62 63 64 65 |
# File 'lib/vinquery.rb', line 62 def create_exception @doc.css('message').each{|msg| @errors << {msg.attributes['key'].value => msg.attributes['value'].value} } VinqueryException.new(@errors.map{|msg| "#{msg.keys[0]}: #{msg.values[0]}"}.join("\n")) unless @errors.empty? end |
#fetch ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vinquery.rb', line 26 def fetch # use reducable=FALSE to get additional fields like fuel_type # use vt=true to get vehicle_type # use gvwr=TRUE to get GVWR # 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}"} @doc = Nokogiri::HTML(@result) rescue Exception => e raise VinqueryException.new(e., e) end end |
#make_vin_key(vin) ⇒ Object
71 72 73 74 |
# File 'lib/vinquery.rb', line 71 def make_vin_key(vin) key = vin.slice(0,8) key << vin.slice(9,2) end |
#parse ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vinquery.rb', line 44 def parse raise create_exception unless valid? 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 attributes[:number_of_cylinders] = attributes[:engine_type].upcase.match(/ [LV](\d{1,2}) /) ? $1 : nil attributes[:has_turbo] = attributes[:engine_type].upcase.match(/ ([TURBO|SUPERCHARGED]) /) ? true : false end @attributes = attributes end |
#valid? ⇒ Boolean
67 68 69 |
# File 'lib/vinquery.rb', line 67 def valid? @valid ||= @doc.css('vin').first.attributes['status'].value == "SUCCESS" rescue false end |