Class: ElocalApiSupport::Authorization::DefaultAuthorizer

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

Constant Summary collapse

FAIL_MESSAGE =
<<-EOL.strip
  No token could be found for ElocalApiSupport to use.  Please resolve this by either
- Define a method required_token which provides a token to check
- Set the configuration token in your config/application.rb by setting a value for config.elocal_api_support_token
- Define a method authorizer return a custom Authorization object which responds to authorize(token)
EOL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller) ⇒ DefaultAuthorizer

Returns a new instance of DefaultAuthorizer.



12
13
14
# File 'lib/elocal_api_support/default_authorizer.rb', line 12

def initialize(caller)
  @caller = caller
end

Instance Attribute Details

#callerObject (readonly)

Returns the value of attribute caller.



3
4
5
# File 'lib/elocal_api_support/default_authorizer.rb', line 3

def caller
  @caller
end

Instance Method Details

#authorize(token) ⇒ Object



16
17
18
# File 'lib/elocal_api_support/default_authorizer.rb', line 16

def authorize(token)
  find_required_token == token
end

#find_required_tokenObject



20
21
22
23
24
25
26
27
28
# File 'lib/elocal_api_support/default_authorizer.rb', line 20

def find_required_token
  if caller.respond_to?(:required_token, true)
    caller.send(:required_token)
  elsif Rails.application.config.elocal_api_support_token.present?
    Rails.application.config.elocal_api_support_token
  else
    fail FAIL_MESSAGE
  end
end