Class: NS1::Transport::NetHttp

Inherits:
Object
  • Object
show all
Defined in:
lib/record_store/provider/ns1/patch_api_header.rb

Constant Summary collapse

X_RATELIMIT_PERIOD =
'x-ratelimit-period'.freeze
X_RATELIMIT_REMAINING =
'x-ratelimit-remaining'.freeze

Instance Method Summary collapse

Instance Method Details

#process_response(response) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/record_store/provider/ns1/patch_api_header.rb', line 17

def process_response(response)
  response_hash = response.to_hash

  if response_hash.key?(X_RATELIMIT_PERIOD) && response_hash.key?(X_RATELIMIT_REMAINING)
    sleep_time = response_hash[X_RATELIMIT_PERIOD].first.to_i /
      [1, response_hash[X_RATELIMIT_REMAINING].first.to_i].max.to_f

    rate_limit = RateLimitWaiter.new('NS1')
    rate_limit.wait(sleep_time)
  end

  begin
    body = JSON.parse(response.body)
    case response
    when Net::HTTPOK
      NS1::Response::Success.new(body, response.code.to_i)
    else
      NS1::Response::Error.new(body, response.code.to_i)
    end
  rescue JSON::ParserError
    NS1::Response::UnparsableBodyError.new(response.code.to_i)
  end
end