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
-
#get_config(config_name) ⇒ String
gets a given env variable, checks for existence and throws exception if not present.
-
#get_error(response) ⇒ Object
Given the response from smartcar API, returns an error object if needed.
-
#initialize(options = {}) ⇒ Subclass os Base
A constructor to take a hash and assign it to the instance variables.
Instance Method Details
#get_config(config_name) ⇒ String
gets a given env variable, checks for existence and throws exception if not present
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
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
7 8 9 10 11 |
# File 'lib/smartcar/utils.rb', line 7 def initialize( = {}) .each do |attribute, value| instance_variable_set("@#{attribute}", value) end end |