Class: Device

Inherits:
ApplicationRecord
  • Object
show all
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

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(options={})
  options[:except] ||= EXCLUDED_JSON_ATTRIBUTES
  #options[:methods] = []
  #options[:methods] << :api_token
  json = super(options)
  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

Returns:

  • (Boolean)


101
102
103
# File 'app/models/device.rb', line 101

def blocked?
  (status == BLOCKED)
end

#can_be_deleted?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/device.rb', line 139

def can_be_deleted?
  false
end

#can_be_edited?Boolean

Permission Methods


Returns:

  • (Boolean)


135
136
137
# File 'app/models/device.rb', line 135

def can_be_edited?
  false
end

#change_number(otp, dialing_prefix, mobile_number, new_dialing_prefix, new_mobile_number) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/device.rb', line 228

def change_number(otp, dialing_prefix, mobile_number, new_dialing_prefix, new_mobile_number)
  
  # Validate OTP and other parameters
  validation_errors = {}

  # TODO - remove 11111 after implementing Twilio
  validation_errors[:otp] = "can't be empty" if otp.blank?
  
  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
  
  self.registration.dialing_prefix = new_dialing_prefix
  self.registration.mobile_number = new_mobile_number
  self.registration.valid?
  validation_errors[:mobile_number] = self.registration.errors[:mobile_number] unless self.registration.errors[:mobile_number].blank?
  validation_errors[:dialing_prefix] = self.registration.errors[:dialing_prefix] unless self.registration.errors[:dialing_prefix].blank?

  self.user.phone = new_dialing_prefix + new_mobile_number
  self.user.valid?
  validation_errors[:user_phone] = self.user.errors[:phone] unless self.user.errors[:phone].blank?

  return false, validation_errors unless validation_errors.empty?
  
  self.registration.save
  self.user.save
  
  # Clearing the OTP so that next time if he uses the same, it shows error
  self.otp = nil
  self.save

  return true, {}
end

#display_nameObject

  • Return full name

Examples

>>> device.display_mobile_number
=> "+919880123456"


273
274
275
# File 'app/models/device.rb', line 273

def display_name
  "#{self.device_name} - #{self.uuid}"
end

#generate_otpObject

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 = Time.now
  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

Returns:

  • (Boolean)


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_otpObject



194
195
196
197
# File 'app/models/device.rb', line 194

def send_otp
  self.generate_otp
  return true
end

#tac_accepted?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'app/models/device.rb', line 262

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
  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

Returns:

  • (Boolean)


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