Class: PhoneVerificationForGuestService

Inherits:
PhoneVerificationService show all
Defined in:
app/services/phone_verification_for_guest_service.rb

Overview

Guest otp request and verification

Instance Attribute Summary

Attributes inherited from PhoneVerificationService

#expiry_time, #first_request, #session, #verification_code

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from PhoneVerificationService

#build_otp_record, #generated_code, #get_domain_name, #match?, #otp_time_update, #phone_data_temp, #request_data_otp, #send_sms, #send_sms_otp, #user_verification_enabled?

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(phone, verification_code = nil, **options) ⇒ PhoneVerificationForGuestService

Returns a new instance of PhoneVerificationForGuestService.



3
4
5
6
7
8
9
10
11
# File 'app/services/phone_verification_for_guest_service.rb', line 3

def initialize(phone, verification_code = nil, **options)
  @ph = Phonelib.parse(phone)
  @phone = "#{@ph.country_code}#{@ph.raw_national}"
  @verification_code = verification_code.to_i
  session = options[:session]
  expiry_time = options.fetch(:expiry_time, 183.seconds)
  first_request = options.fetch(:first_request, false)
  super(@phone, @verification_code, session: session, expiry_time: expiry_time, first_request: first_request)
end

Instance Method Details

#request_codeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/phone_verification_for_guest_service.rb', line 13

def request_code
  errors.clear
  if phone_data_temp.present? && phone_data_temp[:is_verified]
    raise InvalidPhoneVerification, 'Phone number has been verified'
  end

  if @ph.country_code != '66' || !user_verification_enabled?
    raise InvalidPhoneVerification, 'Phone number has been verified'
  end

  build_otp_record

  if errors.present?
    ServiceResult.new errors: errors, message: error_message_simple
  else
    ServiceResult.new data: phone_data_temp
  end
rescue InvalidPhoneVerification => e
  errors.add :base, e.message
  ServiceResult.new errors: errors, message: error_message_simple
end

#verifyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/phone_verification_for_guest_service.rb', line 35

def verify
  errors.clear
  raise InvalidPhoneVerification, 'Failed to verify phone number' if phone_data_temp.nil?

  raise InvalidPhoneVerification, 'Verification code is wrong' unless match?

  phone_data_temp(true, phone_data_temp[:code])

  if errors.present?
    ServiceResult.new errors: errors, message: error_message_simple
  else
    ServiceResult.new data: phone_data_temp
  end
rescue InvalidPhoneVerification => e
  errors.add :base, e.message
  ServiceResult.new errors: errors, message: error_message_simple
end