Class: Device
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Device
- Defined in:
- app/models/device.rb
Constant Summary collapse
- EXCLUDED_JSON_ATTRIBUTES =
Constants
[:last_accessed_at, :last_accessed_api, :otp, :otp_sent_at, :api_token, :token_created_at, :tac_accepted_at, :created_at, :updated_at]
- PENDING =
"pending"- VERIFIED =
"verified"- BLOCKED =
"blocked"- STATUS =
{ PENDING => "Pending", VERIFIED => "Verified", BLOCKED => "Blocked" }
- STATUS_REVERSE =
{ "Pending" => PENDING, "Verified" => VERIFIED, "Blocked" => BLOCKED }
Instance Method Summary collapse
- #accept_tac(tac, dialing_prefix, mobile_number) ⇒ Object
-
#as_json(options = {}) ⇒ Object
Exclude some attributes info from json output.
-
#block! ⇒ Object
change the status to :blocked Return the status == Examples >>> device.block! => “blocked”.
-
#blocked? ⇒ Boolean
-
Return true if the user is not blocked, else false.
-
- #can_be_deleted? ⇒ Boolean
-
#can_be_edited? ⇒ Boolean
Permission Methods ——————.
-
#display_name ⇒ Object
-
Return full name == Examples >>> device.display_mobile_number => “+919880123456”.
-
-
#generate_otp ⇒ Object
Authentication Methods ———————-.
-
#pending! ⇒ Object
change the status to :pending Return the status == Examples >>> device.pending! => “pending”.
-
#pending? ⇒ Boolean
-
Return true if the user is pending, else false.
-
- #resend_otp(dialing_prefix, mobile_number) ⇒ Object
- #send_otp ⇒ Object
- #tac_accepted? ⇒ Boolean
- #validate_otp(otp, dialing_prefix, mobile_number) ⇒ Object
-
#verified? ⇒ Boolean
-
Return true if the user is not verified, else false.
-
-
#verify! ⇒ Object
change the status to :verified Return the status == Examples >>> device.verify! => “verified”.
Instance Method Details
#accept_tac(tac, dialing_prefix, mobile_number) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'app/models/device.rb', line 199 def accept_tac(tac, dialing_prefix, mobile_number) # Validate OTP and other parameters validation_errors = {} # Check if terms and conditions was accepted unless ["true", "yes", "t", "y", "1"].include?(tac.to_s.downcase) validation_errors[:terms_and_conditions] = "must be true" return false, validation_errors end validation_errors[:mobile_number] = "doesn't match with our database" unless self.registration.mobile_number.to_s == mobile_number.to_s validation_errors[:dialing_prefix] = "doesn't match with our database" unless self.registration.dialing_prefix.to_s == dialing_prefix.to_s return false, validation_errors unless validation_errors.empty? # Create API Token if OTP is verified self.tac_accepted_at = Time.now self.save # Verify the Registration as it is now complete self.registration.verify! # Approve the user if it not already approved self.registration.user.approve! if self.registration.user return true, {} end |
#as_json(options = {}) ⇒ Object
Exclude some attributes info from json output.
70 71 72 73 74 75 76 |
# File 'app/models/device.rb', line 70 def as_json(={}) [:except] ||= EXCLUDED_JSON_ATTRIBUTES #options[:methods] = [] #options[:methods] << :api_token json = super() Hash[*json.map{|k, v| [k, v || ""]}.flatten] end |
#block! ⇒ Object
change the status to :blocked Return the status
Examples
>>> device.block!
=> "blocked"
128 129 130 |
# File 'app/models/device.rb', line 128 def block! self.update_attribute(:status, BLOCKED) end |
#blocked? ⇒ Boolean
-
Return true if the user is not blocked, else false.
Examples
>>> device.blocked?
=> true
101 102 103 |
# File 'app/models/device.rb', line 101 def blocked? (status == BLOCKED) end |
#can_be_deleted? ⇒ Boolean
139 140 141 |
# File 'app/models/device.rb', line 139 def can_be_deleted? false end |
#can_be_edited? ⇒ Boolean
Permission Methods
135 136 137 |
# File 'app/models/device.rb', line 135 def can_be_edited? false end |
#display_name ⇒ Object
-
Return full name
Examples
>>> device.display_mobile_number
=> "+919880123456"
239 240 241 |
# File 'app/models/device.rb', line 239 def display_name "#{self.device_name} - #{self.uuid}" end |
#generate_otp ⇒ Object
Authentication Methods
146 147 148 149 150 151 |
# File 'app/models/device.rb', line 146 def generate_otp self.otp = rand(10000..99999) self.otp_sent_at = nil self.otp_verified_at = nil self.save end |
#pending! ⇒ Object
change the status to :pending Return the status
Examples
>>> device.pending!
=> "pending"
110 111 112 |
# File 'app/models/device.rb', line 110 def pending! self.update_attribute(:status, PENDING) end |
#pending? ⇒ Boolean
-
Return true if the user is pending, else false.
Examples
>>> device.pending?
=> true
85 86 87 |
# File 'app/models/device.rb', line 85 def pending? (status == PENDING) end |
#resend_otp(dialing_prefix, mobile_number) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/models/device.rb', line 180 def resend_otp(dialing_prefix, mobile_number) # Validate OTP and other parameters validation_errors = {} validation_errors[:mobile_number] = "doesn't match with our database" unless self.registration.mobile_number.to_s == mobile_number.to_s validation_errors[:dialing_prefix] = "doesn't match with our database" unless self.registration.dialing_prefix.to_s == dialing_prefix.to_s return false, validation_errors unless validation_errors.empty? self.send_otp return true, {} end |
#send_otp ⇒ Object
194 195 196 197 |
# File 'app/models/device.rb', line 194 def send_otp self.generate_otp return true end |
#tac_accepted? ⇒ Boolean
228 229 230 |
# File 'app/models/device.rb', line 228 def tac_accepted? self.tac_accepted_at.present? && self.tac_accepted_at < Time.now end |
#validate_otp(otp, dialing_prefix, mobile_number) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/models/device.rb', line 153 def validate_otp(otp, dialing_prefix, mobile_number) # Validate OTP and other parameters validation_errors = {} # TODO - remove 11111 after implementing Twilio validation_errors[:otp] = "doesn't match with our database" unless (self.otp.to_s == otp.to_s or otp.to_s == "11111") validation_errors[:mobile_number] = "doesn't match with our database" unless self.registration.mobile_number.to_s == mobile_number.to_s validation_errors[:dialing_prefix] = "doesn't match with our database" unless self.registration.dialing_prefix.to_s == dialing_prefix.to_s return false, validation_errors unless validation_errors.empty? # Create API Token if OTP is verified self.otp_verified_at = Time.now self.api_token = SecureRandom.hex self.token_created_at = Time.now self.save self.verify! # Clearing the OTP so that next time if he uses the same, it shows error self.otp = nil self.save return true, {} end |
#verified? ⇒ Boolean
-
Return true if the user is not verified, else false.
Examples
>>> device.verified?
=> true
93 94 95 |
# File 'app/models/device.rb', line 93 def verified? (status == VERIFIED) end |
#verify! ⇒ Object
change the status to :verified Return the status
Examples
>>> device.verify!
=> "verified"
119 120 121 |
# File 'app/models/device.rb', line 119 def verify! self.update_attribute(:status, VERIFIED) end |