Sha256: fb758c1637bcaee52dde27b925e53ae4c7e02d879d33293b168a14ed0caaa819

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

module HttpRestClient

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

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

  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))
    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

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

  # TODO: deprecate this and start using make_request in services as it is used in vin_service and uvc_service
  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

  # TODO: deprecate and start using mappers lib/mappers as in vin_service and uvc_service
  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blackbook_client-1.0.0 lib/http_rest_client.rb
blackbook_client-0.2.1 lib/http_rest_client.rb