Class: VinQuery::Query
- Inherits:
-
Object
- Object
- VinQuery::Query
- Defined in:
- lib/vin_query/query.rb
Instance Attribute Summary collapse
-
#response_xml ⇒ Object
readonly
Returns the value of attribute response_xml.
-
#trim_levels ⇒ Object
readonly
Returns the value of attribute trim_levels.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#vin ⇒ Object
readonly
Returns the value of attribute vin.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(vin, options = {}) ⇒ Query
constructor
A new instance of Query.
- #parse(xml) ⇒ Object
- #valid? ⇒ Boolean
- #validate(xml) ⇒ Object
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, ={}) VinQuery.configuration.() @vin = vin @trim_levels = [] build_url end |
Instance Attribute Details
#response_xml ⇒ Object (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_levels ⇒ Object (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 |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/vin_query/query.rb', line 6 def url @url end |
#vin ⇒ Object (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
#get ⇒ Object
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
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
15 16 17 |
# File 'lib/vin_query/query.rb', line 15 def valid? @valid end |
#validate(xml) ⇒ Object
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 |