ohmycto / insales_api

Gem for accessing the InSales REST web services

Home Page:https://wiki.insales.ru/wiki/%D0%9A%D0%B0%D0%BA_%D0%B8%D0%BD%D1%82%D0%B5%D0%B3%D1%80%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%D1%81%D1%8F_%D1%81_InSales

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

InsalesApi gem

Build Status

Insales api client based on ActiveResource.

Rails application example is here.

Install

Add to Gemfile:

gem 'insales_api'

Initialize

class MyApp < InsalesApi::App
  self.api_key = 'api_key'
end

MyApp.configure_api('domain', 'password')

Use

order = InsalesApi::Order.find 123

# singleton resources
account = InsalesApi::Account.find

Handling Insales API request limit

There is a 500 requests per 5 minutes limit for Insales API. To handle this limitation gracefully, use InsalesApi.wait_retry method:

# prepare a handler for request limit case
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

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

If you don't need to cap the attempts number or you don't want to do any special processing, simply drop the arguments:

InsalesApi.wait_retry do
  products = InsalesApi::Products.all # will try to get products until the limit resets
end