class DaClient

Attributes

account[RW]
env[RW]
host[RW]
token[RW]

Public Class Methods

new(token: , account: , env: "production") click to toggle source
Calls superclass method
# File lib/da_client.rb, line 6
def initialize(token: , account: , env: "production")
  @token = token
  @account = account
  @env = env
  @host = default_host
  super()
end

Public Instance Methods

default_host() click to toggle source
# File lib/da_client.rb, line 14
def default_host
  return ENV["DA_CLIENT_HOST"] if ENV["DA_CLIENT_HOST"]
  env == 'production' ? 'https://app.dealeraccelerate.com' : 'https://app.dealer-accelerate-staging.com'
end
vehicles(q: {}, page: 1, per_page: 20, token: @token) click to toggle source
# File lib/da_client.rb, line 19
def vehicles(q: {}, page: 1, per_page: 20, token: @token)
  path = "api/v1/accounts/#{@account}/vehicles"
  obj = {
    q: q,
    page: page,
    per_page: per_page,
    token: token
  }

  query = to_params(obj)
  uri = "#{@host}/#{path}?#{query}"
  request(uri)
end

Private Instance Methods

request(path) click to toggle source
# File lib/da_client.rb, line 35
def request(path)
  client = Faraday.new
  request = client.get(path)
  request.body
end
to_params(hash) click to toggle source
# File lib/da_client.rb, line 41
def to_params(hash)
  params = ''
  stack = []

  hash.each do |k, v|
    if v.is_a?(Hash)
      stack << [k,v]
    else
      params << "#{k}=#{v}&"
    end
  end

  stack.each do |parent, hash|
    hash.each do |k, v|
      if v.is_a?(Hash)
        stack << ["#{parent}[#{k}]", v]
      else
        params << "#{parent}[#{k}]=#{v}&"
      end
    end
  end

  params.chop!
  params
end