Class: SendOtp::Otp

Inherits:
Object
  • Object
show all
Defined in:
lib/send_otp.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_key, message_template = nil) ⇒ Otp

Creates a new SendOtp instance

Parameters:

  • auth_jey (string)

    Authentication key

  • message_template (string, optional) (defaults to: nil)


16
17
18
19
20
21
22
23
# File 'lib/send_otp.rb', line 16

def initialize(auth_key, message_template=nil)
  @auth_key = auth_key
  if message_template
    @message_template = message_template
  else
    @message_template = 'Your otp is {{otp}}. Please do not share it with anybody'
  end
end

Instance Method Details

#retry(contact_number, retry_voice) ⇒ Object

Retry Otp to given mobile number

Parameters:

  • contact_number (string)

    receiver’s mobile number along with country code

  • retry_voice, (boolean)

    false to retry otp via text call, default true



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/send_otp.rb', line 50

def retry(contact_number, retry_voice)
  # if retry voice is false, set `retry_type` as `text` else set it to `voice` call msg91 API to retry otp
  # return msg91 API response
  retry_type =  'voice'
  retry_type = 'text' unless retry_voice
  args = {
      authkey: @auth_key,
      mobile: contact_number,
      retrytype: retry_type
  }
  do_request('retryotp.php', args)
end

#send_otp(contact_number, sender_id, otp = '', n = 4) ⇒ Object

Send Otp to given mobile number

Parameters:

  • contact_number (string)

    receiver’s mobile number along with country code

  • sender_id (string)
  • otp (string, optional) (defaults to: '')
  • n (string, optional) (defaults to: 4)

    number of digits of Otp



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/send_otp.rb', line 31

def send_otp(contact_number, sender_id, otp = '', n = 4)
  otp = generate_otp(n) if otp.to_s.empty?
  @message_template.gsub! '{{otp}}', otp.to_s
  args = {
      authkey: @auth_key,
      mobile: contact_number,
      sender: sender_id,
      message: @message_template,
      otp: otp
  }

  do_request('sendotp.php', args)
end

#verify(contact_number, otp) ⇒ Object

Verify Otp to given mobile number Return true if OTP verified successfully

Parameters:

  • contact_number (string)

    receiver’s mobile number along with country code

  • otp (string)

    otp to verify



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/send_otp.rb', line 70

def verify(contact_number, otp)
  args = {
      authkey: @auth_key,
      mobile: contact_number,
      otp: otp
  }

  response = do_request('verifyRequestOTP.php', args)
  body = eval(response.body)

  if body[:type] == 'success'
    true
  else
    false
  end
rescue
  false
end