lib/revo/loans_api/client.rb in revo-loans_api-0.0.7 vs lib/revo/loans_api/client.rb in revo-loans_api-0.0.8
- old
+ new
@@ -31,31 +31,44 @@
def create_loan_request(options)
response = make_request(:post, 'loan_requests', loan_request_params(options))
return response unless response.success?
@loan_request_token = response.response.dig(:loan_request, :token)
- Result.new(
- success?: true,
- response: {
- token: loan_request_token,
- terms: loan_request_terms.response[:loan_request]
- }
- )
+ terms = loan_request_terms
+ return terms unless terms.success?
+
+ Result.new(success?: true,
+ response: {
+ token: loan_request_token,
+ terms: terms.response[:loan_request]
+ })
end
def update_loan_request(token:, options:)
update_params = { loan_request: options }
response = make_request(:put, "loan_requests/#{token}", update_params)
return response unless response.success?
+ @loan_request_token = token
+ terms = loan_request_terms
+ return terms unless terms.success?
+
Result.new(
success?: true,
- response: {}
+ response: {
+ terms: terms.response[:loan_request]
+ }
)
end
+ def get_loan_request_info(token:, amount:)
+ result = make_request(:get, "loan_requests/#{token}?amount=#{amount}")
+
+ result.success? ? result.response[:loan_request] : []
+ end
+
# prerequisite: a client with the LR's phone number should already exist
def document(token:, type:, format: 'html')
make_request(:get, "loan_requests/#{token}/documents/#{type}.#{format}")
end
@@ -143,10 +156,13 @@
def connection
@connection ||= HTTP.persistent(base_url)
end
def loan_request_terms(&block)
- make_request(:get, "loan_requests/#{loan_request_token}", &block)
+ result = make_request(:get, "loan_requests/#{loan_request_token}", &block)
+ return result if result.success?
+
+ Result.new(success?: false, response: { errors: { loan_request_terms: :cant_fetch } })
end
def make_request(method, endpoint, params = {}, &block)
headers = { Authorization: session_token }.compact
response = connection.public_send(method, url_for(endpoint), json: params, headers: headers)