Class: ApiUser
- Inherits:
-
Object
- Object
- ApiUser
- Defined in:
- lib/generators/petergate_api/templates/lib/api_user.rb
Constant Summary collapse
- JWT_SECRET =
"$ecr3t_tok3n!"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(token) ⇒ ApiUser
constructor
A new instance of ApiUser.
- #method_missing(sym, *args, &block) ⇒ Object
Constructor Details
#initialize(token) ⇒ ApiUser
Returns a new instance of ApiUser.
6 7 8 9 10 11 12 |
# File 'lib/generators/petergate_api/templates/lib/api_user.rb', line 6 def initialize(token) @jwt = JWT.decode(token, JWT_SECRET, true, {algorithm: 'HS256'}).first @jwt.each do |k, v| v = v.map(&:to_sym) if v.is_a?(Array) define_singleton_method(k){v} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
14 15 16 17 |
# File 'lib/generators/petergate_api/templates/lib/api_user.rb', line 14 def method_missing(sym, *args, &block) @user ||= (User.find(id) || User.first) return @user.send(sym, *args, &block) end |
Class Method Details
.create_token(user) ⇒ Object
19 20 21 22 |
# File 'lib/generators/petergate_api/templates/lib/api_user.rb', line 19 def self.create_token(user) payload = {id: user.id, name: user.name, roles: user.roles, email: user.email} JWT.encode(payload, JWT_SECRET, 'HS256') end |