Class: Communication::SessionManager
- Inherits:
-
Object
- Object
- Communication::SessionManager
- Defined in:
- lib/communication/session_manager.rb
Overview
Manages user session
Class Method Summary collapse
-
.logged_in? ⇒ Boolean
Returns returns true if user is logged in.
-
.logout ⇒ Object
Log out the currently logged in user.
Instance Method Summary collapse
- #handle_login_success(body) ⇒ Object
-
#initialize(account_credentials) ⇒ SessionManager
constructor
A new instance of SessionManager.
-
#login(force: false) ⇒ Object
Logs in a user or prompts with login credentials if it's not logged in.
-
#myself ⇒ Hash
Returns information about the logged in user.
- #parse_login_response(body, _) ⇒ Object
- #store_cookie(body) ⇒ Object
Constructor Details
#initialize(account_credentials) ⇒ SessionManager
Returns a new instance of SessionManager.
24 25 26 |
# File 'lib/communication/session_manager.rb', line 24 def initialize(account_credentials) self.credentials = account_credentials end |
Class Method Details
.logged_in? ⇒ Boolean
Returns returns true if user is logged in.
84 85 86 87 88 |
# File 'lib/communication/session_manager.rb', line 84 def self.logged_in? Utilities. && !CredentialsConfiguration.new.login_credentials.nil? rescue StandardError false end |
.logout ⇒ Object
Log out the currently logged in user.
72 73 74 75 76 77 78 79 |
# File 'lib/communication/session_manager.rb', line 72 def self.logout raise LogworkException::UserNotLoggedIn.new, "You are not logged in." unless logged_in? Communicator.instance.delete("/rest/auth/#{Constants::JIRA_AUTH_VERSION}/session") do Utilities. CredentialsConfiguration.new.update_login_credentials(nil, nil) end end |
Instance Method Details
#handle_login_success(body) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/communication/session_manager.rb', line 53 def handle_login_success(body) unless credentials.is_stored # store credentials Configuration::CredentialsConfiguration.new.update_login_credentials(credentials.username, credentials.password) end (body) yield if block_given? end |
#login(force: false) ⇒ Object
Logs in a user or prompts with login credentials if it's not logged in.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/communication/session_manager.rb', line 31 def login(force: false) if SessionManager.logged_in? && !force raise LogworkException::UserAlreadyLoggedIn.new, "You are already logged in." end params = { "username" => credentials.username, "password" => credentials.password } Communicator.instance.post("/rest/auth/#{Constants::JIRA_AUTH_VERSION}/session", params) do |body, res| parse_login_response(body, res) { yield if block_given? } end end |
#myself ⇒ Hash
Returns information about the logged in user.
93 94 95 96 97 98 99 |
# File 'lib/communication/session_manager.rb', line 93 def myself Communicator.instance.get("#{Communicator.base_api_url}/myself") do |body, _| raise LogworkException::InvalidURL unless body.is_a?(Hash) { full_name: body[:displayName] } end end |
#parse_login_response(body, _) ⇒ Object
47 48 49 50 51 |
# File 'lib/communication/session_manager.rb', line 47 def parse_login_response(body, _) handle_login_success(body) do yield if block_given? end end |
#store_cookie(body) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/communication/session_manager.rb', line 64 def (body) return unless body.is_a?(Hash) = "#{body[:session][:name]}=#{body[:session][:value]}" Communicator.instance.() end |