Sha256: 1c33ac9834b6ea5bca49921d3981bb41b7114f4bd34415af0ed7a0fd5cc7db5a

Contents?: true

Size: 1.74 KB

Versions: 7

Compression:

Stored size: 1.74 KB

Contents

class ZapiAgent

  attr_reader :response, :zapi, :generate_xml, :responder

  def initialize username:, password:, zapi: nil, xml_generator: nil, responder: nil
    @zapi = zapi || Zapi.new()
    @generate_xml = xml_generator  || ZapiXML.new()
    @responder = responder || ZapiObject
    @response = _login username: username, password: password
  end

  def is_logged_in?
    response_msg == "Login Successful" && _method_name == 'zapiAgentLogin'
  end

  def response_msg
    response.try(:[],'methodErrorMessage')
  end

  def history
#raise response.inspect
    Zaui.agents_get_profile(agent_id: _user_id)
  end

  def session_hash
    {
      user_id: _user_id,
      account_id: _account_id,
      account_type: _account_type,
      cart_id: _cart_id,
      api_token: _api_token,
      username: _username,
      firstname: _firstname,
      lastname: _lastname,
      fullname: _fullname
    }
  end

  def session hash: session_hash
    responder.new(hash: hash)
  end

  def _login username:, password:
    login_xml = generate_xml.agent_login(username: username, password: password)
    zapi.request(xml: login_xml)
  end

  def _user_id
    response.try(:[],'zapiUserId').to_i
  end

  def _account_id
    response.try(:[],'zapiAccountId').to_i
  end

  def _account_type
    response.try(:[],'zapiAccountType').to_i
  end

  def _cart_id
    response.try(:[],'cartId')
  end

  def _api_token
    response.try(:[],'zapiApiToken')
  end

  def _username
    response.try(:[],'zapiUsername')
  end

  def _firstname
    response.try(:[],'firstName')
  end

  def _lastname
    response.try(:[],'lastName')
  end

  def _fullname
    [_firstname, _lastname].compact.join(" ") if _firstname || _lastname
  end

  def _method_name
    response.try(:[],'methodName')
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
zaui_zapi-0.3.1 lib/zapi_agent.rb
zaui_zapi-0.3.0 lib/zapi_agent.rb
zaui_zapi-0.2.8 lib/zapi_agent.rb
zaui_zapi-0.2.7 lib/zapi_agent.rb
zaui_zapi-0.2.6 lib/zapi_agent.rb
zaui_zapi-0.2.5 lib/zapi_agent.rb
zaui_zapi-0.2.4 lib/zapi_agent.rb