Class: MusefindUser::User

Inherits:
Object
  • Object
show all
Defined in:
lib/musefind_user/user.rb

Constant Summary collapse

SECRET =
ENV['SECRET_KEY_BASE'] || 'secret'
ADMIN_ID =
ENV['ADMIN_AUTH_ID'] || 1
USERS_API_URL =
ENV['USERS_API_URL']
REDIS_HOST =
ENV['REDIS_HOST']
MEMCACHED_HOST =
ENV['MEMCACHED_HOST']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ User

Returns a new instance of User.



39
40
41
# File 'lib/musefind_user/user.rb', line 39

def initialize(profile)
  @profile = HashWithIndifferentAccess.new(profile)
end

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



37
38
39
# File 'lib/musefind_user/user.rb', line 37

def profile
  @profile
end

Class Method Details

.cache_profile(id, permissions_profile) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/musefind_user/user.rb', line 64

def self.cache_profile(id, permissions_profile)
  begin
    redis.set(path(id), JSON.generate(permissions_profile))     if REDIS_HOST
    memcache.set(path(id), JSON.generate(permissions_profile))  if MEMCACHED_HOST
  rescue Exception => e
    puts "USER ERROR: failed to cache user name=#{e.class.name} error=#{e.message}"
    nil
  end
end

.find(id) ⇒ Object



43
44
45
# File 'lib/musefind_user/user.rb', line 43

def self.find(id)
  User.new(retrieve(id))
end

.from_token(token) ⇒ Object



59
60
61
62
# File 'lib/musefind_user/user.rb', line 59

def self.from_token(token)
  payload, _ = JWT.decode(token, SECRET, true)
  find(payload['sub'])
end

.retrieve(id) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/musefind_user/user.rb', line 47

def self.retrieve(id)
  begin
    user = retrieve_redis(path(id))
    user = retrieve_memcache(path(id)) unless user
    user = retrieve_http(path(id)) unless user
    HashWithIndifferentAccess.new(user) if user
  rescue Exception => e
    puts "USER ERROR: failed to retrieve user name=#{e.class.name} error=#{e.message}"
    nil
  end
end

.unset_cached_profile(id) ⇒ Object



74
75
76
77
# File 'lib/musefind_user/user.rb', line 74

def self.unset_cached_profile(id)
  redis.del(path(id))     if REDIS_HOST
  memcache.del(path(id))  if MEMCACHED_HOST
end

Instance Method Details

#activatedObject



99
100
101
# File 'lib/musefind_user/user.rb', line 99

def activated
  @profile[:profile][:activated]
end

#companiesObject



111
112
113
# File 'lib/musefind_user/user.rb', line 111

def companies
  @profile[:companies]
end

#emailObject



87
88
89
# File 'lib/musefind_user/user.rb', line 87

def email
  @profile[:profile][:email]
end

#idObject



83
84
85
# File 'lib/musefind_user/user.rb', line 83

def id
  @profile[:user_id]
end

#nameObject



91
92
93
# File 'lib/musefind_user/user.rb', line 91

def name
  @profile[:profile][:name]
end

#roleObject



103
104
105
# File 'lib/musefind_user/user.rb', line 103

def role
  @profile[:role]
end

#save_sessionObject



119
120
121
# File 'lib/musefind_user/user.rb', line 119

def save_session
  memcache.set(session_path(id), JSON.generate(@session)) if MEMCACHED_HOST && @session
end

#sessionObject



115
116
117
# File 'lib/musefind_user/user.rb', line 115

def session
  @session ||= self.class.retrieve_memcache(session_path(id))
end

#subscriptionsObject



107
108
109
# File 'lib/musefind_user/user.rb', line 107

def subscriptions
  @profile[:subscriptions]
end

#super_admin?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/musefind_user/user.rb', line 79

def super_admin?
  role == 'SUPERADMIN'
end

#usernameObject



95
96
97
# File 'lib/musefind_user/user.rb', line 95

def username
  @profile[:profile][:username]
end