Module: Utils

Included in:
Smartcar::Base, Smartcar::Oauth, Smartcar::Vehicle
Defined in:
lib/smartcar/utils.rb

Overview

Utils module , provides utility methods to underlying classes

Instance Method Summary collapse

Instance Method Details

#get_config(config_name) ⇒ String

gets a given env variable, checks for existence and throws exception if not present

Parameters:

  • config_name (String)

    key of the env variable

Returns:

  • (String)

    value of the env variable

Raises:



17
18
19
20
21
# File 'lib/smartcar/utils.rb', line 17

def get_config(config_name)
  config_name = "INTEGRATION_#{config_name}" if ENV['MODE'] == 'test'
  raise Smartcar::ConfigNotFound, "Environment variable #{config_name} not found !" unless ENV[config_name]
  ENV[config_name]
end

#get_error(response) ⇒ Object

Given the response from smartcar API, returns an error object if needed

Parameters:

  • response (Object)

    response Object with status and body

Returns:

  • (Object)

    nil OR Error object



27
28
29
30
31
32
33
34
# File 'lib/smartcar/utils.rb', line 27

def get_error(response)
  status = response.status
  return nil if [200,204].include?(status)
  return Smartcar::ServiceUnavailableError.new("Service Unavailable - #{response.body}") if status == 404
  return Smartcar::BadRequestError.new("Bad Request - #{response.body}") if status == 400
  return Smartcar::AuthenticationError.new("Authentication error") if status == 401
  return Smartcar::ExternalServiceError.new("API error - #{response.body}")
end

#initialize(options = {}) ⇒ Subclass os Base

A constructor to take a hash and assign it to the instance variables

Parameters:

  • options (defaults to: {})

    = {} [Hash] Could by any class's hash, but the first level keys should be defined in the class

Returns:

  • (Subclass os Base)

    Returns object of any subclass like Report



7
8
9
10
11
# File 'lib/smartcar/utils.rb', line 7

def initialize(options = {})
  options.each do |attribute, value|
    instance_variable_set("@#{attribute}", value)
  end
end