Module: InsalesApi::Helpers::InitApi

Extended by:
ActiveSupport::Concern
Defined in:
lib/insales_api/helpers/init_api.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#init_apiObject

Configures api with credentials taken from self.insales_domain and self.insales_password.

If block is given, it is evaluated and its result is returned. After this old configuration is restored.

account1 = Account.find(1)
account2 = Account.find(2)

account1.init_api
# account1 credentials are used
product1 = InsalesApi::Product.find(1)
# will search within second account
product2 = account2.init_api { InsalesApi::Product.find(2) }
# configuration is restored
variant1 = InsalesApi::Variants.find(1)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/insales_api/helpers/init_api.rb', line 83

def init_api
  if block_given?
    old_config = insales_app_class.dump_config
    begin
      init_api
      yield
    ensure
      insales_app_class.restore_config(old_config)
    end
  else
    insales_app_class.configure_api(insales_domain, insales_password)
    self
  end
end