module Smartcar
Main Smartcar
umbrella module
Constants
- API_ORIGIN
Host to connect to smartcar
- AUTH_ORIGIN
Path for smartcar oauth
- PATHS
- UNITS
Constant for units
- VERSION
Gem current version number
Public Class Methods
Module method Used to get api version to be used. This is the getter for the class instance variable @api_version
@return [String] api version number without 'v' prefix
# File lib/smartcar.rb, line 51 def get_api_version instance_variable_get('@api_version') end
Module method Used to check compatiblity for VIN and scope
API Documentation - smartcar.com/docs/api#connect-compatibility @param vin [String] VIN of the vehicle to be checked @param scope [Array of Strings] - array of scopes @param country [String] An optional country code according to [ISO 3166-1 alpha-2](en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Defaults to US. @param options [Hash] Other optional parameters including overrides @option options [String] :client_id Client ID that overrides ENV @option options [String] :client_secret Client Secret that overrides ENV @option options [String] :version API version to use, defaults to what is globally set @option options [Hash] :flags A hash of flag name string as key and a string or boolean value. @option options [Boolean] :test_mode Wether to use test mode or not. @option options [String] :test_mode_compatibility_level this is required argument while using test mode with a real vin. For more information refer to docs.
@return [OpenStruct] And object representing the JSON response mentioned in smartcar.com/docs/api#connect-compatibility
and a meta attribute with the relevant items from response headers.
# File lib/smartcar.rb, line 74 def get_compatibility(vin:, scope:, country: 'US', options: {}) raise InvalidParameterValue.new, 'vin is a required field' if vin.nil? raise InvalidParameterValue.new, 'scope is a required field' if scope.nil? base_object = Base.new( { version: options[:version] || Smartcar.get_api_version, auth_type: Base::BASIC } ) base_object.token = generate_basic_auth(options, base_object) base_object.build_response(*base_object.fetch( { path: PATHS[:compatibility], query_params: build_compatibility_params(vin, scope, country, options) } )) end
Module method Used to get user id
API Documentation - smartcar.com/docs/api#get-user @param token [String] Access token
@return [OpenStruct] And object representing the JSON response mentioned in smartcar.com/docs/api#get-user
and a meta attribute with the relevant items from response headers.
# File lib/smartcar.rb, line 102 def get_user(token:, version: Smartcar.get_api_version) base_object = Base.new( { token: token, version: version } ) base_object.build_response(*base_object.fetch({ path: PATHS[:user] })) end
Module method Returns a paged list of all vehicles connected to the application for the current authorized user.
API Documentation - smartcar.com/docs/api#get-all-vehicles @param token [String] - Access token @param paging [Hash] - Optional filter parameters (check documentation)
@return [OpenStruct] And object representing the JSON response mentioned in smartcar.com/docs/api#get-all-vehicles
and a meta attribute with the relevant items from response headers.
# File lib/smartcar.rb, line 120 def get_vehicles(token:, paging: {}, version: Smartcar.get_api_version) base_object = Base.new( { token: token, version: version } ) base_object.build_response(*base_object.fetch( { path: PATHS[:vehicles], query_params: paging } )) end
Module method to generate hash challenge for webhooks. It does HMAC_SHA256(amt, challenge)
@param amt [String] - Application Management Token @param challenge [String] - Challenge string
@return [String] String representing the hex digest
# File lib/smartcar.rb, line 141 def hash_challenge(amt, challenge) OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), amt, challenge) end
Module method Used to set api version to be used. This method can be used at the top to set the version and any following request will use the version set here unless overridden separately. @param version [String] version to be set without 'v' prefix.
# File lib/smartcar.rb, line 43 def set_api_version(version) instance_variable_set('@api_version', version) end
Module method used to verify webhook payload with AMT and signature.
@param amt [String] - Application Management Token @param signature [String] - sc-signature header value @param body [Object] - webhook response body
@return [true, false] - true if signature matches the hex digest of amt and body
# File lib/smartcar.rb, line 152 def verify_payload(amt, signature, body) hash_challenge(amt, body.to_json) == signature end
Private Class Methods
# File lib/smartcar.rb, line 158 def build_compatibility_params(vin, scope, country, options) query_params = { vin: vin, scope: scope.join(' '), country: country } query_params[:flags] = options[:flags].map { |key, value| "#{key}:#{value}" }.join(' ') if options[:flags] query_params[:mode] = options[:test_mode].is_a?(TrueClass) ? 'test' : 'live' unless options[:test_mode].nil? if options[:test_mode_compatibility_level] query_params[:test_mode_compatibility_level] = options[:test_mode_compatibility_level] query_params[:mode] = 'test' end query_params end
returns auth token for Basic auth
@return [String] Base64 encoding of CLIENT:SECRET
# File lib/smartcar.rb, line 179 def generate_basic_auth(options, base_object) client_id = options[:client_id] || base_object.get_config('SMARTCAR_CLIENT_ID') client_secret = options[:client_secret] || base_object.get_config('SMARTCAR_CLIENT_SECRET') Base64.strict_encode64("#{client_id}:#{client_secret}") end