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, retry_codes: ['429', '503'], valid_codes: ['200', '201', '204'], }
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.
-
#retry_codes ⇒ Object
readonly
Returns the value of attribute retry_codes.
-
#retry_delay_exponent ⇒ Object
readonly
Returns the value of attribute retry_delay_exponent.
-
#valid_codes ⇒ Object
readonly
Returns the value of attribute valid_codes.
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.
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, ={}) @object = object # defaults that can be used as-is [:request_methods, :retry_delay_exponent, :max_retries].each do |option| instance_variable_set(:"@#{option}", [option] || DEFAULTS[option]) end # defaults that require casting as string arrays [:retry_codes, :valid_codes].each do |option| instance_variable_set(:"@#{option}", ([option] || DEFAULTS[option]).map(&:to_s)) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
53 54 55 56 57 58 59 |
# File 'lib/my_john_deere_api/net_http_retry/decorator.rb', line 53 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 |
#retry_codes ⇒ Object (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_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 |
#valid_codes ⇒ Object (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 49 |
# 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.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 unless valid_codes.include?(result.code) raise InvalidResponseError.new(result) end result end |