Module: Stackify::Authorizable::ClassMethods

Defined in:
lib/stackify/authorization/authorizable.rb

Constant Summary collapse

@@authorized =
false
@@auth_lock =
Mutex.new
@@auth_client =
nil

Instance Method Summary collapse

Instance Method Details

#authorize(attempts = 3) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/stackify/authorization/authorizable.rb', line 13

def authorize attempts=3
  Stackify::EnvDetails.instance.set_rails_info
  @@auth_lock.synchronize do
    return unless @@auth_client.nil?
    @@auth_client = Stackify::Authorizable::AuthorizationClient.new
    @@auth_client.auth attempts
  end
end

#authorized!Object



28
29
30
# File 'lib/stackify/authorization/authorizable.rb', line 28

def authorized!
  @@authorized = true
end

#authorized?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/stackify/authorization/authorizable.rb', line 22

def authorized?
  @@auth_lock.synchronize do
    @@authorized
  end
end

#if_not_authorized(failure_msg, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stackify/authorization/authorizable.rb', line 42

def if_not_authorized failure_msg, &block
  failure_msg += ', but Stackify module is not authorized'
  if Stackify.authorized?
    begin
      block.call
    rescue => e
      Stackify.log_internal_error e.message
    end
  else
    Stackify.log_internal_error failure_msg
  end
end

#response_string(r) ⇒ Object



55
56
57
58
# File 'lib/stackify/authorization/authorizable.rb', line 55

def response_string r
  return '' if r.nil?
  "Status: #{r.try(:status)}, Message: '#{r.try(:body)}'"
end

#successfull_authorisation(response) ⇒ Object



32
33
34
35
# File 'lib/stackify/authorization/authorizable.rb', line 32

def successfull_authorisation response
  Stackify::EnvDetails.instance.update_auth_info JSON.parse(response.body)
  Stackify.internal_log :info, 'Authorization is finished successfully.'
end

#unsuccessfull_authorisation(response, caller) ⇒ Object



37
38
39
40
# File 'lib/stackify/authorization/authorizable.rb', line 37

def unsuccessfull_authorisation response, caller
  Stackify.log_internal_error "Authorization finally failed: #{response_string(response)}"
  Stackify.shutdown_all caller unless @@authorized
end