Class: InstantQuote::Adapters::Iwoca

Inherits:
InstantQuote::Adapter show all
Defined in:
lib/instant_quote/adapters/iwoca.rb

Constant Summary

Constants inherited from InstantQuote::Adapter

InstantQuote::Adapter::DEFAULT_ADDITIONAL_FIELDS

Instance Method Summary collapse

Methods inherited from InstantQuote::Adapter

#accept_offer, accept_offer, additional_fields, #get_approval, get_approval, get_link, get_preapproval, #get_preapproval, get_quote, get_status

Instance Method Details

Gets the URL for the given customer_id.



111
112
113
114
115
116
117
# File 'lib/instant_quote/adapters/iwoca.rb', line 111

def get_link(connection)
  response = ::Iwoca::Customer.(customer_id: connection.external_id)

  return response.data[:login_link] if response.success?

  raise_error(response)
end

#get_quote(params, _connection) ⇒ Object

Creates the iwoca customer and application and returns the customer_id from the response. The params come from the connection translator that returns 3 hashes:

  • company: The company information

  • people: The people information

  • requests: The requests information



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/instant_quote/adapters/iwoca.rb', line 65

def get_quote(params, _connection)
  company = params.dig(:data, :company)
  people = params.dig(:data, :people)
  requests = params.dig(:data, :requests)

  # We can only send company and people
  response = ::Iwoca::Customer.create(
    data: {
      data: {
        company: company,
        people: people
      }
    }
  )

  raise_error(response) unless response.success?

  customer_id = response.data[:customer_id]


  # This returns an application_id but we can't store it since we don't have more than one
  # external ID field. We shouldn't need it anyway.
  ::Iwoca::Application.create(
    customer_id: response.data[:customer_id],
    data: { data: { requests: requests }}
  )

  return customer_id if response.success?

  raise_error(response)
end

#get_status(customer_id, _connection) ⇒ Object

Get the current status for the created application.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/instant_quote/adapters/iwoca.rb', line 98

def get_status(customer_id, _connection)
  status_response = ::Iwoca::Application.latest(customer_id: customer_id)

  raise_error(status_response) unless status_response.success?

  # Get the offers for the application.
  app_id = status_response.data[:application_id]
  offers_response = ::Iwoca::Application.offers(application_id: app_id)

  decision_parser.new({ status: status_response.data, offers: offers_response.data })
end