Class: OtpRequestService

Inherits:
BaseOperationService show all
Includes:
DefaultErrorContainer
Defined in:
app/services/otp_request_service.rb

Overview

To verify user on registration process could be by Email or SMS

Instance Attribute Summary

Attributes inherited from BaseOperationService

#outcome

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from BaseOperationService

#success?

Constructor Details

#initialize(user_id, otp_code_param = nil, verify_by = nil) ⇒ OtpRequestService

Returns a new instance of OtpRequestService.



9
10
11
12
13
14
# File 'app/services/otp_request_service.rb', line 9

def initialize(user_id, otp_code_param=nil, verify_by=nil)
  @user = User.find(user_id)
  @otp_code_param = otp_code_param
  @verify_by = verify_by
  update_otp_was_sent_at
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/otp_request_service.rb', line 16

def valid?
  errors.add(:base, 'Request OTP failed, please try again after 3 minutes')
  return false unless otp_was_sent_at.nil? || otp_was_sent_at < Time.zone.now

  generate_otp_code = SecureRandom.random_number(99999)
  if user.update(verify_code: generate_otp_code)
    send_request_otp
    true
  else
    errors.add(:base, 'Request OTP Code failed')
    false
  end
end

#verifyObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/otp_request_service.rb', line 30

def verify
  errors.add(:base, 'Verification code not valid')
  return false if otp_code_param.to_i != user.verify_code
  errors.clear

  if user.confirm
    true
  else
    errors.add(:base, user.errors.full_messages.to_sentence)
    false
  end
end