class Ruze::Kamereon

Constants

BASE_URL
COUNTRY

Attributes

gigya_token[R]
person_id[R]

Public Class Methods

new(person_id, gigya_token, vin = nil) click to toggle source
# File lib/ruze/kamereon.rb, line 9
def initialize(person_id, gigya_token, vin = nil)
  raise ArgumentError unless person_id.is_a?(String) && gigya_token.is_a?(String)

  @person_id = person_id
  @gigya_token = gigya_token
  @vin = vin
end

Public Instance Methods

account_id() click to toggle source
# File lib/ruze/kamereon.rb, line 18
def account_id
  accounts.first['accountId']
end
accounts() click to toggle source
# File lib/ruze/kamereon.rb, line 22
def accounts
  @accounts ||= return_from get(
    uri("/persons/#{person_id}?country=#{COUNTRY}"),
    headers
  ), keys: %w[accounts]
end
battery() click to toggle source
# File lib/ruze/kamereon.rb, line 40
def battery
  @battery ||= return_from get(
    uri("/accounts/#{account_id}/kamereon/kca/car-adapter/v2/cars/#{vin}/battery-status?country=#{COUNTRY}"),
    headers
  ), keys: %w[data attributes]
end
cockpit() click to toggle source
# File lib/ruze/kamereon.rb, line 47
def cockpit
  @cockpit ||= return_from get(
    uri("/accounts/#{account_id}/kamereon/kca/car-adapter/v2/cars/#{vin}/cockpit?country=#{COUNTRY}"),
    headers
  ), keys: %w[data attributes]
end
location() click to toggle source
# File lib/ruze/kamereon.rb, line 54
def location
  @location ||= return_from get(
    uri("/accounts/#{account_id}/kamereon/kca/car-adapter/v1/cars/#{vin}/location?country=#{COUNTRY}"),
    headers
  ), keys: %w[data attributes]
end
vehicles() click to toggle source
# File lib/ruze/kamereon.rb, line 29
def vehicles
  @vehicles ||= return_from get(
    uri("/accounts/#{account_id}/vehicles?country=#{COUNTRY}"),
    headers
  ), keys: %w[vehicleLinks]
end
vin() click to toggle source
# File lib/ruze/kamereon.rb, line 36
def vin
  @vin ||= vehicles.first.dig('vehicleDetails', 'vin')
end

Private Instance Methods

api_key() click to toggle source
# File lib/ruze/kamereon.rb, line 63
def api_key
  ENV.fetch('KAMEREON_API_KEY')
end
get(url, headers) click to toggle source
# File lib/ruze/kamereon.rb, line 78
def get(url, headers)
  Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http|
    request = Net::HTTP::Get.new(url)
    headers.each_pair { |key, value| request[key] = value }
    http.request(request)
  end
end
headers() click to toggle source
# File lib/ruze/kamereon.rb, line 67
def headers
  {
    'apikey'           => api_key,
    'x-gigya-id_token' => gigya_token
  }
end
return_from(response, keys:) click to toggle source
# File lib/ruze/kamereon.rb, line 86
def return_from(response, keys:)
  unless response.is_a?(Net::HTTPOK)
    caller = caller_locations(1, 1)[0].label
    raise Error, "Error in #{caller}: #{response.message} (#{response.code})"
  end

  json = JSON.parse(response.body)
  json.dig(*keys)
end
uri(path) click to toggle source
# File lib/ruze/kamereon.rb, line 74
def uri(path)
  URI("#{BASE_URL}#{path}")
end