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 |
# 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 return true, {} end |
#as_json(options = {}) ⇒ Object
Exclude some attributes info from json output.
69 70 71 72 73 74 75 |
# File 'app/models/device.rb', line 69 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"
127 128 129 |
# File 'app/models/device.rb', line 127 def block! self.update_attribute(:status, BLOCKED) end |
#blocked? ⇒ Boolean
-
Return true if the user is not blocked, else false.
Examples
>>> device.blocked?
=> true
100 101 102 |
# File 'app/models/device.rb', line 100 def blocked? (status == BLOCKED) end |
#can_be_deleted? ⇒ Boolean
138 139 140 |
# File 'app/models/device.rb', line 138 def can_be_deleted? false end |
#can_be_edited? ⇒ Boolean
Permission Methods
134 135 136 |
# File 'app/models/device.rb', line 134 def can_be_edited? false end |
#display_name ⇒ Object
-
Return full name
Examples
>>> device.display_mobile_number
=> "+919880123456"
233 234 235 |
# File 'app/models/device.rb', line 233 def display_name "#{self.device_name} - #{self.uuid}" end |
#generate_otp ⇒ Object
Authentication Methods
145 146 147 148 149 150 |
# File 'app/models/device.rb', line 145 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"
109 110 111 |
# File 'app/models/device.rb', line 109 def pending! self.update_attribute(:status, PENDING) end |
#pending? ⇒ Boolean
-
Return true if the user is pending, else false.
Examples
>>> device.pending?
=> true
84 85 86 |
# File 'app/models/device.rb', line 84 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
222 223 224 |
# File 'app/models/device.rb', line 222 def tac_accepted? self.tac_accepted_at.present? && self.tac_accepted_at < Time.now end |
#validate_otp(otp, dialing_prefix, mobile_number) ⇒ Object
152 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 152 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! self.registration.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
92 93 94 |
# File 'app/models/device.rb', line 92 def verified? (status == VERIFIED) end |
#verify! ⇒ Object
change the status to :verified Return the status
Examples
>>> device.verify!
=> "verified"
118 119 120 |
# File 'app/models/device.rb', line 118 def verify! self.update_attribute(:status, VERIFIED) end |