Class: MusefindUser::User
- Inherits:
-
Object
- Object
- MusefindUser::User
- 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
-
#profile ⇒ Object
Returns the value of attribute profile.
Class Method Summary collapse
- .cache_profile(id, permissions_profile) ⇒ Object
- .find(id) ⇒ Object
- .from_token(token) ⇒ Object
- .retrieve(id) ⇒ Object
- .unset_cached_profile(id) ⇒ Object
Instance Method Summary collapse
- #activated ⇒ Object
- #companies ⇒ Object
- #email ⇒ Object
- #id ⇒ Object
-
#initialize(profile) ⇒ User
constructor
A new instance of User.
- #name ⇒ Object
- #role ⇒ Object
- #save_session ⇒ Object
- #session ⇒ Object
- #subscriptions ⇒ Object
- #super_admin? ⇒ Boolean
- #username ⇒ Object
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
#profile ⇒ Object
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, ) begin redis.set(path(id), JSON.generate()) if REDIS_HOST memcache.set(path(id), JSON.generate()) if MEMCACHED_HOST rescue Exception => e puts "USER ERROR: failed to cache user name=#{e.class.name} error=#{e.}" 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.}" 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
#activated ⇒ Object
99 100 101 |
# File 'lib/musefind_user/user.rb', line 99 def activated @profile[:profile][:activated] end |
#companies ⇒ Object
111 112 113 |
# File 'lib/musefind_user/user.rb', line 111 def companies @profile[:companies] end |
#email ⇒ Object
87 88 89 |
# File 'lib/musefind_user/user.rb', line 87 def email @profile[:profile][:email] end |
#id ⇒ Object
83 84 85 |
# File 'lib/musefind_user/user.rb', line 83 def id @profile[:user_id] end |
#name ⇒ Object
91 92 93 |
# File 'lib/musefind_user/user.rb', line 91 def name @profile[:profile][:name] end |
#role ⇒ Object
103 104 105 |
# File 'lib/musefind_user/user.rb', line 103 def role @profile[:role] end |
#save_session ⇒ Object
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 |
#session ⇒ Object
115 116 117 |
# File 'lib/musefind_user/user.rb', line 115 def session @session ||= self.class.retrieve_memcache(session_path(id)) end |
#subscriptions ⇒ Object
107 108 109 |
# File 'lib/musefind_user/user.rb', line 107 def subscriptions @profile[:subscriptions] end |
#super_admin? ⇒ Boolean
79 80 81 |
# File 'lib/musefind_user/user.rb', line 79 def super_admin? role == 'SUPERADMIN' end |
#username ⇒ Object
95 96 97 |
# File 'lib/musefind_user/user.rb', line 95 def username @profile[:profile][:username] end |