Class: Cts::Mpx::User

Inherits:
Object
  • Object
show all
Includes:
Creatable
Defined in:
lib/cts/mpx/user.rb

Overview

Class to wrap the user and basic functions related to it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#durationNumeric

Returns total time to live for the token.

Returns:

  • (Numeric)

    total time to live for the token



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cts/mpx/user.rb', line 14

class User
  include Creatable

  attribute name: 'username', kind_of: String
  attribute name: 'password', kind_of: String
  attribute name: 'idle_timeout', kind_of: Integer
  attribute name: 'duration', kind_of: Integer
  attribute name: 'token', kind_of: [String, NilClass]

  # Attempt to sign the user in with the provided credentials
  # @param [Numeric] idle_timeout how long the token will stay alive without communicating with the services
  # @param [Numeric] duration total time to live for the token
  # @return [Self]
  def (idle_timeout: nil, duration: nil)
    raise 'token is already set, use sign_out first.' if token

    arguments = {}

    arguments['idleTimeout'] if idle_timeout
    arguments['duration'] if duration
    headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }

    self.token = 'sign_in_token'
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
    self.token = nil

    raise "sign_in exception, status: #{response.status}" unless response.status == 200

    self.token = response.data['signInResponse']['token']
    self
  end

  # Sign the token out
  # @return [Void]
  def sign_out
    arguments = { "token" => token }
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
    @token = nil if response.status == 200
    nil
  end

  # Override to masq the password
  # @return [String] updated output
  def inspect
    output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"

    %i[username token idle_timeout duration].each do |attribute|
      value = instance_variable_get "@#{attribute}".to_sym
      output += " @#{attribute}=#{value}" unless value.nil?
    end

    output += '>'
    output
  end

  # raise an error if the token is not set, otherwise return the token
  # @return [String] token
  def token!
    raise "#{username} is not signed in, (token is set to nil)." unless token

    token
  end
end

#idle_timeoutNumeric

Returns how long the token will stay alive without communicating with the services.

Returns:

  • (Numeric)

    how long the token will stay alive without communicating with the services



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cts/mpx/user.rb', line 14

class User
  include Creatable

  attribute name: 'username', kind_of: String
  attribute name: 'password', kind_of: String
  attribute name: 'idle_timeout', kind_of: Integer
  attribute name: 'duration', kind_of: Integer
  attribute name: 'token', kind_of: [String, NilClass]

  # Attempt to sign the user in with the provided credentials
  # @param [Numeric] idle_timeout how long the token will stay alive without communicating with the services
  # @param [Numeric] duration total time to live for the token
  # @return [Self]
  def (idle_timeout: nil, duration: nil)
    raise 'token is already set, use sign_out first.' if token

    arguments = {}

    arguments['idleTimeout'] if idle_timeout
    arguments['duration'] if duration
    headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }

    self.token = 'sign_in_token'
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
    self.token = nil

    raise "sign_in exception, status: #{response.status}" unless response.status == 200

    self.token = response.data['signInResponse']['token']
    self
  end

  # Sign the token out
  # @return [Void]
  def sign_out
    arguments = { "token" => token }
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
    @token = nil if response.status == 200
    nil
  end

  # Override to masq the password
  # @return [String] updated output
  def inspect
    output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"

    %i[username token idle_timeout duration].each do |attribute|
      value = instance_variable_get "@#{attribute}".to_sym
      output += " @#{attribute}=#{value}" unless value.nil?
    end

    output += '>'
    output
  end

  # raise an error if the token is not set, otherwise return the token
  # @return [String] token
  def token!
    raise "#{username} is not signed in, (token is set to nil)." unless token

    token
  end
end

#passwordString

Returns password of the user.

Returns:

  • (String)

    password of the user



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cts/mpx/user.rb', line 14

class User
  include Creatable

  attribute name: 'username', kind_of: String
  attribute name: 'password', kind_of: String
  attribute name: 'idle_timeout', kind_of: Integer
  attribute name: 'duration', kind_of: Integer
  attribute name: 'token', kind_of: [String, NilClass]

  # Attempt to sign the user in with the provided credentials
  # @param [Numeric] idle_timeout how long the token will stay alive without communicating with the services
  # @param [Numeric] duration total time to live for the token
  # @return [Self]
  def (idle_timeout: nil, duration: nil)
    raise 'token is already set, use sign_out first.' if token

    arguments = {}

    arguments['idleTimeout'] if idle_timeout
    arguments['duration'] if duration
    headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }

    self.token = 'sign_in_token'
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
    self.token = nil

    raise "sign_in exception, status: #{response.status}" unless response.status == 200

    self.token = response.data['signInResponse']['token']
    self
  end

  # Sign the token out
  # @return [Void]
  def sign_out
    arguments = { "token" => token }
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
    @token = nil if response.status == 200
    nil
  end

  # Override to masq the password
  # @return [String] updated output
  def inspect
    output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"

    %i[username token idle_timeout duration].each do |attribute|
      value = instance_variable_get "@#{attribute}".to_sym
      output += " @#{attribute}=#{value}" unless value.nil?
    end

    output += '>'
    output
  end

  # raise an error if the token is not set, otherwise return the token
  # @return [String] token
  def token!
    raise "#{username} is not signed in, (token is set to nil)." unless token

    token
  end
