Class: CustomerScore::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/customer_score.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
# File 'lib/customer_score.rb', line 7

def initialize()
  @default_url = "http://not_real.com/customer_scoring"
end

Instance Method Details

#get_score(income, zipcode, age) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/customer_score.rb', line 11

def get_score(income, zipcode, age)

  return "Please provide a valid income (no commas or decimals)" unless income.to_s !~ /\d+[,.]\d+/ 
  return "Please provide a valid 6 or 10 digit zipcode" if zipcode.to_s !~ /^\d{5}(-\d{4})?$/
  return "Please provide a valid age" unless age.to_s !~ /\d+[,.]\d+/ 
 
  url = "#{@default_url}?income=#{income}&zipcode=#{zipcode}&age=#{age}"

  case status = get_status(url)
  when 200
    return Unirest.get(url).body
  when 404 
    raise ResourceNotFound, "The specified resource does not exist"
  when 500
    raise InternalError, "The server encountered an internal error, please retry the request"
  else
    raise "Error, your request was not completed"
  end
end

#get_status(url) ⇒ Object



31
32
33
# File 'lib/customer_score.rb', line 31

def get_status(url)
  Unirest.get(url).code
end