Class: ZapiAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/zapi_agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, zapi: nil, xml_generator: nil, responder: nil) ⇒ ZapiAgent

Returns a new instance of ZapiAgent.



5
6
7
8
9
10
# File 'lib/zapi_agent.rb', line 5

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 =  username: username, password: password
end

Instance Attribute Details

#generate_xmlObject (readonly)

Returns the value of attribute generate_xml.



3
4
5
# File 'lib/zapi_agent.rb', line 3

def generate_xml
  @generate_xml
end

#responderObject (readonly)

Returns the value of attribute responder.



3
4
5
# File 'lib/zapi_agent.rb', line 3

def responder
  @responder
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/zapi_agent.rb', line 3

def response
  @response
end

#zapiObject (readonly)

Returns the value of attribute zapi.



3
4
5
# File 'lib/zapi_agent.rb', line 3

def zapi
  @zapi
end

Instance Method Details

#_account_idObject



52
53
54
# File 'lib/zapi_agent.rb', line 52

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

#_account_typeObject



56
57
58
# File 'lib/zapi_agent.rb', line 56

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

#_api_tokenObject



64
65
66
# File 'lib/zapi_agent.rb', line 64

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

#_cart_idObject



60
61
62
# File 'lib/zapi_agent.rb', line 60

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

#_firstnameObject



72
73
74
# File 'lib/zapi_agent.rb', line 72

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

#_fullnameObject



80
81
82
# File 'lib/zapi_agent.rb', line 80

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

#_lastnameObject



76
77
78
# File 'lib/zapi_agent.rb', line 76

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

#_login(username:, password:) ⇒ Object



43
44
45
46
# File 'lib/zapi_agent.rb', line 43

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

#_method_nameObject



84
85
86
# File 'lib/zapi_agent.rb', line 84

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

#_user_idObject



48
49
50
# File 'lib/zapi_agent.rb', line 48

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

#_usernameObject



68
69
70
# File 'lib/zapi_agent.rb', line 68

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

#historyObject



20
21
22
23
# File 'lib/zapi_agent.rb', line 20

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

#is_logged_in?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/zapi_agent.rb', line 12

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

#response_msgObject



16
17
18
# File 'lib/zapi_agent.rb', line 16

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

#session(hash: session_hash) ⇒ Object



39
40
41
# File 'lib/zapi_agent.rb', line 39

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

#session_hashObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zapi_agent.rb', line 25

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