Class: Otp

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Otp

Initializes an object with an API key.



9
10
11
# File 'lib/mslm/otp.rb', line 9

def initialize(api_key)
    @lib = Lib.new(api_key)
end

Instance Attribute Details

#libObject (readonly)

Class for performing otp using an API.



6
7
8
# File 'lib/mslm/otp.rb', line 6

def lib
  @lib
end

Instance Method Details

#send(otp_req, opts = nil) ⇒ Object

Sends an OTP to the specified phone number with the provided options.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mslm/otp.rb', line 29

def send(otp_req, opts = nil)
    opt = ReqOpts::Builder.new
                        .with_api_key(@lib.api_key)
                        .with_base_url(@lib.base_url)
                        .with_http_client(@lib.http)
                        .with_user_agent(@lib.user_agent)
                        .build

    opt = opts if opts
    query_params = {
        'phone' => otp_req['phone'],
        'tmpl_sms' => otp_req['tmpl_sms'],
        'token_len' => otp_req['token_len'],
        'expire_seconds' => otp_req['expire_seconds']
    }

    target_url = @lib.prepare_url('/api/otp/v1/send', {}, opt)

    resp = @lib.req_and_resp(target_url, opt, 'POST', query_params.to_json)
    resp
end

#set_api_key(api_key) ⇒ Object

Sets the API key for authentication.



24
25
26
# File 'lib/mslm/otp.rb', line 24

def set_api_key(api_key)
    @lib.set_api_key(api_key)
end

#set_http_client(http_client) ⇒ Object

Sets the HTTP client to be used for making requests.



14
15
16
# File 'lib/mslm/otp.rb', line 14

def set_http_client(http_client)
    @lib.set_http_client(http_client)
end

#set_user_agent(user_agent) ⇒ Object

Sets the user agent to be used in HTTP requests.



19
20
21
# File 'lib/mslm/otp.rb', line 19

def set_user_agent(user_agent)
    @lib.set_user_agent(user_agent)
end

#verify(otp_token_req, opts = nil) ⇒ Object

Verifies an OTP token for the specified phone number with the provided options.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mslm/otp.rb', line 52

def verify(otp_token_req, opts = nil)
    opt = ReqOpts::Builder.new
                        .with_api_key(@lib.api_key)
                        .with_base_url(@lib.base_url)
                        .with_http_client(@lib.http)
                        .with_user_agent(@lib.user_agent)
                        .build

    opt = opts if opts
    query_params = {
        'phone' => otp_token_req["phone"],
        'token' => otp_token_req["token"],
        'consume' => otp_token_req["consume"]
    }

    target_url = @lib.prepare_url('/api/otp/v1/token_verify', {}, opt)

    resp = @lib.req_and_resp(target_url, opt, 'POST', query_params.to_json)
    resp
end