Module: InsalesApi

Extended by:
ActiveSupport::Autoload
Defined in:
lib/insales_api.rb,
lib/insales_api/app.rb,
lib/insales_api/base.rb,
lib/insales_api/file.rb,
lib/insales_api/page.rb,
lib/insales_api/user.rb,
lib/insales_api/asset.rb,
lib/insales_api/field.rb,
lib/insales_api/image.rb,
lib/insales_api/order.rb,
lib/insales_api/theme.rb,
lib/insales_api/client.rb,
lib/insales_api/domain.rb,
lib/insales_api/js_tag.rb,
lib/insales_api/account.rb,
lib/insales_api/collect.rb,
lib/insales_api/product.rb,
lib/insales_api/variant.rb,
lib/insales_api/version.rb,
lib/insales_api/webhook.rb,
lib/insales_api/category.rb,
lib/insales_api/currency.rb,
lib/insales_api/password.rb,
lib/insales_api/property.rb,
lib/insales_api/collection.rb,
lib/insales_api/order_line.rb,
lib/insales_api/price_kind.rb,
lib/insales_api/marketplace.rb,
lib/insales_api/option_name.rb,
lib/insales_api/client_group.rb,
lib/insales_api/notification.rb,
lib/insales_api/option_value.rb,
lib/insales_api/custom_status.rb,
lib/insales_api/discount_code.rb,
lib/insales_api/product_field.rb,
lib/insales_api/characteristic.rb,
lib/insales_api/pick_up_source.rb,
lib/insales_api/stock_currency.rb,
lib/insales_api/payment_gateway.rb,
lib/insales_api/delivery_variant.rb,
lib/insales_api/application_charge.rb,
lib/insales_api/application_widget.rb,
lib/insales_api/resource/countable.rb,
lib/insales_api/resource/paginated.rb,
lib/insales_api/product_field_value.rb,
lib/insales_api/active_resource_proxy.rb,
lib/insales_api/resource/with_updated_since.rb,
lib/insales_api/recurring_application_charge.rb

Defined Under Namespace

Modules: Helpers, Password, Resource, VERSION Classes: Account, ActiveResourceProxy, App, ApplicationCharge, ApplicationWidget, Asset, Base, Category, Characteristic, Client, ClientGroup, Collect, Collection, Currency, CustomStatus, DeliveryVariant, DiscountCode, Domain, Field, File, Image, JsTag, Marketplace, Notification, OptionName, OptionValue, Order, OrderLine, Page, PaymentGateway, PickUpSource, PriceKind, Product, ProductField, ProductFieldValue, Property, RecurringApplicationCharge, StockCurrency, Theme, User, Variant, Webhook

Constant Summary collapse

Deprecator =
ActiveSupport::Deprecation.new('1.0', name)
PER_PAGE_DEFAULT =
100

Class Method Summary collapse

Class Method Details

.wait_retry(max_attempts = nil, callback = nil, &block) ⇒ Object

Calls the supplied block. If the block raises ActiveResource::ServerError 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:

+max_attempts+:: maximum number of attempts. Defaults to +nil+ (unlimited).
+callback+:: +Proc+ or lambda to execute before waiting. Will receive four arguments: number
             of seconds we are going to wait, number of failed attempts, maximum number of
             attempts and the caught <tt>ActiveResource::ServerError</tt>. Defaults to +nil+
             (no callback).

Example

notify_user = Proc.new do |wait_for, attempt, max_attempts, ex|
  puts "API limit reached. Waiting for #{wait_for} seconds. Attempt #{attempt}/#{max_attempts}"
end

InsalesApi.wait_retry(10, notify_user) do |x|
  puts "Attempt ##{x}."
  products = InsalesApi::Products.all
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/insales_api.rb', line 84

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 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