Class: DealervaultApi::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dealervault_api/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, subscription_key, config = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



6
7
8
9
10
11
12
# File 'lib/dealervault_api/api_client.rb', line 6

def initialize(user,subscription_key, config = {})
  @host = "https://authenticom.azure-api.net/dv-delivery/v1"
  @user_agent = "DealerVault-Api/#{VERSION}/ruby"

  set_config(config.merge!({user: user,
                            subscription_key: subscription_key}))
end

Instance Method Details

#call_api(http_method, path, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dealervault_api/api_client.rb', line 19

def call_api(http_method, path, opts = {})
  headers = {'X-User' => @user, 'Content-Type' => "application/json", 'Ocp-Apim-Subscription-Key': @subscription_key}
  if opts[:headers]
    headers.merge!(opts[:headers])
  end

  conn = Faraday.new(
    url: @host,
    params: opts[:query_params],
    headers: headers
  ) do |f|
    f.response :json # decode response bodies as JSON
  end
  res = nil
  case http_method.to_sym.downcase
  when :post, :put, :patch, :delete
    res = conn.run_request(http_method.to_sym.downcase, path,  opts[:body].to_json, nil)
  when :get
    res = conn.run_request(:get, path,  nil, nil)

  else
  end

  data = nil
  data = res.body if res.status == 200
  data = "success" if res.status == 204

  unless data
    fail ApiError.new(:status => res.status, :response_body => res.body)
  end

  data
end

#set_config(config = {}) ⇒ Object



14
15
16
17
# File 'lib/dealervault_api/api_client.rb', line 14

def set_config(config = {})
  @user= config[:user] || ''
  @subscription_key = config[:subscription_key] || ''
end