Class: VinQuery::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/vin_query/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vin, options = {}) ⇒ Query

Returns a new instance of Query.



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

def initialize(vin, options={})
  VinQuery.configuration.merge_options(options)
  @vin = vin
  @trim_levels = []
  build_url
end

Instance Attribute Details

#response_xmlObject (readonly)

Returns the value of attribute response_xml.



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

def response_xml
  @response_xml
end

#trim_levelsObject (readonly)

Returns the value of attribute trim_levels.



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

def trim_levels
  @trim_levels
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#vinObject (readonly)

Returns the value of attribute vin.



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

def vin
  @vin
end

Instance Method Details

#getObject



59
60
61
62
# File 'lib/vin_query/query.rb', line 59

def get
  @response_xml = fetch(@url)
  parse(@response_xml) if validate(@response_xml)
end

#parse(xml) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vin_query/query.rb', line 31

def parse(xml)
  doc = Nokogiri::XML(xml)
  raise ParseError if doc.xpath('//VINquery/VIN/Vehicle').size == 0

  results_vin = doc.xpath('//VINquery/VIN').first
  doc.xpath('//VINquery/VIN/Vehicle').each do |v|
    vehicle = {}

    v.xpath('Item').each do |a|
      key = a.attributes['Key'].to_s.
                                downcase.
                                gsub('/', '_').
                                gsub(' ', '_').
                                gsub('-', '_').
                                gsub('.', '').
                                to_sym

      if a.attributes['Unit'].to_s.length == 0
        vehicle[key] = a.attributes['Value'].to_s
      else
        vehicle[key] = "#{a.attributes['Value'].to_s} #{a.attributes['Unit'].to_s}"
      end
    end

    @trim_levels.push(VinQuery::TrimLevel.new(results_vin.attributes['Number'].to_s, vehicle))
  end
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/vin_query/query.rb', line 15

def valid?
  @valid
end

#validate(xml) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vin_query/query.rb', line 19

def validate(xml)
  doc = Nokogiri::XML(xml)
  raise ValidationError if doc.xpath('//VINquery/VIN').size == 0

  results_vin = doc.xpath('//VINquery/VIN').first
  if results_vin.attributes['Status'].to_s == 'SUCCESS'
    @valid = true
  else
    @valid = false
  end
end