car_query 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use 1.9.3-p125@car_query
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ # uncomment this line if your project needs to run something other than `rake`:
11
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rake", ">= 0.9"
4
+ gem "httparty", "~> 0.8.3"
5
+ gem "addressable", "~> 2.2.7"
6
+
7
+ group :development do
8
+ gem "yard", "~> 0.7"
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.1.3"
11
+ gem "jeweler", "~> 1.8.3"
12
+ gem "simplecov", "~> 0.6"
13
+ gem "ZenTest", "~> 4.7.0"
14
+ end
15
+
16
+ group :development,:test do
17
+ gem "rspec", "~> 2.9.0"
18
+ gem "vcr", "~> 2.1.1"
19
+ gem "webmock", "~> 1.8.6"
20
+ end
@@ -0,0 +1,56 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.7.0)
5
+ addressable (2.2.7)
6
+ crack (0.3.1)
7
+ diff-lcs (1.1.3)
8
+ git (1.2.5)
9
+ httparty (0.8.3)
10
+ multi_json (~> 1.0)
11
+ multi_xml
12
+ jeweler (1.8.3)
13
+ bundler (~> 1.0)
14
+ git (>= 1.2.5)
15
+ rake
16
+ rdoc
17
+ json (1.6.6)
18
+ multi_json (1.3.2)
19
+ multi_xml (0.4.4)
20
+ rake (0.9.2.2)
21
+ rdoc (3.12)
22
+ json (~> 1.4)
23
+ rspec (2.9.0)
24
+ rspec-core (~> 2.9.0)
25
+ rspec-expectations (~> 2.9.0)
26
+ rspec-mocks (~> 2.9.0)
27
+ rspec-core (2.9.0)
28
+ rspec-expectations (2.9.1)
29
+ diff-lcs (~> 1.1.3)
30
+ rspec-mocks (2.9.0)
31
+ simplecov (0.6.2)
32
+ multi_json (~> 1.3)
33
+ simplecov-html (~> 0.5.3)
34
+ simplecov-html (0.5.3)
35
+ vcr (2.1.1)
36
+ webmock (1.8.6)
37
+ addressable (>= 2.2.7)
38
+ crack (>= 0.1.7)
39
+ yard (0.7.5)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ ZenTest (~> 4.7.0)
46
+ addressable (~> 2.2.7)
47
+ bundler (~> 1.1.3)
48
+ httparty (~> 0.8.3)
49
+ jeweler (~> 1.8.3)
50
+ rake (>= 0.9)
51
+ rdoc (~> 3.12)
52
+ rspec (~> 2.9.0)
53
+ simplecov (~> 0.6)
54
+ vcr (~> 2.1.1)
55
+ webmock (~> 1.8.6)
56
+ yard (~> 0.7)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Orlando Del Aguila
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # CarQuery
2
+
3
+ A Ruby wrapper for the www.carqueryapi.com's JSON API
4
+
5
+ [![Build Status](https://secure.travis-ci.org/orlandodelaguila/car_query.png)](http://travis-ci.org/orlandodelaguila/car\_query)
6
+
7
+ Use RubyGems to install the gem:
8
+
9
+ gem install car_query
10
+
11
+ ## Contributing to CarQuery
12
+
13
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
14
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
15
+ * Fork the project.
16
+ * Start a feature/bugfix branch.
17
+ * Commit and push until you are happy with your contribution.
18
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
19
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
20
+
21
+ ## Copyright
22
+
23
+ Copyright (c) 2012 Orlando Del Aguila. See LICENSE.txt for
24
+ further details.
25
+
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "car_query"
18
+ gem.homepage = "http://github.com/orlandodelaguila/car_query"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A Ruby wrapper for the www.carqueryapi.com's JSON API}
21
+ gem.description = %Q{A Ruby wrapper for the www.carqueryapi.com's JSON API}
22
+ gem.email = "me@orlandodelaguila.com"
23
+ gem.authors = ["Orlando Del Aguila"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,149 @@
1
+ # encoding: utf-8
2
+ require 'httparty'
3
+ require 'addressable/uri'
4
+
5
+ # @author Orlando Del Aguila
6
+ # Holds all CarQuery methods
7
+ module CarQuery
8
+ # @author Orlando Del Aguila
9
+ # raised when the api returns error.
10
+ class APIError < StandardError;end
11
+
12
+ class << self; attr_reader :url; end
13
+ @url = Addressable::URI.parse("http://www.carqueryapi.com/api/0.3/")
14
+
15
+ # @author Orlando Del Aguila
16
+ # Contains query method for years
17
+ module Years
18
+ # @author Orlando Del Aguila
19
+ # Returns max and min years of models.
20
+ # @return [Hash] A hash min_year and max_year
21
+ # @example Returns years
22
+ # CarQuery::Years.get #=> {"min_year"=>"1940", "max_year"=>"2012"}
23
+ def self.get
24
+ request = CarQuery.url; request.query_values = {:cmd => "getYears"}
25
+ CarQuery.query(request,"Years")
26
+ end
27
+ end
28
+
29
+ # @author Orlando Del Aguila
30
+ # Contains query method for makes
31
+ module Makes
32
+ # @author Orlando Del Aguila
33
+ # Returns Makes.
34
+ # @param [Hash] params the options to return makes.
35
+ # @option params [String] :year ('') returns makes that have model of this year
36
+ # @option params [String] :sold_in_us ('') 1(sold in US), 0(not sold in US)
37
+ # @return [Hash] with makes
38
+ # @example Returns makes list that has models from 2012
39
+ # CarQuery::Makes.get({:year => 2012}) #=> [{"make_id"=>"acura", "make_display"=>"Acura"...}]
40
+ def self.get(params={})
41
+ params.delete(:cmd)
42
+ request = CarQuery.url
43
+ request.query_values = {:cmd => "getMakes"}.merge(params)
44
+ CarQuery.query(request,"Makes")
45
+ end
46
+ end
47
+
48
+ # @author Orlando Del Aguila
49
+ # Contains query method for models
50
+ module Models
51
+ # @author Orlando Del Aguila
52
+ # Returns Models
53
+ # @param [Hash] params the options to return models.
54
+ # @option params [String] :make The make of the models (required)
55
+ # @option params [String] :year ('') Model year
56
+ # @option params [String] :sold_in_us ('') 1(sold in US), 0(not sold in US)
57
+ # @option params [String] :body ('') the body type. ex Coupe, Sedan, SUV, Pickup, Crossover, Minivan, etc.
58
+ # @return [Hash] with models
59
+ # @example Returns makes list that has models from 2012
60
+ # CarQuery::Models.get({:make => "ford", :year => "2012"}) #=> [{"model_name"=>"Bantam", "model_make_id"=>"ford"}...]
61
+ # @raise [APIError] if :make param is missing
62
+ def self.get(params={})
63
+ params.delete(:cmd)
64
+ request = CarQuery.url
65
+ request.query_values = {:cmd => "getModels"}.merge(params)
66
+ CarQuery.query(request,"Models")
67
+ end
68
+ end
69
+
70
+ # @author Orlando Del Aguila
71
+ # Contains query method for trims
72
+ module Trims
73
+ # @author Orlando Del Aguila
74
+ # Returns Model Trims, all params are optional, if no param is passed then returns full results for all trims from all models (results are limited to 500).
75
+ # @param [Hash] params the options to return a trim.
76
+ # @option params [String] :body ('') the body type. ex Coupe, Sedan, SUV, Pickup, Crossover, Minivan, etc.
77
+ # @option params [String] :doors ('') number of doors
78
+ # @option params [String] :drive ('') ex Front, Rear, AWD, 4WD, etc
79
+ # @option params [String] :engine_position ('') ex Front, Middle, Rear
80
+ # @option params [String] :engine_type ('') ex V, in-line, etc
81
+ # @option params [String] :fuel_type ('') ex Gasoline, Diesel, etc
82
+ # @option params [String] :full_results ('') 1 by default. Set to 0 to include only basic year / make /model / trim data (improves load times)
83
+ # @option params [String] :keyword ('') Keyword search. Searches year, make, model, and trim values
84
+ # @option params [String] :make ('') Make ID
85
+ # @option params [String] :min_cylinders ('') Minimum Number of cylinders
86
+ # @option params [String] :min_lkm_hwy ('') Maximum fuel efficiency (highway, l/100km)
87
+ # @option params [String] :min_power ('') Minimum engine power (PS)
88
+ # @option params [String] :min_top_speed ('') Minimum Top Speed (km/h)
89
+ # @option params [String] :min_torque ('') Minimum Torque (nm)
90
+ # @option params [String] :min_weight ('') Minimum Weight (kg)
91
+ # @option params [String] :min_year ('') Earliest Model Year
92
+ # @option params [String] :max_cylinders ('') Maximum Number of cylinders
93
+ # @option params [String] :max_lkm_hwy ('') Minimum fuel efficiency (highway, l/100km)
94
+ # @option params [String] :max_power ('') Minimum engine power (HP)
95
+ # @option params [String] :max_top_speed ('') Maximum Top Speed (km/h)
96
+ # @option params [String] :max_torque ('') Maximum Torque (nm)
97
+ # @option params [String] :max_weight ('') Maximum Weight (kg)
98
+ # @option params [String] :max_year ('') Latest Model Year
99
+ # @option params [String] :model ('') Model Name
100
+ # @option params [String] :seats ('') Number of Seats
101
+ # @option params [String] :sold_in_us ('') 1(sold in US), 0(not sold in US)
102
+ # @option params [String] :year ('') Model Year
103
+ # @raise [APIError] if :model param is missing.
104
+ # @return [Hash] Results are sorted by year, make, model, and trim. Results are limited to 500 records
105
+ # @example Returns trims by keyword and only basic results
106
+ # CarQuery::Trims.get({:keyword => "ford f-350", :full_results => 0}) #=> [{"model_id":"48922","model_year":"2012"...}]
107
+ def self.get(params={})
108
+ params.delete(:cmd)
109
+ request = CarQuery.url
110
+ request.query_values = {:cmd => "getTrims"}.merge(params)
111
+ CarQuery.query(request,"Trims")
112
+ end
113
+ end
114
+
115
+ # @author Orlando Del Aguila
116
+ # Contains query method for model
117
+ module Model
118
+ # @author Orlando Del Aguila
119
+ # Returns Model Trims, all params all optional, if no param is passed then returns full results for all trims from all models.
120
+ # @param [Hash] params the options to return a model.
121
+ # @option params [Integer] :model The model_id (required)
122
+ # @return [Hash] with all model data or empty
123
+ # @example Returns all data of the Ford F-350 2012
124
+ # CarQuery::Model.get({:model =>"48922"}) #=> {"model_id":"48922","model_make_id":"ford"...}
125
+ def self.get(params={})
126
+ params.delete(:cmd)
127
+ request = CarQuery.url
128
+ request.query_values = {:cmd => "getModel"}.merge(params)
129
+ CarQuery.query(request,"Model")
130
+ end
131
+
132
+ end
133
+
134
+ protected
135
+ # @author Orlando Del Aguila
136
+ # Contains base query method
137
+ def self.query(url=self.url,key="")
138
+ if key == "Model"
139
+ response = HTTParty.get(url).first
140
+ raise APIError,response.last if response.first == "error"
141
+ return response
142
+ end
143
+ response = HTTParty.get(url)
144
+ raise APIError,response["error"] if response.has_key?("error")
145
+ return response.parsed_response[key] unless key.empty?
146
+ response.parsed_response
147
+ end
148
+
149
+ end
@@ -0,0 +1,133 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe CarQuery do
4
+ use_vcr_cassette :record => :new_episodes
5
+ context "Base" do
6
+ it "returns an Addressable::URI Object with the base url" do
7
+ CarQuery.url.to_s.should == "http://www.carqueryapi.com/api/0.3/"
8
+ CarQuery.url.class.should == Addressable::URI
9
+ end
10
+
11
+ it "raises an error if api returns error" do
12
+ lambda{CarQuery.query}.should raise_error(CarQuery::APIError)
13
+ end
14
+
15
+ it "returns parsed_response if the query its good" do
16
+ request = CarQuery.url
17
+ request.query_values = {:cmd => "getYears"}
18
+ lambda{CarQuery.query(request.to_s)}.should_not raise_error(CarQuery::APIError)
19
+ end
20
+
21
+ it "returns a non_scoped parsed_response if key is passed as a param" do
22
+ request = CarQuery.url
23
+ request.query_values = {:cmd => "getYears"}
24
+ CarQuery.query(request.to_s).should == {"Years"=>{"min_year"=>"1940", "max_year"=>"2012"}}
25
+ CarQuery.query(request.to_s,"Years").should == {"min_year"=>"1940", "max_year"=>"2012"}
26
+ end
27
+ end
28
+ end
29
+
30
+ describe CarQuery::Years do
31
+ use_vcr_cassette :record => :new_episodes
32
+ it "returns range of years of models avaible in the db" do
33
+ CarQuery::Years.get.should == {"min_year"=>"1940", "max_year"=>"2012"}
34
+ end
35
+ end
36
+
37
+ describe CarQuery::Makes do
38
+ use_vcr_cassette :record => :new_episodes
39
+
40
+ it "returns all makers" do
41
+ CarQuery::Makes.get.should_not be_empty
42
+ end
43
+
44
+ it "accepts params as a hash" do
45
+ lambda{CarQuery::Makes.get({:year => 2000})}.should_not raise_error
46
+ end
47
+
48
+ it "returns all makers that has models in a certain year" do
49
+ CarQuery::Makes.get({:year => 2012}).should_not be_empty
50
+ end
51
+
52
+ it "returns all makers that sold cars in USA" do
53
+ CarQuery::Makes.get({:sold_in_us => 1}).should_not be_empty
54
+ end
55
+ end
56
+
57
+ describe CarQuery::Models do
58
+ use_vcr_cassette :record => :new_episodes
59
+
60
+ it "should raise error if no maker is passed" do
61
+ lambda{CarQuery::Models.get}.should raise_error(CarQuery::APIError)
62
+ end
63
+
64
+ it "should not raise error if a maker is passed" do
65
+ lambda{CarQuery::Models.get({:make => "ford"})}.should_not raise_error(CarQuery::APIError)
66
+ end
67
+
68
+ it "returns all models for a certain maker" do
69
+ response = CarQuery::Models.get({:make => "ford"})
70
+ response.should_not be_empty
71
+ response.first.should == {"model_name"=>"021 C", "model_make_id"=>"ford"}
72
+ end
73
+
74
+ it "returns all models from a maker by year" do
75
+ response = CarQuery::Models.get({:make => "ford", :year => "2012"})
76
+ response.should_not be_empty
77
+ response.first.should == {"model_name"=>"Bantam","model_make_id"=>"ford"}
78
+ end
79
+ end
80
+
81
+ describe CarQuery::Trims do
82
+ use_vcr_cassette :record => :new_episodes
83
+
84
+ it "should return 500 first trims" do
85
+ response = CarQuery::Trims.get
86
+ response.should_not be_empty
87
+ response.size.should == 500
88
+ end
89
+
90
+ it "accepts variables like year,model,make etc." do
91
+ response = CarQuery::Trims.get({:year => 2012, :make => "ford", :model => "f-350"})
92
+ response.should_not be_empty
93
+ response.first.should == {"model_id"=>"48922", "model_make_id"=>"ford", "model_name"=>"F-350", "model_trim"=>"Super Duty King Ranch", "model_year"=>"2012", "model_body"=>"Pickup", "model_engine_position"=>"Front", "model_engine_cc"=>"6200", "model_engine_cyl"=>"8", "model_engine_type"=>"V", "model_engine_valves_per_cyl"=>"2", "model_engine_power_ps"=>"390", "model_engine_power_rpm"=>"5500", "model_engine_torque_nm"=>"548", "model_engine_torque_rpm"=>"4500", "model_engine_bore_mm"=>nil, "model_engine_stroke_mm"=>nil, "model_engine_compression"=>nil, "model_engine_fuel"=>"Flex Fuel", "model_top_speed_kph"=>nil, "model_0_to_100_kph"=>nil, "model_drive"=>"Rear", "model_transmission_type"=>"6-speed shiftable automatic", "model_seats"=>nil, "model_doors"=>nil, "model_weight_kg"=>nil, "model_length_mm"=>"6269", "model_width_mm"=>"2029", "model_height_mm"=>"1961", "model_wheelbase_mm"=>"3967", "model_lkm_hwy"=>nil, "model_lkm_mixed"=>nil, "model_lkm_city"=>nil, "model_fuel_cap_l"=>"132", "model_sold_in_us"=>"1", "make_display"=>"Ford", "make_country"=>"USA"}
94
+ end
95
+
96
+ it "accepts a full_results param that give us only model,year and make instead of all data of a trim" do
97
+ response = CarQuery::Trims.get({:year => 2012, :make => "ford", :model => "f-350", :full_results => 0})
98
+ response.should_not be_empty
99
+ response.first.should == {"model_id"=>"48922", "model_year"=>"2012", "model_make_id"=>"ford", "model_name"=>"F-350", "model_trim"=>"Super Duty King Ranch", "make_display"=>"Ford", "make_country"=>"USA"}
100
+ end
101
+
102
+ it "accepts a keyword param that tries to find a trim by keywords" do
103
+ response = CarQuery::Trims.get({:keyword => "ford f-350", :full_results => 0})
104
+ response.should_not be_empty
105
+ response.first.should == {"model_id"=>"48922", "model_year"=>"2012", "model_make_id"=>"ford", "model_name"=>"F-350", "model_trim"=>"Super Duty King Ranch", "make_display"=>"Ford", "make_country"=>"USA"}
106
+ end
107
+
108
+ it "accepts a keyword param that tries to find a trim by keywords and could return empty if theres no match" do
109
+ response = CarQuery::Trims.get({:keyword => "no matching keyword here!", :full_results => 0})
110
+ response.should be_empty
111
+ end
112
+
113
+ it "returns an empty hash if theres no match" do
114
+ response = CarQuery::Trims.get({:year => 2015, :make => "ford", :model => "f-350"})
115
+ response.should be_empty
116
+ end
117
+ end
118
+
119
+ describe CarQuery::Trims do
120
+ use_vcr_cassette :record => :new_episodes
121
+
122
+ it "raises an error if no model param is passed" do
123
+ lambda{CarQuery::Model.get}.should raise_error(CarQuery::APIError)
124
+ end
125
+
126
+ it "dont raises an error if model param is passed" do
127
+ lambda{CarQuery::Model.get({:model =>"48922"})}.should_not raise_error(CarQuery::APIError)
128
+ end
129
+
130
+ it "returns all data of a certain model" do
131
+ response = CarQuery::Model.get({:model =>'48922'})
132
+ end
133
+ end