Class: MyJohnDeereApi::NetHttpRetry::Decorator
- Inherits:
-
Object
- Object
- MyJohnDeereApi::NetHttpRetry::Decorator
- 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, response_codes: ['429', '503'] }
Instance Attribute Summary collapse
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#request_methods ⇒ Object
readonly
Returns the value of attribute request_methods.
-
#response_codes ⇒ Object
readonly
Returns the value of attribute response_codes.
-
#retry_delay_exponent ⇒ Object
readonly
Returns the value of attribute retry_delay_exponent.
Instance Method Summary collapse
-
#initialize(object, options = {}) ⇒ Decorator
constructor
A new instance of Decorator.
- #request(method_name, *args) ⇒ Object
Constructor Details
#initialize(object, options = {}) ⇒ Decorator
Returns a new instance of Decorator.
13 14 15 16 17 18 19 20 21 |
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 13 def initialize(object, ={}) @object = object [:request_methods, :retry_delay_exponent, :max_retries].each do |option| instance_variable_set(:"@#{option}", [option] || DEFAULTS[option]) end @response_codes = ([:response_codes] || DEFAULTS[:response_codes]).map(&:to_s) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
44 45 46 47 48 49 50 |
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 44 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_retries ⇒ Object (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 |
#object ⇒ Object (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_methods ⇒ Object (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 |
#response_codes ⇒ Object (readonly)
Returns the value of attribute response_codes.
4 5 6 |
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 4 def response_codes @response_codes end |
#retry_delay_exponent ⇒ Object (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 |
Instance Method Details
#request(method_name, *args) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 23 def request(method_name, *args) retries = 0 result = object.send(method_name, *args) while response_codes.include?(result.code) if retries >= max_retries raise MaxRetriesExceededError.new(method_name, "#{result.code} #{result.}") end delay = [result['retry-after'].to_i, retry_delay_exponent ** retries].max sleep(delay) result = object.send(method_name, *args) retries += 1 end result end |