Class: OpenStax::Api::ApiUser

Inherits:
Object
  • Object
show all
Defined in:
app/models/openstax/api/api_user.rb

Constant Summary collapse

USER_CLASS =
OpenStax::Api.configuration.user_class_name.constantize

Instance Method Summary collapse

Constructor Details

#initialize(doorkeeper_token, non_doorkeeper_user_proc) ⇒ ApiUser

Returns a new instance of ApiUser.



20
21
22
23
# File 'app/models/openstax/api/api_user.rb', line 20

def initialize(doorkeeper_token, non_doorkeeper_user_proc)
  @doorkeeper_token = doorkeeper_token
  @non_doorkeeper_user_proc = non_doorkeeper_user_proc
end

Instance Method Details

#applicationObject

Returns a Doorkeeper::Application or nil



26
27
28
29
30
# File 'app/models/openstax/api/api_user.rb', line 26

def application
  # If we have a doorkeeper_token, derive the Application from it.
  # If not, we're in case #1 above and the Application should be nil.
  @application ||= @doorkeeper_token.try(:application)
end

#human_userObject

Returns an instance of User, AnonymousUser, or nil



33
34
35
36
37
38
39
40
# File 'app/models/openstax/api/api_user.rb', line 33

def human_user
  # If we have a doorkeeper_token, derive the User from it.
  # If not, we're in case #1 above and the User should be
  # retrieved from the non_doorkeeper_user_proc.
  @user ||= @doorkeeper_token ? \
              USER_CLASS.where(id: @doorkeeper_token.try(:resource_owner_id)).first : \
              @non_doorkeeper_user_proc.call
end