class Scorekeeper::CustomerScoring
Constants
- BASE_URL
- NUM_PATTERN
Attributes
age[R]
income[R]
zipcode[R]
Public Class Methods
new(args={})
click to toggle source
initialize object with args containing income, zipcode, and age
# File lib/scorekeeper/customer_scoring.rb, line 14 def initialize(args={}) @income = args.fetch(:income, nil) @zipcode = args.fetch(:zipcode, nil) @age = args.fetch(:age, nil) validate_paramters check_values end
Public Instance Methods
search()
click to toggle source
calls make_request
method to ensure there are no errors. Returns response from API call.
# File lib/scorekeeper/customer_scoring.rb, line 23 def search response = make_request begin response_hash = JSON.parse(response.body) rescue JSON::ParserError raise RequestException else response_hash end end
Private Instance Methods
check_numericity()
click to toggle source
ensures that income and age are numeric passes pattern that checks to see if there is a non-digit or negative number. If so the pattern match will not be nil and an error will be raised
# File lib/scorekeeper/customer_scoring.rb, line 61 def check_numericity check_patterns(@income.to_s) check_patterns(@age.to_s) end
check_patterns(number)
click to toggle source
# File lib/scorekeeper/customer_scoring.rb, line 74 def check_patterns(number) raise InvalidParameterException unless (number =~ NUM_PATTERN).nil? end
check_values()
click to toggle source
ensures that valid values are passed
# File lib/scorekeeper/customer_scoring.rb, line 55 def check_values check_numericity check_zip_code end
check_zip_code()
click to toggle source
ensures that zip code is all digits and that length is equal to 5
# File lib/scorekeeper/customer_scoring.rb, line 67 def check_zip_code zip_string = @zipcode.to_s raise InvalidParameterException if zip_string.size != 5 # raise InvalidParameterException if (@zipcode.to_s =~ pattern) check_patterns(zip_string) end
make_request()
click to toggle source
Handles call to API. It will return a response if successful otherwise it will raise an error.
# File lib/scorekeeper/customer_scoring.rb, line 37 def make_request query = {'income': @income, 'zipcode': @zipcode, 'age': @age} begin response = HTTParty.get(BASE_URL, query: query) # I know I can do better than this rescue Exception => e raise RequestException else response end end
validate_paramters()
click to toggle source
ensures that the necessary parameters were passed in and that values conform to parameters required by API
# File lib/scorekeeper/customer_scoring.rb, line 50 def validate_paramters raise Scorekeeper::MissingParameterException if @income.nil? || @zipcode.nil? || @age.nil? end