Module: HttpRestClient

Included in:
AutocompleteService, ChromedataService, UVCService, VinService
Defined in:
lib/http_rest_client.rb

Constant Summary collapse

BASE_URL =
'https://service.blackbookcloud.com'.freeze

Instance Method Summary collapse

Instance Method Details

#headersObject



6
7
8
9
# File 'lib/http_rest_client.rb', line 6

def headers
  token = Base64.encode64("#{username}:#{password}")
  { Authorization: "Basic #{token}" }
end

#make_request(method, url, query_params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/http_rest_client.rb', line 11

def make_request(method, url, query_params = {})
  return unless [:get].include?(method)
  begin
  response = JSON.parse(HTTParty.send(method, url, headers: headers, query: query_params).body)
  status = 200

  if response['error_count'].positive? || message_with_error(response)
    response = { error: response['message_list'][0]['description'] }
    status = 400
  end

  [response, status]

  rescue => e
    [ { error: e.to_s }, 500]
  end
end

#map_fields(response) ⇒ Object

TODO: deprecate and start using mappers lib/mappers as in vin_service and uvc_service



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/http_rest_client.rb', line 48

def map_fields(response)
  return {} unless response && response['used_vehicles'] && response['used_vehicles']['used_vehicle_list']
  vehicle_range = response['used_vehicles']['used_vehicle_list'][0]

  return {
    range: {
      xclean: vehicle_range['adjusted_whole_xclean'],
      clean: vehicle_range['adjusted_whole_clean'],
      average: vehicle_range['adjusted_whole_avg'],
      rough: vehicle_range['adjusted_whole_rough']
    }
  }
end

#message_with_error(response) ⇒ Object



29
30
31
# File 'lib/http_rest_client.rb', line 29

def message_with_error(response)
  response['message_list'].any? && response['message_list'][0]['type'] == 'Error'
end

#process_response(response, transformer = nil) ⇒ Object

TODO: deprecate this and start using make_request in services as it is used in vin_service and uvc_service



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/http_rest_client.rb', line 34

def process_response(response, transformer = nil)
  status = 200

  if response['error_count'].positive?
    response = { error: response['message_list'][0]['description'] }
    status = 400
  elsif transformer
    response = send(transformer, response)
  end

  [response, status]
end