Class: SmartyStreets::StatusCodeSender

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

Instance Method Summary collapse

Constructor Details

#initialize(inner) ⇒ StatusCodeSender

Returns a new instance of StatusCodeSender.



6
7
8
# File 'lib/smartystreets_ruby_sdk/status_code_sender.rb', line 6

def initialize(inner)
  @inner = inner
end

Instance Method Details

#assign_exception(response) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/smartystreets_ruby_sdk/status_code_sender.rb', line 36

def assign_exception(response)
  response.error = case response.status_code
                     when '401'
                       BadCredentialsError.new(BAD_CREDENTIALS)
                     when '402'
                       PaymentRequiredError.new(PAYMENT_REQUIRED)
                     when '413'
                       RequestEntityTooLargeError.new(REQUEST_ENTITY_TOO_LARGE)
                     when '400'
                       BadRequestError.new(BAD_REQUEST)
                     when '422'
                       UnprocessableEntityError.new(UNPROCESSABLE_ENTITY)
                     when '429'
                       TooManyRequestsError.new(TOO_MANY_REQUESTS)
                     when '500'
                       InternalServerError.new(INTERNAL_SERVER_ERROR)
                     when '503'
                       ServiceUnavailableError.new(SERVICE_UNAVAILABLE)
                     else
                       nil
                   end
end

#parse_rate_limit_response(response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/smartystreets_ruby_sdk/status_code_sender.rb', line 21

def parse_rate_limit_response(response)
  error_message = ""
  if !response.payload.nil?
    response_json = JSON.parse(response.payload)
    response_json["errors"].each do |error|
      error_message += (" " + error["message"])
    end
    error_message.strip!
  end
  if error_message == ""
    error_message = TOO_MANY_REQUESTS
  end
  TooManyRequestsError.new(error_message)
end

#send(request) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/smartystreets_ruby_sdk/status_code_sender.rb', line 10

def send(request)
  response = @inner.send(request)

  if response.status_code == '429'
    response.error = parse_rate_limit_response(response)
  end
  assign_exception(response) if response.error == nil

  response
end