carquery 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6defa7e4ce72d8b2a63b5710685969a961fcaf28
4
+ data.tar.gz: cb457d301d27c4e0e4fec9a841c59ad0f4311faa
5
+ SHA512:
6
+ metadata.gz: 900e1ef8a3ad500bf31fbddb9b390e406253ca5f5ad871164ef6f9770ce2cea20d1a2233b2305f7cee75367ca0eda0d9c49c86d5daeab86f46e3f3f31de4b222
7
+ data.tar.gz: cc310a34978a82524fc3d8bc846978fdf94e037d3709152ec703bab0a8ec565ba56cc5cbdaa5cc5dbe3c81fd84d96ff29add3b31c28dd13b9d72fc678539389f
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carquery.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry-debugger'
8
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rustam Sharshenov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Carquery
2
+
3
+ Provides DSL for a [www.carqueryapi.com](http://www.carqueryapi.com) API. Read more about CarQuery database API [here](http://www.carqueryapi.com/about/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carquery'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carquery
18
+
19
+ ## Usage
20
+
21
+ Get car makes
22
+
23
+ Carquery.get_makes year: 2009
24
+
25
+ Get car model list by make
26
+
27
+ Carquery.get_models_for 'volvo'
28
+
29
+ Get car model trim list
30
+
31
+ Carquery.get_trims
32
+
33
+ Get car model trim by id
34
+
35
+ Carquery.get_trim 12345
36
+
37
+ Get range of available years from the CarQuery database
38
+
39
+ Carquery.get_years_range
40
+
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carquery/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carquery"
8
+ spec.version = Carquery::VERSION
9
+ spec.authors = ["Rustam Sharshenov"]
10
+ spec.email = ["rustam@sharshenov.com"]
11
+ spec.description = %q{www.carqueryapi.com API client}
12
+ spec.summary = %q{Provides DSL for a www.carqueryapi.com API.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'httparty'
22
+ spec.add_dependency 'addressable'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "webmock"
28
+ spec.add_development_dependency "vcr"
29
+ end
@@ -0,0 +1,15 @@
1
+ require "carquery/errors"
2
+ require "carquery/version"
3
+ require "carquery/request"
4
+ require "carquery/resources/base_struct"
5
+ Dir[File.dirname(__FILE__) + "/carquery/dsl/*.rb"].each { |f| require f }
6
+ Dir[File.dirname(__FILE__) + "/carquery/resources/*.rb"].each { |f| require f }
7
+
8
+ # @TODO remove
9
+ require "pry"
10
+
11
+ module Carquery
12
+
13
+ API_URL="http://www.carqueryapi.com/api/0.3"
14
+
15
+ end
@@ -0,0 +1,14 @@
1
+ module Carquery
2
+
3
+ # Returns auto makes
4
+ # Optional params:
5
+ # @year [optional] - all makes which produced a model in the specified year
6
+ # @sold_in_us [optional] - setting it to “1″ will restrict results to models sold in the USA.
7
+ # Usage:
8
+ # Carquery.get_makes year: 2009 => [#<struct Carquery::Make code="abarth", title="Abarth", is_common=false, country="Italy">, ...]
9
+ def self.get_makes params={}
10
+ response = request 'getMakes', params
11
+ response['Makes'].map {|make| Make.build make }
12
+ end
13
+
14
+ end
@@ -0,0 +1,23 @@
1
+ module Carquery
2
+
3
+ class << self
4
+
5
+ # Returns car models by the manufacturer
6
+ # Input: auto make code
7
+ # Optional params:
8
+ # @year [optional] - omitting it will retrieve all model names ever produced by the manufacturer.
9
+ # @sold_in_us [optional] - setting it to “1″ will restrict results to models sold in the USA.
10
+ # @body [optional] - including it will restrict results to models of the specified body type (SUV, Sedan, etc)
11
+ # Usage:
12
+ # Carquery.get_models_for 'volvo', year: 2010 => [#<struct Carquery::CarModel title="120">, ...]
13
+ def get_models make_code, params={}
14
+ params.merge! make: make_code
15
+ response = request 'getModels', params
16
+ response['Models'].map {|make| CarModel.build make }
17
+ end
18
+
19
+ alias_method :get_models_for, :get_models
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,17 @@
1
+ module Carquery
2
+
3
+ class << self
4
+
5
+ # Returns car model trim by specified id
6
+ # Input: auto make code
7
+ # Optional params: none
8
+ # Usage:
9
+ # Carquery.get_trim 12343 => #<struct Carquery::Trim id=12343, title="Tempra", make_code="fiat", trim="1.6", ...>
10
+ def get_trim id
11
+ response = request 'getModel', model: id
12
+ Trim.build response
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,45 @@
1
+ module Carquery
2
+
3
+ class << self
4
+
5
+ # Returns trim data for models meeting specified criteria.
6
+ # Optional params:
7
+ # @make - Make code
8
+ # @model - Model Name
9
+ # @body - Coupe, Sedan, SUV, Pickup, Crossover, Minivan, etc.
10
+ # @doors - number of doors
11
+ # @drive - Front, Rear, AWD, 4WD, etc
12
+ # @engine_position - Front, Middle, Rear
13
+ # @engine_type - V, in-line, etc
14
+ # @fuel_type - Gasoline, Diesel, etc
15
+ # @full_results - 1 by default. Set to 0 to include only basic year / make /model / trim data (improves load times)
16
+ # @keyword - Keyword search. Searches year, make, model, and trim values
17
+ # @min_cylinders - Minimum Number of cylinders
18
+ # @min_lkm_hwy - Maximum fuel efficiency (highway, l/100km)
19
+ # @min_power - Minimum engine power (PS)
20
+ # @min_top_speed - Minimum Top Speed (km/h)
21
+ # @min_torque - Minimum Torque (nm)
22
+ # @min_weight - Minimum Weight (kg)
23
+ # @min_year - Earliest Model Year
24
+ # @max_cylinders - Maximum Number of cylinders
25
+ # @max_lkm_hwy - Minimum fuel efficiency (highway, l/100km)
26
+ # @max_power - Minimum engine power (HP)
27
+ # @max_top_speed - Maximum Top Speed (km/h)
28
+ # @max_torque - Maximum Torque (nm)
29
+ # @max_weight - Maximum Weight (kg)
30
+ # @max_year - Latest Model Year
31
+ # @seats - Number of Seats
32
+ # @sold_in_us - 1(sold in US), 0(not sold in US)
33
+ # @year - Model Year
34
+ # Usage:
35
+ # Carquery.get_trims 'volvo', year: 2010 => [#<struct Carquery::Trim id=12343, title="Tempra", make_code="fiat", ...>, ...]
36
+ # Notes:
37
+ # Results are sorted by year, make, model, and trim. Results are limited to 500 records.
38
+ def get_trims params={}
39
+ response = request 'getTrims', params
40
+ response['Trims'].map {|make| Trim.build make }
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,17 @@
1
+ module Carquery
2
+
3
+ class << self
4
+
5
+ # Returns range of available years from the CarQuery database.
6
+ # Input: none
7
+ # Usage:
8
+ # Carquery.get_years_range => 1941..2012
9
+ def get_years_range
10
+ response = request 'getYears'
11
+ min = response["Years"]["min_year"].to_i
12
+ max = response["Years"]["max_year"].to_i
13
+ min..max
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ module Carquery
2
+ class RequestError < StandardError; end
3
+ end
@@ -0,0 +1,29 @@
1
+ require "httparty"
2
+ require "addressable/uri"
3
+
4
+ module Carquery
5
+
6
+ class << self
7
+
8
+ protected
9
+
10
+ def request cmd, params={}
11
+ params.merge! cmd: cmd
12
+ response = HTTParty.get _build_url(params)
13
+
14
+ if cmd == 'getModel'
15
+ raise RequestError, "Not found" unless response.first.has_key? "model_id"
16
+ response.parsed_response.first
17
+ else
18
+ raise RequestError, response["error"] if response.has_key? "error"
19
+ response.parsed_response
20
+ end
21
+ end
22
+
23
+ def _build_url params={}
24
+ uri = Addressable::URI.parse API_URL
25
+ uri.query_values = params
26
+ uri.to_s
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ module Carquery
2
+
3
+ class BaseStruct < Struct
4
+ class << self
5
+
6
+ protected
7
+
8
+ def get_i str
9
+ return nil if str.nil?
10
+ str.to_i
11
+ end
12
+
13
+ def get_f str
14
+ return nil if str.nil?
15
+ str.to_f
16
+ end
17
+
18
+ def get_boolean str
19
+ str == '1'
20
+ end
21
+
22
+ def get_str str
23
+ str.to_s.strip
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,9 @@
1
+ module Carquery
2
+ CarModel = BaseStruct.new :title do
3
+
4
+ def self.build raw
5
+ title = get_str raw['model_name']
6
+ new title
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Carquery
2
+ Make = BaseStruct.new :code, :title, :is_common, :country do
3
+
4
+ def self.build raw
5
+ code = get_str raw['make_id']
6
+ title = get_str raw['make_display']
7
+ is_common = get_boolean raw['make_is_common']
8
+ country = get_str raw['make_country']
9
+
10
+ new code, title, is_common, country
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,62 @@
1
+ module Carquery
2
+ Trim = BaseStruct.new :id, :title, :make_code, :trim, :year, :body, :engine_position,
3
+ :engine_cc, :engine_cyl, :engine_type, :engine_valves_per_cyl,
4
+ :engine_power_ps, :engine_power_rpm, :engine_torque_nm, :engine_torque_rpm,
5
+ :engine_bore_mm, :engine_stroke_mm, :engine_compression, :engine_fuel,
6
+ :top_speed_kph, :speedup_0_to_100_kph, :drive, :transmission_type,
7
+ :seats, :doors, :weight_kg, :length_mm, :width_mm, :height_mm,
8
+ :wheelbase_mm, :lkm_hwy, :lkm_mixed, :lkm_city, :fuel_cap_l, :sold_in_us,
9
+ :co2, :make_title, :make_country do
10
+
11
+ def self.build raw
12
+ id = get_i raw["model_id"]
13
+ title = get_str raw["model_name"]
14
+ make_code = get_str raw["model_make_id"]
15
+ trim = get_str raw["model_trim"]
16
+ year = get_i raw["model_year"]
17
+ body = get_str raw["model_body"]
18
+ engine_position = get_str raw["model_engine_position"]
19
+ engine_cc = get_i raw["model_engine_cc"]
20
+ engine_cyl = get_i raw["model_engine_cyl"]
21
+ engine_type = get_str raw["model_engine_type"]
22
+ engine_valves_per_cyl = get_i raw["model_engine_valves_per_cyl"]
23
+ engine_power_ps = get_i raw["model_engine_power_ps"]
24
+ engine_power_rpm = get_i raw["model_engine_power_rpm"]
25
+ engine_torque_nm = get_i raw["model_engine_torque_nm"]
26
+ engine_torque_rpm = get_i raw["model_engine_torque_rpm"]
27
+ engine_bore_mm = get_f raw["model_engine_bore_mm"]
28
+ engine_stroke_mm = get_f raw["model_engine_stroke_mm"]
29
+ engine_compression = get_f raw["model_engine_compression"]
30
+ engine_fuel = get_str raw["model_engine_fuel"]
31
+ top_speed_kph = get_i raw["model_top_speed_kph"]
32
+ speedup_0_to_100_kph = get_f raw["model_0_to_100_kph"]
33
+ drive = get_str raw["model_drive"]
34
+ transmission_type = get_str raw["model_transmission_type"]
35
+ seats = get_i raw["model_seats"]
36
+ doors = get_i raw["model_doors"]
37
+ weight_kg = get_i raw["model_weight_kg"]
38
+ length_mm = get_i raw["model_length_mm"]
39
+ width_mm = get_i raw["model_width_mm"]
40
+ height_mm = get_i raw["model_height_mm"]
41
+ wheelbase_mm = get_i raw["model_wheelbase_mm"]
42
+ lkm_hwy = get_f raw["model_lkm_hwy"]
43
+ lkm_mixed = get_f raw["model_lkm_mixed"]
44
+ lkm_city = get_f raw["model_lkm_city"]
45
+ fuel_cap_l = get_i raw["model_fuel_cap_l"]
46
+ sold_in_us = get_boolean raw["model_sold_in_us"]
47
+ co2 = get_i raw["model_co2"]
48
+ make_title = get_str raw["make_display"]
49
+ make_country = get_str raw["make_country"]
50
+
51
+ new id, title, make_code, trim, year, body, engine_position, engine_cc,
52
+ engine_cyl, engine_type, engine_valves_per_cyl, engine_power_ps,
53
+ engine_power_rpm, engine_torque_nm, engine_torque_rpm, engine_bore_mm,
54
+ engine_stroke_mm, engine_compression, engine_fuel, top_speed_kph,
55
+ speedup_0_to_100_kph, drive, transmission_type, seats, doors, weight_kg,
56
+ length_mm, width_mm, height_mm, wheelbase_mm, lkm_hwy, lkm_mixed, lkm_city,
57
+ fuel_cap_l, sold_in_us, co2, make_title, make_country
58
+ end
59
+ end
60
+ end
61
+
62
+
@@ -0,0 +1,3 @@
1
+ module Carquery
2
+ VERSION = "0.1.0"
3
+ end