Class: Http::Client::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/tractive/http/client/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



6
7
8
9
# File 'lib/tractive/http/client/request.rb', line 6

def initialize(args)
  @args = args
  @max_retries = args[:max_retries] || 3
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



34
35
36
# File 'lib/tractive/http/client/request.rb', line 34

def response
  @response
end

Class Method Details

.execute(args, &block) ⇒ Object



53
54
55
# File 'lib/tractive/http/client/request.rb', line 53

def execute(args, &block)
  new(args).execute(&block)
end

.get(url, headers = {}, &block) ⇒ Object



37
38
39
# File 'lib/tractive/http/client/request.rb', line 37

def get(url, headers = {}, &block)
  execute(method: :get, url: url, headers: headers, &block)
end

.patch(url, payload, headers = {}, &block) ⇒ Object



49
50
51
# File 'lib/tractive/http/client/request.rb', line 49

def patch(url, payload, headers = {}, &block)
  execute(method: :patch, url: url, payload: payload, headers: headers, &block)
end

.post(url, payload, headers = {}, &block) ⇒ Object



41
42
43
# File 'lib/tractive/http/client/request.rb', line 41

def post(url, payload, headers = {}, &block)
  execute(method: :post, url: url, payload: payload, headers: headers, &block)
end

.put(url, payload, headers = {}, &block) ⇒ Object



45
46
47
# File 'lib/tractive/http/client/request.rb', line 45

def put(url, payload, headers = {}, &block)
  execute(method: :put, url: url, payload: payload, headers: headers, &block)
end

Instance Method Details

#execute(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tractive/http/client/request.rb', line 11

def execute(&block)
  retries = 0

  begin
    retries += 1
    RestClient::Request.execute(@args, &block)
  rescue RestClient::Forbidden => e
    retry_after = e.http_headers[:x_ratelimit_reset].to_i - Time.now.to_i + 5
    raise e if retry_after.negative? || retries > @max_retries

    while retry_after.positive?
      minutes = retry_after / 60
      seconds = retry_after % 60

      $logger.info "Rate Limit Exceeded, Will retry in #{minutes} min #{seconds} sec"
      sleep(1)

      retry_after = e.http_headers[:x_ratelimit_reset].to_i - Time.now.to_i + 5
    end
    retry if retries <= @max_retries
  end
end