Class: QAuthRubyClient::User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- QAuthRubyClient::User
- Defined in:
- app/models/q_auth_ruby_client/user.rb
Direct Known Subclasses
Class Method Summary collapse
- .create_session(auth_token) ⇒ Object
- .fetch_all_users(auth_token) ⇒ Object
- .find_in_cache(data) ⇒ Object
Instance Method Summary collapse
-
#active? ⇒ Boolean
-
Return true if the user is activated, else false.
-
-
#display_address ⇒ Object
-
Return address which includes city, state & country == Examples >>> user.display_address => “Mysore, Karnataka, India”.
-
-
#inactive? ⇒ Boolean
-
Return true if the user is not activated, else false.
-
-
#is_admin? ⇒ Boolean
-
Return true if the user is either a Q-Auth Super Admin or Q-Auth Admin == Examples >>> user.is_admin? => true.
-
-
#is_super_admin? ⇒ Boolean
-
Return true if the user is either a Q-Auth Admin == Examples >>> user.is_super_admin? => true.
-
-
#suspended? ⇒ Boolean
-
Return true if the user is suspended, else false.
-
- #token_expired? ⇒ Boolean
- #update_cache(data) ⇒ Object
Class Method Details
.create_session(auth_token) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/q_auth_ruby_client/user.rb', line 69 def self.create_session(auth_token) qauth_url = QAuthRubyClient.configuration.q_auth_url + "/api/v1/my_profile" request = Typhoeus::Request.new( qauth_url, method: :get, headers: {"Authorization" => "Token token=#{auth_token}"}, verbose: false ) response = JSON.parse(request.run.body) if response["success"] user = find_in_cache(response['data']) user.update_cache(response['data']) return user else return response end end |
.fetch_all_users(auth_token) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/models/q_auth_ruby_client/user.rb', line 7 def self.fetch_all_users(auth_token) qauth_url = QAuthRubyClient.configuration.q_auth_url + "/api/v1/members" request = Typhoeus::Request.new( qauth_url, method: :get, headers: {"Authorization" => "Token token=#{auth_token}"}, verbose: false ) response = JSON.parse(request.run.body) if response["success"] response['data'].each do |data| user = find_in_cache(data) user.update_cache(data) end else raise end end |
.find_in_cache(data) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/q_auth_ruby_client/user.rb', line 27 def self.find_in_cache(data) # Checking if we already have this user(s) in our database users = QAuthRubyClient::User.where("auth_token = ? or username = ? or email = ? or q_auth_uid = ?", data['auth_token'], data['username'], data['email'], data['id']).all # Get the first user user = users.first if user # Corner Case : If there are (by chance) multiple rows, we need to remove them. users.delete(user) users.destroy_all if users.count > 0 return user else # Create a new user QAuthRubyClient::User.new(auth_token: data['auth_token'], username: data['username'], email: data['email'], q_auth_uid: data['id']) end end |
Instance Method Details
#active? ⇒ Boolean
-
Return true if the user is activated, else false.
Examples
>>> user.active?
=> true
133 134 135 |
# File 'app/models/q_auth_ruby_client/user.rb', line 133 def active? (status == "active") end |
#display_address ⇒ Object
-
Return address which includes city, state & country
Examples
>>> user.display_address
=> "Mysore, Karnataka, India"
96 97 98 99 100 101 102 |
# File 'app/models/q_auth_ruby_client/user.rb', line 96 def display_address address_list = [] address_list << city unless city.blank? address_list << state unless state.blank? address_list << country unless country.blank? address_list.join(", ") end |
#inactive? ⇒ Boolean
-
Return true if the user is not activated, else false.
-
inactive status will be there only for users who are not activated by user
Examples
>>> user.inactive?
=> true
125 126 127 |
# File 'app/models/q_auth_ruby_client/user.rb', line 125 def inactive? (status == "inactive") end |
#is_admin? ⇒ Boolean
-
Return true if the user is either a Q-Auth Super Admin or Q-Auth Admin
Examples
>>> user.is_admin?
=> true
108 109 110 |
# File 'app/models/q_auth_ruby_client/user.rb', line 108 def is_admin? user_type == 'admin' || user_type == 'super_admin' end |
#is_super_admin? ⇒ Boolean
-
Return true if the user is either a Q-Auth Admin
Examples
>>> user.is_super_admin?
=> true
116 117 118 |
# File 'app/models/q_auth_ruby_client/user.rb', line 116 def is_super_admin? user_type == 'super_admin' end |
#suspended? ⇒ Boolean
-
Return true if the user is suspended, else false.
Examples
>>> user.suspended?
=> true
141 142 143 |
# File 'app/models/q_auth_ruby_client/user.rb', line 141 def suspended? (status == "suspended") end |
#token_expired? ⇒ Boolean
88 89 90 |
# File 'app/models/q_auth_ruby_client/user.rb', line 88 def token_expired? return self.token_created_at.nil? || (Time.now > self.token_created_at + QAuthRubyClient.configuration.session_time_out) end |
#update_cache(data) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/q_auth_ruby_client/user.rb', line 45 def update_cache(data) self.name = data["name"] self.biography = data["biography"] self.phone = data["phone"] self.skype = data["skype"] self.linkedin = data["linkedin"] self.city = data["city"] self.state = data["state"] self.country = data["country"] self.token_created_at = data["token_created_at"] || Time.now - 1.day self.user_type = data["user_type"] self.thumb_url = data.try(:[],"profile_image").try(:[],"thumb") self.medium_url = data.try(:[],"profile_image").try(:[],"medium") self.large_url = data.try(:[],"profile_image").try(:[],"large") self.original_url = data.try(:[],"profile_image").try(:[],"original") self.designation = data.try(:[],"designation").try(:[],"title") self.department = data.try(:[],"department").try(:[],"name") self.save if self.valid? end |