end

#tokenString

Returns the retrieved token.

Returns:

  • (String)

    the retrieved token



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cts/mpx/user.rb', line 14

class User
  include Creatable

  attribute name: 'username', kind_of: String
  attribute name: 'password', kind_of: String
  attribute name: 'idle_timeout', kind_of: Integer
  attribute name: 'duration', kind_of: Integer
  attribute name: 'token', kind_of: [String, NilClass]

  # Attempt to sign the user in with the provided credentials
  # @param [Numeric] idle_timeout how long the token will stay alive without communicating with the services
  # @param [Numeric] duration total time to live for the token
  # @return [Self]
  def (idle_timeout: nil, duration: nil)
    raise 'token is already set, use sign_out first.' if token

    arguments = {}

    arguments['idleTimeout'] if idle_timeout
    arguments['duration'] if duration
    headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }

    self.token = 'sign_in_token'
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
    self.token = nil

    raise "sign_in exception, status: #{response.status}" unless response.status == 200

    self.token = response.data['signInResponse']['token']
    self
  end

  # Sign the token out
  # @return [Void]
  def sign_out
    arguments = { "token" => token }
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
    @token = nil if response.status == 200
    nil
  end

  # Override to masq the password
  # @return [String] updated output
  def inspect
    output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"

    %i[username token idle_timeout duration].each do |attribute|
      value = instance_variable_get "@#{attribute}".to_sym
      output += " @#{attribute}=#{value}" unless value.nil?
    end

    output += '>'
    output
  end

  # raise an error if the token is not set, otherwise return the token
  # @return [String] token
  def token!
    raise "#{username} is not signed in, (token is set to nil)." unless token

    token
  end
end

#usernameString

Returns username of the user.

Returns:

  • (String)

    username of the user



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cts/mpx/user.rb', line 14

class User
  include Creatable

  attribute name: 'username', kind_of: String
  attribute name: 'password', kind_of: String
  attribute name: 'idle_timeout', kind_of: Integer
  attribute name: 'duration', kind_of: Integer
  attribute name: 'token', kind_of: [String, NilClass]

  # Attempt to sign the user in with the provided credentials
  # @param [Numeric] idle_timeout how long the token will stay alive without communicating with the services
  # @param [Numeric] duration total time to live for the token
  # @return [Self]
  def (idle_timeout: nil, duration: nil)
    raise 'token is already set, use sign_out first.' if token

    arguments = {}

    arguments['idleTimeout'] if idle_timeout
    arguments['duration'] if duration
    headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }

    self.token = 'sign_in_token'
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
    self.token = nil

    raise "sign_in exception, status: #{response.status}" unless response.status == 200

    self.token = response.data['signInResponse']['token']
    self
  end

  # Sign the token out
  # @return [Void]
  def sign_out
    arguments = { "token" => token }
    response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
    @token = nil if response.status == 200
    nil
  end

  # Override to masq the password
  # @return [String] updated output
  def inspect
    output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"

    %i[username token idle_timeout duration].each do |attribute|
      value = instance_variable_get "@#{attribute}".to_sym
      output += " @#{attribute}=#{value}" unless value.nil?
    end

    output += '>'
    output
  end

  # raise an error if the token is not set, otherwise return the token
  # @return [String] token
  def token!
    raise "#{username} is not signed in, (token is set to nil)." unless token

    token
  end
end

Instance Method Details

#inspectString

Override to masq the password

Returns:

  • (String)

    updated output



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cts/mpx/user.rb', line 57

def inspect
  output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"

  %i[username token idle_timeout duration].each do |attribute|
    value = instance_variable_get "@#{attribute}".to_sym
    output += " @#{attribute}=#{value}" unless value.nil?
  end

  output += '>'
  output
end

#sign_in(idle_timeout: nil, duration: nil) ⇒ Self

Attempt to sign the user in with the provided credentials

Parameters:

  • idle_timeout (Numeric) (defaults to: nil)

    how long the token will stay alive without communicating with the services

  • duration (Numeric) (defaults to: nil)

    total time to live for the token

Returns:

  • (Self)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cts/mpx/user.rb', line 27

def (idle_timeout: nil, duration: nil)
  raise 'token is already set, use sign_out first.' if token

  arguments = {}

  arguments['idleTimeout'] if idle_timeout
  arguments['duration'] if duration
  headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }

  self.token = 'sign_in_token'
  response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
  self.token = nil

  raise "sign_in exception, status: #{response.status}" unless response.status == 200

  self.token = response.data['signInResponse']['token']
  self
end

#sign_outVoid

Sign the token out

Returns:

  • (Void)


48
49
50
51
52
53
# File 'lib/cts/mpx/user.rb', line 48

def sign_out
  arguments = { "token" => token }
  response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
  @token = nil if response.status == 200
  nil
end

#token!String

raise an error if the token is not set, otherwise return the token

Returns:

  • (String)

    token



71
72
73
74
75
# File 'lib/cts/mpx/user.rb', line 71

def token!
  raise "#{username} is not signed in, (token is set to nil)." unless token

  token
end