Class: SysAid

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

Overview

The main SysAid class

Defined Under Namespace

Classes: Activity, Project, Task, Ticket, User

Constant Summary collapse

@@logged_in =
false
@@server_settings =
{ account: nil, username: nil, password: nil, wsdl_uri: nil, debug: false }

Class Method Summary collapse

Class Method Details

.call(method, message) ⇒ Object

Proxy for @@client.call but allows us to catch the various exceptions which may happen due to network errors, SOAP errors, etc., which otherwise would have to be caught in many different places.



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

def self.call(method, message)
  begin
    return @@client.call(method, message)
  rescue SocketError => e
      raise SysAidException, "Unable to fetch user information from SysAid server: #{e.message}"
  rescue Errno::EHOSTUNREACH => e
    raise SysAidException, "Unable to save user due to server being unreachable: #{e.message}"
  rescue Savon::SOAPFault => e
    raise SysAidException, "Unable to save user due to SOAP communications error: #{e.message}"
  end
end

.clientObject

Accessor for internal SysaidApiService object. Used by SysAid::Ticket



14
15
16
# File 'lib/sysaid.rb', line 14

def self.client
  @@client
end

.ensure_logged_inObject

The SysAid API isn’t clear on what creates an API timeout. It’s possible to login and make SOAP calls many minutes later but the API seems to timeout at some point. With the exception of the added delay, there’s no harm in simply logging in over and over with each call, so we do this in ensure_logged_in until we can get a better answer on what we can do with the SysAid API to actually ensure we’re logged in.



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

def self.ensure_logged_in
  self. unless self.logged_in?
end

.logged_in?Boolean

Returns true if logged into SysAid server Note: By design, logged_in? will try to log in if it isn’t already

Example:

>> SysAid.logged_in?
=> true

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sysaid.rb', line 65

def self.logged_in?
  puts "SysAid.logged_in? called" if @@server_settings[:debug]

  # Until official word comes from the company, we're going to login every time
  # to avoid a problem with undetected timeouts.
  #if @@logged_in == false
    
  #end

  if @@logged_in
    return true
  else
    return false
  end
end

.login(account = nil, username = nil, password = nil, wsdl_uri = nil, debug = nil) ⇒ Object

self.login does not require credentials be passed every time. SysAid sometimes times out the session and we can simply call ‘login’ again with the saved credentials to get around the timeout.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sysaid.rb', line 84

def self.( = nil, username = nil, password = nil, wsdl_uri = nil, debug = nil)
  puts "SysAid.login" if @@server_settings[:debug]

  if  and username and password and wsdl_uri
    # Save server settings in case we need to log in again later
    @@server_settings = { :account => , :username => username, :password => password, :wsdl_uri => wsdl_uri }
    # 'debug' parameter cannot default to false else it would override default, so use nil and check
    @@server_settings[:debug] = debug if debug != nil
  end

  begin
    @@client = Savon.client(wsdl: @@server_settings[:wsdl_uri], log: @@server_settings[:debug])

    # login
    unless @@server_settings[:account].nil?
      # Call login
      response = self.call(:login, message: { accountId: @@server_settings[:account], userName: @@server_settings[:username], password: @@server_settings[:password] })

      # Retrieve response
      @@session_id = response.to_hash[:login_response][:return]

      @@logged_in = true
    end
  rescue Net::ReadTimeout => e
    # This isn't the API timeout, this is a normal socket timeout error.
    raise SysAidException, "Unable to log into SysAid server: #{e.message}"
  end
end

.server_settingsObject

Accessor for server settings. Used by SysAid::Ticket



41
42
43
# File 'lib/sysaid.rb', line 41

def self.server_settings
  @@server_settings
end

.server_settings=(server_settings) ⇒ Object



44
45
46
47
48
# File 'lib/sysaid.rb', line 44

def self.server_settings=(server_settings)
  @@server_settings = server_settings
  # If they don't pass in debug, set it to false to maintain default settings
  @@server_settings[:debug] = false if server_settings[:debug].nil?
end

.session_idObject

Accessor for session ID returned by SysAid server. Used by SysAid::Ticket



35
36
37
# File 'lib/sysaid.rb', line 35

def self.session_id
  @@session_id
end