lib/insales_api.rb in insales_api-0.1.3 vs lib/insales_api.rb in insales_api-0.2.0

- old
+ new

@@ -24,24 +24,27 @@ autoload :Client autoload :ClientGroup autoload :Collect autoload :Collection autoload :Currency + autoload :CustomStatus autoload :DeliveryVariant autoload :DiscountCode autoload :Domain autoload :Field autoload :File autoload :Image autoload :JsTag + autoload :Marketplace autoload :Notification autoload :OptionName autoload :OptionValue autoload :Order autoload :OrderLine autoload :Page autoload :PaymentGateway + autoload :PickUpSource autoload :PriceKind autoload :Product autoload :ProductField autoload :ProductFieldValue autoload :Property @@ -53,11 +56,11 @@ autoload :Variant autoload :Webhook end class << self - # Calls the supplied block. If the block raises <tt>ActiveResource::ServerError</tt> with 503 + # Calls the supplied block. If the block raises <tt>ActiveResource::ServerError</tt> with 429 # code which means Insales API request limit is reached, it will wait for the amount of seconds # specified in 'Retry-After' response header. The called block will receive a parameter with # current attempt number. # # ==== Params: @@ -76,26 +79,26 @@ # # InsalesApi.wait_retry(10, notify_user) do |x| # puts "Attempt ##{x}." # products = InsalesApi::Products.all # end - def wait_retry(max_attempts = nil, callback = nil, &block) + def wait_retry(max_attempts = nil, callback = nil, &block) # rubocop:disable Lint/UnusedMethodArgument attempts = 0 begin attempts += 1 yield attempts rescue ActiveResource::ServerError => ex - raise ex if '503' != ex.response.code.to_s + raise ex unless %w[429 503].include?(ex.response.code.to_s) raise ex if max_attempts && attempts >= max_attempts + retry_after = (ex.response['Retry-After'] || 150).to_i callback.call(retry_after, attempts, max_attempts, ex) if callback sleep(retry_after) retry end end end - end require 'insales_api/helpers/init_api' require 'insales_api/helpers/has_insales_object'