class Leapfrog::CustomerScoring::Client

Constants

DEFAULT_URL

Attributes

url[R]

Public Class Methods

new(url=DEFAULT_URL) click to toggle source

You may initialize the Client with any url or just use the default url of β€œinternal.leapfrogonline.com/customer_scoring”.

client = Leapfrog::CustomerScoring::Client.new

– OR–

other_url = "http://example.com/customer_scoring"
client = Leapfrog::CustomerScoring::Client.new(other_url)

This is the url to which the client will make requests

# File lib/leapfrog/customer_scoring/client.rb, line 17
def initialize(url=DEFAULT_URL)
  @url = url
end

Public Instance Methods

get_score(income, zipcode, age) click to toggle source

Makes request to the initialized endpoint to retrieve customer scoring advice. The income, zipcode and age parameters are required. The return value is a Hash with keys containing the :propensity and :ranking for customer with the passed parameters

advice = client.get_score("50000", "60621", "35")
advice.inspect
=> "{:propensity=>0.26532, :ranking=>\"C\"}"
# File lib/leapfrog/customer_scoring/client.rb, line 31
def get_score(income, zipcode, age)
  params = {income: income, zipcode: zipcode, age: age}
  begin
    RestClient.get(url, params: params) do |response, request, result|
      case response.code
      when 200
        return ::JSON.parse(response, symbolize_names: true)
      when 422
        raise Leapfrog::CustomerScoring::InvalidInput
      when 404
        raise Leapfrog::CustomerScoring::ResourceNotFound
      when 500
        raise Leapfrog::CustomerScoring::ServerError
      else
        response.return!(request, result)
      end
    end
  rescue RestClient::RequestTimeout
    raise Leapfrog::CustomerScoring::ServerTimeout
  end
end