Class: InstaVIN::Reports

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/instavin/reports.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ Reports

Returns a new instance of Reports.



9
10
11
12
13
14
15
# File 'lib/instavin/reports.rb', line 9

def initialize(access_token=nil)
  if access_token.nil? != true
    @auth = { username: access_token, password: InstaVIN.config.api_key }
  else
    @auth = { username: InstaVIN.config.username, password: InstaVIN.config.password }
  end
end

Instance Method Details

#access_token(username, password) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
# File 'lib/instavin/reports.rb', line 49

def access_token(username, password)
  response = self.class.post("/account/access_token",
    body: { api_key: InstaVIN.config.api_key },
    basic_auth: { username: username, password: password }
  ).parsed_response

  check_response_for_errors(response)
  raise DeveloperError, "InstaVIN did not respond with an access token" if response["token"].nil? || response["token"].empty?
  response["token"]
end

#order_report(report_type, options) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/instavin/reports.rb', line 21

def order_report(report_type, options)
  raise ArgumentError, "A VIN is required" if options[:vin].nil? || options[:vin].empty?

  response = self.class.post("/order/#{report_type}", { body: options, basic_auth: @auth })
  parsed_response = response.parsed_response
  check_response_for_errors parsed_response
  parsed_response["items"].map do |report_item|
    check_report_for_error report_item

    raise NoDataError if report_item["report_summary"].nil?
    report_summary = report_item["report_summary"]
    {
      vin: report_summary["vin"],
      json_url: report_summary["url"],
      html_url: report_summary["html_url"],
      pdf_url: report_summary["pdf_url"]
    }
  end
end

#set_auth_with_token(access_token) ⇒ Object



17
18
19
# File 'lib/instavin/reports.rb', line 17

def set_auth_with_token(access_token)
  @auth = { username: access_token, password: InstaVIN.config.api_key }
end

#svc_report(vin) ⇒ Object



45
46
47
# File 'lib/instavin/reports.rb', line 45

def svc_report(vin)
  order_report("SVC", vin: vin)
end

#vtr_report(vin) ⇒ Object



41
42
43
# File 'lib/instavin/reports.rb', line 41

def vtr_report(vin)
  order_report("VTR", vin: vin)
end