Module: Communication::CommunicatorHelpers

Included in:
Communicator
Defined in:
lib/communication/modules/communicator_helpers.rb

Overview

Communicator helpers

Instance Method Summary collapse

Instance Method Details

#auth_call?(path) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/communication/modules/communicator_helpers.rb', line 46

def auth_call?(path)
  path.include?("/rest/auth/")
end

#check_not_found(status) ⇒ Object



29
30
31
# File 'lib/communication/modules/communicator_helpers.rb', line 29

def check_not_found(status)
  raise LogworkException::APIResourceNotFound.new, "API resource not found." if status == 404
end

#check_not_success(status) ⇒ Object



33
34
35
# File 'lib/communication/modules/communicator_helpers.rb', line 33

def check_not_success(status)
  raise LogworkException::NotSuccessStatusCode.new, "Not success response." if status / 200 != 1
end

#check_unauthorized(status, url) ⇒ Object



23
24
25
26
27
# File 'lib/communication/modules/communicator_helpers.rb', line 23

def check_unauthorized(status, url)
  return unless status == 401 && auth_call?(url)

  raise LogworkException::InvalidCredentials.new, "Login failed! Please check your credentials."
end

#handle_success(body) {|json_body| ... } ⇒ Object

Yields:

  • (json_body)


37
38
39
40
# File 'lib/communication/modules/communicator_helpers.rb', line 37

def handle_success(body)
  json_body = Utilities.valid_json?(body) ? parse_json(body) : parse_json({})
  yield(json_body) if block_given?
end

#reloginObject



50
51
52
53
54
55
56
57
# File 'lib/communication/modules/communicator_helpers.rb', line 50

def relogin
  # read credentials from configuration file configuration
  Utilities.remove_cookie
  conn.headers.delete("Cookie")
  Communication::SessionManager.new(CredentialsConfiguration.new.).(force: true)
  self.relogin_performed = true
  cached_request_callback.call(cached_success_callback)
end

#should_relogin(res) ⇒ Object



42
43
44
# File 'lib/communication/modules/communicator_helpers.rb', line 42

def should_relogin(res)
  !relogin_performed && !auth_call?(res.env.url.to_s) && res.status == 401
end