Class: MyJohnDeereApi::NetHttpRetry::Decorator

Inherits:
Object
  • Object
show all
Defined in:
lib/my_john_deere_api/net_http_retry/decorator.rb

Constant Summary collapse

DEFAULTS =
{
  request_methods:      [:get, :post, :put, :delete],
  retry_delay_exponent: 2,
  max_retries: 12,
  retry_codes: [429, 503],
  valid_codes: [200, 201, 204],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Decorator

Returns a new instance of Decorator.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 14

def initialize(object, options={})
  @object = object

  # options that can be used as-is
  [:request_methods, :retry_delay_exponent, :max_retries, :retry_codes, :valid_codes].each do |option|
    instance_variable_set(:"@#{option}", options[option] || DEFAULTS[option])
  end

  # options that require casting as integer arrays
  [:retry_codes, :valid_codes].each do |option|
    instance_variable_set(:"@#{option}", (options[option] || DEFAULTS[option]).map(&:to_i))
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



52
53
54
55
56
57
58
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 52

def method_missing(method_name, *args, &block)
  if request_methods.include?(method_name)
    request(method_name, *args)
  else
    object.send(method_name, *args, &block)
  end
end

Instance Attribute Details

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



4
5
6
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4

def max_retries
  @max_retries
end

#objectObject (readonly)

Returns the value of attribute object.



4
5
6
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4

def object
  @object
end

#request_methodsObject (readonly)

Returns the value of attribute request_methods.



4
5
6
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4

def request_methods
  @request_methods
end

#retry_codesObject (readonly)

Returns the value of attribute retry_codes.



4
5
6
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4

def retry_codes
  @retry_codes
end

#retry_delay_exponentObject (readonly)

Returns the value of attribute retry_delay_exponent.



4
5
6
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4

def retry_delay_exponent
  @retry_delay_exponent
end

#valid_codesObject (readonly)

Returns the value of attribute valid_codes.



4
5
6
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4

def valid_codes
  @valid_codes
end

Instance Method Details

#request(method_name, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 28

def request(method_name, *args)
  retries = 0
  result = object.send(method_name, *args)
  while retry_codes.include?(result.status)
    if retries >= max_retries
      raise MaxRetriesExceededError.new(method_name, "#{result.status} #{result.response.reason_phrase}")
    end

    delay = [result.headers['retry-after'].to_i, retry_delay_exponent ** retries].max
    sleep(delay)

    result = object.send(method_name, *args)
    retries += 1
  end

  unless valid_codes.include?(result.status)
    raise InvalidResponseError.new(result)
  end

  result
end