Class: ZapiEmployee

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ZapiEmployee.



5
6
7
8
9
10
# File 'lib/zapi_employee.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_employee.rb', line 3

def generate_xml
  @generate_xml
end

#responderObject (readonly)

Returns the value of attribute responder.



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

def responder
  @responder
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#zapiObject (readonly)

Returns the value of attribute zapi.



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

def zapi
  @zapi
end

Instance Method Details

#_account_idObject



47
48
49
# File 'lib/zapi_employee.rb', line 47

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

#_account_typeObject



51
52
53
# File 'lib/zapi_employee.rb', line 51

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

#_api_tokenObject



59
60
61
# File 'lib/zapi_employee.rb', line 59

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

#_cart_idObject



55
56
57
# File 'lib/zapi_employee.rb', line 55

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

#_firstnameObject



67
68
69
# File 'lib/zapi_employee.rb', line 67

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

#_fullnameObject



75
76
77
# File 'lib/zapi_employee.rb', line 75

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

#_lastnameObject



71
72
73
# File 'lib/zapi_employee.rb', line 71

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

#_login(username:, password:) ⇒ Object



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

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

#_method_nameObject



79
80
81
# File 'lib/zapi_employee.rb', line 79

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

#_user_idObject



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

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

#_usernameObject



63
64
65
# File 'lib/zapi_employee.rb', line 63

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

#is_logged_in?Boolean

Returns:

  • (Boolean)


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

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

#response_msgObject



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

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

#session(hash: session_hash) ⇒ Object



34
35
36
# File 'lib/zapi_employee.rb', line 34

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

#session_hashObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zapi_employee.rb', line 20

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