smartcar 0.0.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8fd8a3a8c8239d87ed360d17d3683525c64c0662
4
- data.tar.gz: f72f37a1d07b31bcec3d14691b9f4b5f466f6963
2
+ SHA256:
3
+ metadata.gz: 77763e723a789174f3bab09883c2c4770019d73f6984acc2a20ab5580b71683d
4
+ data.tar.gz: 72ea1a37d4864c8f1e2251e15afcb8510ed07a910bdffe12abba2e6b17ff48fe
5
5
  SHA512:
6
- metadata.gz: d3e8b86b26a86136e2f9644f011aff83d25f22193e1cc8d9cb39192d2c663b07524ebbc11feafb0559e4b5465ee94be59185f9d76debc8ca3d6b95b02601fdab
7
- data.tar.gz: 2a1257fe53d9635d82b80ddcc1722308969cd6b1b89fd161d7d0627eca5d49cb6d31182d323375b887e29d52fb27635f51740ff4e22eb9d001825bd282ef07d6
6
+ metadata.gz: c8ca779368dc98843d5e623b1faa52fda1503077b932727da4ac84fd6ce2d9b35670e15c82427ee4074ea821fc025a46158ac42d63e9441eb5af25d8a12d26ad
7
+ data.tar.gz: 55c56eb58615e9ef19dfff70a8d25ec4cd61b3ac90152b63555ceb7218b15230a533302887d827b502d8b0b6f06dc3010412ae13d66e9d6757cd71aefcfaeaf7
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # byebug files
14
+ .byebug*
15
+
16
+ # ctags
17
+ .tags*
18
+
19
+ #idea files
20
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.5
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at Ashwin.Subramanian@ooma.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in smartcar.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ smartcar (0.1.1)
5
+ oauth2 (~> 1.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (11.0.1)
11
+ diff-lcs (1.3)
12
+ faraday (0.17.0)
13
+ multipart-post (>= 1.2, < 3)
14
+ jwt (2.2.1)
15
+ multi_json (1.14.1)
16
+ multi_xml (0.6.0)
17
+ multipart-post (2.1.1)
18
+ oauth2 (1.4.2)
19
+ faraday (>= 0.8, < 2.0)
20
+ jwt (>= 1.0, < 3.0)
21
+ multi_json (~> 1.3)
22
+ multi_xml (~> 0.5)
23
+ rack (>= 1.2, < 3)
24
+ rack (2.0.8)
25
+ rake (13.0.1)
26
+ rspec (3.8.0)
27
+ rspec-core (~> 3.8.0)
28
+ rspec-expectations (~> 3.8.0)
29
+ rspec-mocks (~> 3.8.0)
30
+ rspec-core (3.8.2)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-expectations (3.8.4)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-mocks (3.8.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-support (3.8.2)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 2.0)
45
+ byebug (~> 11.0)
46
+ rake (>= 12.3.3)
47
+ rspec (~> 3.0)
48
+ smartcar!
49
+
50
+ BUNDLED WITH
51
+ 2.1.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Ashwin Subramanian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+
2
+
3
+ Ruby gem library to quickly get started with the Smartcar API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'smartcar'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install smartcar
20
+
21
+ ## Usage
22
+
23
+ Setup the environment variables for CLIENT_ID and CLIENT_SECRET.
24
+ ```bash
25
+ # Get your API keys from https://dashboard.smartcar.com/signup
26
+ export CLIENT_ID=<client id>
27
+ export CLIENT_SECRET=<client secret>
28
+ ```
29
+
30
+ Example Usage for calling the reports API with oAuth token
31
+ ```ruby
32
+ 2.5.5 :003 > require 'smartcar'
33
+ => true
34
+ 2.5.5 :009 > ids = Smartcar::Vehicle.all_vehicle_ids(token: token)
35
+ => ["35e8a7c4-9e5c-4eb6-b552-7509e371669a", "c3332c35-fdeb-4780-a84b-706b7364979a", "d10ad5cf-5469-467e-972e-90427981873f", "fab5a744-6488-40d8-a6dd-41f0a804d44f"]
36
+ 2.5.5 :010 > vehicle = Smartcar::Vehicle.new(token: token, id: ids.first)
37
+ => #<Smartcar::Vehicle:0x00007fbad71aa2b8 @token="56801a5e-6a0b-4d05-a43e-52a4d5e6648f", @id="35e8a7c4-9e5c-4eb6-b552-7509e371669a", @unit_system="imperial">
38
+ 2.5.5 :011 > vehicle.permissions
39
+ => ["control_security", "read_battery", "read_charge", "read_location", "read_odometer", "read_vehicle_info", "read_vin"]
40
+ 2.5.5 :012 > vehicle.odometer
41
+ => #<Smartcar::Odometer:0x00007fbad718a3f0 @distance=74988.44443760936>
42
+ 2.5.5 :013 > vehicle.battery
43
+ => #<Smartcar::Battery:0x00007fbad50f4c80 @range=134.35, @percentRemaining=0.02>
44
+ 2.5.5 :014 > vehicle.charge
45
+ => #<Smartcar::Charge:0x00007fbad787e620 @state="FULLY_CHARGED", @isPluggedIn=true>
46
+ ```
47
+
48
+ Example Usage for oAuth -
49
+ ```ruby
50
+ # To get the redirect URL :
51
+ 2.5.5 :002 > options = {test_mode: true,scope: ["read_battery","read_charge","read_fuel","read_location","control_security","read_odometer","read_tires","read_vin","read_vehicle_info"]}
52
+ 2.5.5 :003 > require 'smartcar'
53
+ 2.5.5 :004 > url = Smartcar::Oauth.authorization_url(options)
54
+ => "https://connect.smartcar.com/oauth/authorize?approval_prompt=auto&client_id=2715c6b2-eba8-4fda-85b1-8d849733a344&mode=test&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fcallback&response_type=code&scope=read_battery+read_charge+read_fuel+read_location+control_security+read_odometer+read_tires+read_vin+read_vehicle_info"
55
+ # Redirect user to the above URL.
56
+ # After authentication user control reaches the callback URL with code.
57
+ # Use the code from the parameters and request a token
58
+ 2.5.5 :006 > token_hash = Smartcar::Oauth.get_token(code)
59
+ => {"token_type"=>"Bearer", :access_token=>"56801a5e-6a0b-4d05-a43e-52a4d5e6648f", :refresh_token=>"4f46e7e4-28c5-47b3-ba8d-7dcef73d05dd", :expires_at=>1577875279}
60
+ # This access_token can be used to call the Smartcar APIs as given above.
61
+ # Store this hash and if it expired refresh the token OR use the code again to
62
+ # get a new token or use .
63
+ ```
64
+
65
+ ## Development
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ To contribute, please:
72
+
73
+ 1. Open an issue for the feature (or bug) you would like to resolve.
74
+ 2. Resolve the issue and add tests in your feature branch.
75
+ 3. Open a PR from your feature branch into `develop` that tags the issue.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "smartcar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,72 @@
1
+ require 'oauth2'
2
+ require 'base64'
3
+ module Smartcar
4
+ # The Base class for all of the other class.
5
+ # Let other classes inherit from here and put common methods here.
6
+ #
7
+ # @author [ashwin]
8
+ #
9
+ class Base
10
+ class InvalidParameterValue < StandardError; end
11
+ BEARER = 'BEARER'.freeze
12
+ BASIC = 'BASIC'.freeze
13
+
14
+ attr_accessor :token
15
+ # meta programming and define all Restful methods.
16
+ # @param path [String] the path to hit for the request.
17
+ # @param token [String] the access token to be used.
18
+ #
19
+ # @return [Hash] The response Json parsed as a hash.
20
+ %i{get post patch put delete}.each do |verb|
21
+ define_method verb do |path, data=nil|
22
+ response = service.send(verb) do |request|
23
+ request.headers['Authorization'] = "BEARER #{token}"
24
+ request.headers['Authorization'] = "BASIC #{get_basic_auth}" if data[:auth] == BASIC
25
+ request.headers['sc-unit-system'] = unit_system
26
+ request.headers['Content-Type'] = "application/json"
27
+ complete_path = "/#{API_VERSION}#{path}"
28
+ if verb==:get
29
+ request.url complete_path, data
30
+ else
31
+ request.url complete_path
32
+ request.body = data if data
33
+ end
34
+ end
35
+ status = response.status
36
+ raise ServiceUnavailableError.new, "Service Unavailable - #{response.body}" if status == 404
37
+ raise BadRequestError.new, "Bad Request - #{response.body}" if status == 400
38
+ raise AuthenticationError.new, "Authentication error" if status == 401
39
+ raise ExternalServiceError.new, "API error - #{response.body}" unless [200,204].include?(status)
40
+ JSON.parse(response.body)
41
+ end
42
+ end
43
+
44
+ # This requires a proc 'PATH' to be defined in the class
45
+ # @param token [String] Access token
46
+ # @param token [String] Vechicle ID
47
+ #
48
+ # @return [Object]
49
+ def fetch(path: , options: {}, auth: 'BEARER')
50
+ _path = path
51
+ _path += "?#{URI.encode_www_form(options)}" unless options.empty?
52
+ get(_path, {auth: auth})
53
+ end
54
+
55
+ private
56
+
57
+ # returns auth token for BASIC auth
58
+ #
59
+ # @return [String] Base64 encoding of CLIENT:SECRET
60
+ def get_basic_auth
61
+ Base64.strict_encode64("#{ENV['CLIENT_ID']}:#{ENV['CLIENT_SECRET']}")
62
+ end
63
+
64
+ # gets a smartcar API service/client
65
+ # @param token [String] Access token.
66
+ #
67
+ # @return [OAuth2::AccessToken] An initialized AccessToken instance that acts as service client
68
+ def service
69
+ @service ||= Faraday.new(url: SITE)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,14 @@
1
+ module Smartcar
2
+ # class to represent battery info
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Battery < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/battery"}
9
+ attr_accessor :percentRemaining, :range
10
+
11
+ # just to have Ruby-esque method names
12
+ alias_method :percentage_remaining, :percentRemaining
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Smartcar
2
+ # class to represent Charge info
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Charge < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/charge"}
9
+ attr_accessor :isPluggedIn, :state
10
+
11
+ # just to have Ruby-esque method names
12
+ alias_method :is_plugged_in?, :isPluggedIn
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Smartcar
2
+ # class to represent Engine oil life
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class EngineOil < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/engine/oil"}
9
+ attr_accessor :lifeRemaining
10
+
11
+ # just to have Ruby-esque method names
12
+ alias_method :life_remaining, :lifeRemaining
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module Smartcar
2
+ # class to represent Fuel info
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Fuel < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/fuel"}
9
+ attr_accessor :amountRemaining, :percentRemaining, :range
10
+
11
+ # just to have Ruby-esque method names
12
+ alias_method :amount_remaining, :amountRemaining
13
+ alias_method :percent_remaining, :percentRemaining
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Smartcar
2
+ # class to represent Location info
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Location < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/location"}
9
+ attr_accessor :latitude, :longitude
10
+ end
11
+ end
@@ -0,0 +1,93 @@
1
+ module Smartcar
2
+ # Oauth class to take care of the Oauth 2.0 with genomelink APIs
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Oauth < Base
7
+ class << self
8
+ # Generate the OAuth authorization URL.
9
+ #
10
+ # By default users are not shown the permission dialog if they have already
11
+ # approved the set of scopes for this application. The application can elect
12
+ # to always display the permissions dialog to the user by setting
13
+ # approval_prompt to `force`.
14
+ #
15
+ # @param options [Hash]
16
+ # @param options[:state] [String] - OAuth state parameter passed to the
17
+ # redirect uri. This parameter may be used for identifying the user who
18
+ # initiated the request.
19
+ # @param options[:test_mode] [Boolean] - Setting this to 'true' runs it in test mode.
20
+ # @param options[:force_prompt] [Boolean] - Setting `force_prompt` to
21
+ # `true` will show the permissions approval screen on every authentication
22
+ # attempt, even if the user has previously consented to the exact scope of
23
+ # permissions.
24
+ # @param options[:make] [String] - `make' is an optional parameter that allows
25
+ # users to bypass the car brand selection screen.
26
+ # For a complete list of supported makes, please see our
27
+ # [API Reference](https://smartcar.com/docs/api#authorization) documentation.
28
+ # @param options[:scope] [Array of Strings] - array of scopes that specify what the user can access
29
+ # EXAMPLE : ['read_odometer', 'read_vehicle_info', 'required:read_location']
30
+ # For further details refer to https://smartcar.com/docs/guides/scope/
31
+ #
32
+ # @return [String] URL where user needs to be redirected for authorization
33
+ def authorization_url(options)
34
+ parameters = {
35
+ redirect_uri: get_config('REDIRECT_URI'),
36
+ approval_prompt: options[:force_prompt] ? FORCE : AUTO,
37
+ mode: options[:test_mode] ? TEST : LIVE,
38
+ response_type: CODE
39
+ }
40
+ parameters[:scope] = options[:scope].join(' ') if options[:scope]
41
+ %I(state make).each do |parameter|
42
+ parameters[:parameter] = options[:parameter] unless options[:parameter].nil?
43
+ end
44
+
45
+ client.auth_code.authorize_url(parameters)
46
+ end
47
+
48
+ # [get_token description]
49
+ # @param auth_code [String] This is the code that is returned after use
50
+ # visits and authorizes on the authorization URL.
51
+ #
52
+ # @return [Hash] Hash of token, refresh token, expiry info and token type
53
+ def get_token(auth_code)
54
+ client.auth_code
55
+ .get_token(
56
+ auth_code,
57
+ redirect_uri: get_config('REDIRECT_URI')
58
+ ).to_hash
59
+ end
60
+
61
+ # [refresh_token description]
62
+ # @param token_hash [Hash] This is the hash that is returned with the
63
+ # get_token method
64
+ #
65
+ # @return [Hash] Hash of token, refresh token, expiry info and token type
66
+ def refresh_token(token_hash)
67
+ token_object = OAuth2::AccessToken.from_hash(client, token_hash)
68
+ token_object = token_object.refresh!
69
+ token_object.to_hash
70
+ end
71
+
72
+ private
73
+ # gets the Oauth Client object
74
+ #
75
+ # @return [OAuth2::Client] A Oauth Client object.
76
+ def client
77
+ @client ||= OAuth2::Client.new( get_config('CLIENT_ID'),
78
+ get_config('CLIENT_SECRET'),
79
+ :site => OAUTH_PATH
80
+ )
81
+ end
82
+
83
+ # gets a given env variable, checks for existence and throws exception if not present
84
+ # @param config_name [String] key of the env variable
85
+ #
86
+ # @return [String] value of the env variable
87
+ def get_config(config_name)
88
+ raise ConfigNotFound, "Environment variable #{config_name} not found !" unless ENV[config_name]
89
+ ENV[config_name]
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,11 @@
1
+ module Smartcar
2
+ # class to get Charge info
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Odometer < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/odometer"}
9
+ attr_accessor :distance
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Smartcar
2
+ # class to get Charge info
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Permissions
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/permissions"}
9
+ attr_accessor :permissions
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Smartcar
2
+ # class to represent Engine oil life
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class TirePressure < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/tires/pressure"}
9
+ attr_accessor :backLeft, :backRight, :frontLeft, :frontRight
10
+
11
+ # just to have Ruby-esque method names
12
+ alias_method :back_left, :backLeft
13
+ alias_method :back_right, :backRight
14
+ alias_method :front_left, :frontLeft
15
+ alias_method :front_right, :frontRight
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module Smartcar
2
+ # Class to get to user API.
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class User < Base
7
+ USER_PATH = '/user'.freeze
8
+ attr_accessor :id, :token
9
+
10
+ def initialize(token:)
11
+ raise InvalidParameterValue.new, "Access Token(token) is a required field" if token.nil?
12
+ @token = token
13
+ end
14
+
15
+ # Class method Used to get user id
16
+ # EX : Smartcar::User.fetch
17
+ # API - https://smartcar.com/docs/api#get-user
18
+ # @param token [String] Access token
19
+ #
20
+ # @return [User] object
21
+ def self.user_id(token:)
22
+ new(token: token).get(USER_PATH)['id']
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ module Utils
2
+ # A constructor to take a hash and assign it to the instance variables
3
+ # @param options = {} [Hash] Could by any class's hash, but the first level keys should be defined in the class
4
+ #
5
+ # @return [Subclass os Base] Returns object of any subclass like Report
6
+ def initialize(options = {})
7
+ options.each do |attribute, value|
8
+ instance_variable_set("@#{attribute}", value)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,186 @@
1
+ module Smartcar
2
+ # Vehicle class to connect to vehicle basic info,disconnect, lock unlock and get all vehicles API
3
+ # For ease of use, this also has methods define to be able to call other resources on a vehicle object
4
+ # For Ex. Vehicle object will be treate as an entity and doing vehicle_object.
5
+ # Battery should return Battery object.
6
+ #
7
+ # @author [ashwin]
8
+ #
9
+ class Vehicle < Base
10
+
11
+ COMPATIBLITY_PATH = '/compatibility'.freeze
12
+ PATH = Proc.new{|id| "/vehicles/#{id}"}
13
+ attr_accessor :token, :id, :unit_system
14
+
15
+
16
+ def initialize(token:, id:, unit_system: IMPERIAL)
17
+ raise InvalidParameterValue.new, "Invalid Units provided : #{unit_system}" unless UNITS.include?(unit_system)
18
+ raise InvalidParameterValue.new, "Vehicle ID (id) is a required field" if id.nil?
19
+ raise InvalidParameterValue.new, "Access Token(token) is a required field" if token.nil?
20
+ @token = token
21
+ @id = id
22
+ @unit_system = unit_system
23
+ end
24
+
25
+ # Accessor method for vehicle attributes.
26
+ %I(make model year).each do |method_name|
27
+ define_method method_name do
28
+ vehicle_attributes.send(method_name)
29
+ end
30
+ end
31
+
32
+ # Class method Used to get all the vehicles in the app. This only returns
33
+ # API - https://smartcar.com/docs/api#get-all-vehicles
34
+ # @param token [String] - Access token
35
+ # @param options [Hash] - Optional filter parameters (check documentation)
36
+ #
37
+ # @return [Array] of vehicle IDs(Strings)
38
+ def self.all_vehicle_ids(token:, options: {})
39
+ new(token: token, id: 'none').fetch(
40
+ path: PATH.call(''),
41
+ options: options
42
+ )['vehicles']
43
+ end
44
+
45
+ # Class method Used to check compatiblity for VIN and scope
46
+ # API - https://smartcar.com/docs/api#connect-compatibility
47
+ # @param vin [String] VIN of the vehicle to be checked
48
+ # @param scope [Array of Strings] - array of scopes
49
+ #
50
+ # @return [Boolean] true or false
51
+ def self.compatible?(vin:, scope:)
52
+ raise InvalidParameterValue.new, "vin is a required field" if vin.nil?
53
+ raise InvalidParameterValue.new, "scope is a required field" if scope.nil?
54
+
55
+ new(token: 'none', id: 'none').fetch(path: COMPATIBLITY_PATH,
56
+ options: {
57
+ vin: vin,
58
+ scope: scope.join(' ')
59
+ },
60
+ auth: BASIC
61
+ )['compatible']
62
+ end
63
+
64
+ # Fetch the list of permissions that this application has been granted for
65
+ # this vehicle
66
+ # EX : Smartcar::Vehicle.new(token: token, id: id).permissions
67
+ # @param options [Hash] - Optional filter parameters (check documentation)
68
+ #
69
+ # @return [Array] of permissions (Strings)
70
+ def permissions(options: {})
71
+ Permissions.new(fetch(
72
+ path: Permissions::PATH.call(id),
73
+ options: options
74
+ )).permissions
75
+ end
76
+
77
+ # Method Used toRevoke access for the current requesting application
78
+ # API - https://smartcar.com/docs/api#delete-disconnect
79
+ #
80
+ # @return [Boolean] true if success
81
+ def disconnect!
82
+ response = delete(PATH.call(id) + "/application")
83
+ response['status'] == SUCCESS
84
+ end
85
+
86
+ # Methods Used lock or unlock car
87
+ # API - https://smartcar.com/docs/api#post-security
88
+ #
89
+ # @return [Boolean] true if success
90
+ %w(lock unlock).each do |method_name|
91
+ define_method "#{method_name}!" do
92
+ lock_or_unlock!(action: Smartcar.const_get(method_name.upcase))
93
+ end
94
+ end
95
+
96
+ # Following section defined methods using meta programing to fetch various
97
+ # details of a vehicle. The key is the method name, and value is Class that
98
+ # wraps the data.
99
+ {
100
+ # Returns the state of charge (SOC) and remaining range of an electric or
101
+ # plug-in hybrid vehicle's battery.
102
+ # API - https://smartcar.com/docs/api#get-ev-battery
103
+ #
104
+ # @return [Battery] object
105
+ battery: Battery,
106
+ # Returns the current charge status of the vehicle.
107
+ # API - https://smartcar.com/docs/api#get-ev-battery
108
+ #
109
+ # @return [Charge] object
110
+ charge: Charge,
111
+ # Returns the remaining life span of a vehicle's engine oil
112
+ # API - https://smartcar.com/docs/api#get-engine-oil-life
113
+ #
114
+ # @return [EngineOil] object
115
+ engine_oil: EngineOil,
116
+ # Returns the status of the fuel remaining in the vehicle's gas tank.
117
+ # API - https://smartcar.com/docs/api#get-fuel-tank
118
+ #
119
+ # @return [Fuel] object
120
+ fuel: Fuel,
121
+ # Returns the last known location of the vehicle in geographic coordinates.
122
+ # API - https://smartcar.com/docs/api#get-location
123
+ #
124
+ # @return [Location] object
125
+ location: Location,
126
+ # Returns the vehicle's last known odometer reading.
127
+ # API - https://smartcar.com/docs/api#get-odometer
128
+ #
129
+ # @return [Odometer] object
130
+ odometer: Odometer,
131
+ # Returns the air pressure of each of the vehicle's tires.
132
+ # API - https://smartcar.com/docs/api#get-tire-pressure
133
+ #
134
+ # @return [TirePressure] object
135
+ tire_pressure: TirePressure,
136
+ }.each do |method_name, klass|
137
+ define_method method_name do
138
+ klass.new(
139
+ fetch(
140
+ path: klass::PATH.call(id)
141
+ )
142
+ )
143
+ end
144
+ end
145
+
146
+ #NOTE : The following two also could be defined by metaprogramming,
147
+ # But these are items that dont change and hence can be cached in the
148
+ # vehicle object.
149
+
150
+ # Returns the vehicle's manufacturer identifier.
151
+ # API - https://smartcar.com/docs/api#get-vin
152
+ #
153
+ # @return [Vin] object
154
+ def vin
155
+ @vin ||= Vin.new(
156
+ fetch(
157
+ path: Vin::PATH.call(id)
158
+ )
159
+ ).vin
160
+ end
161
+
162
+ # Returns the vehicle's model make and year.
163
+ # API - https://smartcar.com/docs/api#get-vehicle-attributes
164
+ #
165
+ # @return [VehicleAttributes] object
166
+ def vehicle_attributes
167
+ @vehicle_attributes ||= VehicleAttributes.new(
168
+ fetch(
169
+ path: PATH.call(id)
170
+ )
171
+ )
172
+ end
173
+
174
+ private
175
+
176
+ def lock_or_unlock!(action:)
177
+ response = post(PATH.call(id) + "/security", {action: action}.to_json)
178
+ response['status'] == SUCCESS
179
+ end
180
+
181
+ class VehicleAttributes
182
+ include Utils
183
+ attr_accessor :id, :make, :model, :year
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,3 @@
1
+ module Smartcar
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ module Smartcar
2
+ # class to represent Engine oil life
3
+ #
4
+ # @author [ashwin]
5
+ #
6
+ class Vin < Base
7
+ include Utils
8
+ PATH = Proc.new{|id| "/vehicles/#{id}/vin"}
9
+ attr_accessor :vin
10
+ end
11
+ end
data/lib/smartcar.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "smartcar/utils"
2
+ require "smartcar/version"
3
+ require "smartcar/base"
4
+ require "smartcar/oauth"
5
+ require "smartcar/permissions"
6
+ require "smartcar/battery"
7
+ require "smartcar/charge"
8
+ require "smartcar/engine_oil"
9
+ require "smartcar/fuel"
10
+ require "smartcar/location"
11
+ require "smartcar/odometer"
12
+ require "smartcar/tire_pressure"
13
+ require "smartcar/vin"
14
+ require "smartcar/vehicle"
15
+ require "smartcar/user"
16
+
17
+
18
+ module Smartcar
19
+ class ConfigNotFound < StandardError; end
20
+ class ExternalServiceError < StandardError; end
21
+ class ServiceUnavailableError < ExternalServiceError; end
22
+ class AuthenticationError < ExternalServiceError; end
23
+ class ParserError < ExternalServiceError; end
24
+ class BadRequestError < ExternalServiceError; end
25
+ API_VERSION = "v1.0".freeze
26
+ SITE = "https://api.smartcar.com/".freeze
27
+
28
+ # Path for smartcar oauth
29
+ OAUTH_PATH = "https://connect.smartcar.com/oauth/authorize".freeze
30
+ %w(success code test live force auto metric imperial).each do |constant|
31
+ const_set(constant.upcase, constant.freeze)
32
+ end
33
+ LOCK = "LOCK".freeze
34
+ UNLOCK = "UNLOCK".freeze
35
+ UNITS = [IMPERIAL,METRIC]
36
+ end
data/ruby-sdk.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "smartcar/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "smartcar"
7
+ spec.version = Smartcar::VERSION
8
+ spec.authors = ["Ashwin Subramanian"]
9
+ spec.email = ["sshwin.subramanian@smartcar.com"]
10
+ spec.homepage = 'https://rubygems.org/gems/smartcar'
11
+ spec.summary = %q{Ruby Gem to access smartcar APIs (https://smartcar.com/docs/)}
12
+ spec.description = %q{This is a ruby gem to access the smartcar APIs. It includes the API classes and the OAuth system.}
13
+ spec.license = "MIT"
14
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 2.0"
22
+ spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3'
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "byebug", "~> 11.0"
25
+ spec.add_dependency "oauth2", "~> 1.4"
26
+ end
metadata CHANGED
@@ -1,22 +1,128 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartcar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - Philip Hayes
7
+ - Ashwin Subramanian
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
12
- dependencies: []
13
- description: ''
14
- email: hello@smartcar.com
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 12.3.3
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '12.3'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 12.3.3
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: byebug
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '11.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '11.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: oauth2
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.4'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.4'
89
+ description: This is a ruby gem to access the smartcar APIs. It includes the API classes
90
+ and the OAuth system.
91
+ email:
92
+ - sshwin.subramanian@smartcar.com
15
93
  executables: []
16
94
  extensions: []
17
95
  extra_rdoc_files: []
18
- files: []
19
- homepage: https://smartcar.com
96
+ files:
97
+ - ".gitignore"
98
+ - ".rspec"
99
+ - ".travis.yml"
100
+ - CODE_OF_CONDUCT.md
101
+ - Gemfile
102
+ - Gemfile.lock
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - bin/console
107
+ - bin/setup
108
+ - lib/smartcar.rb
109
+ - lib/smartcar/base.rb
110
+ - lib/smartcar/battery.rb
111
+ - lib/smartcar/charge.rb
112
+ - lib/smartcar/engine_oil.rb
113
+ - lib/smartcar/fuel.rb
114
+ - lib/smartcar/location.rb
115
+ - lib/smartcar/oauth.rb
116
+ - lib/smartcar/odometer.rb
117
+ - lib/smartcar/permissions.rb
118
+ - lib/smartcar/tire_pressure.rb
119
+ - lib/smartcar/user.rb
120
+ - lib/smartcar/utils.rb
121
+ - lib/smartcar/vehicle.rb
122
+ - lib/smartcar/version.rb
123
+ - lib/smartcar/vin.rb
124
+ - ruby-sdk.gemspec
125
+ homepage: https://rubygems.org/gems/smartcar
20
126
  licenses:
21
127
  - MIT
22
128
  metadata: {}
@@ -35,9 +141,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
35
141
  - !ruby/object:Gem::Version
36
142
  version: '0'
37
143
  requirements: []
38
- rubyforge_project:
39
- rubygems_version: 2.4.8
144
+ rubygems_version: 3.1.2
40
145
  signing_key:
41
146
  specification_version: 4
42
- summary: Smartcar ruby client library.
147
+ summary: Ruby Gem to access smartcar APIs (https://smartcar.com/docs/)
43
148
  test_files: []