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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/q_auth_ruby_client/user.rb', line 77 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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/q_auth_ruby_client/user.rb', line 15 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/q_auth_ruby_client/user.rb', line 35 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 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
141 142 143 |
# File 'app/models/q_auth_ruby_client/user.rb', line 141 def active? (status == "active") end |
#display_address ⇒ Object
-
Return address which includes city, state & country
Examples
>>> user.display_address
=> "Mysore, Karnataka, India"
104 105 106 107 108 109 110 |
# File 'app/models/q_auth_ruby_client/user.rb', line 104 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
133 134 135 |
# File 'app/models/q_auth_ruby_client/user.rb', line 133 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
116 117 118 |
# File 'app/models/q_auth_ruby_client/user.rb', line 116 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
124 125 126 |
# File 'app/models/q_auth_ruby_client/user.rb', line 124 def is_super_admin? user_type == 'super_admin' end |
#suspended? ⇒ Boolean
-
Return true if the user is suspended, else false.
Examples
>>> user.suspended?
=> true
149 150 151 |
# File 'app/models/q_auth_ruby_client/user.rb', line 149 def suspended? (status == "suspended") end |
#token_expired? ⇒ Boolean
96 97 98 |
# File 'app/models/q_auth_ruby_client/user.rb', line 96 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/q_auth_ruby_client/user.rb', line 53 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 |