Class: Authful::User

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ User

Returns a new instance of User.


9
10
11
12
13
# File 'lib/authful/user.rb', line 9

def initialize(args)
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#emailObject

Returns the value of attribute email


3
4
5
# File 'lib/authful/user.rb', line 3

def email
  @email
end

#errorObject

Returns the value of attribute error


3
4
5
# File 'lib/authful/user.rb', line 3

def error
  @error
end

#phoneObject

Returns the value of attribute phone


3
4
5
# File 'lib/authful/user.rb', line 3

def phone
  @phone
end

#qr_codeObject

Returns the value of attribute qr_code


3
4
5
# File 'lib/authful/user.rb', line 3

def qr_code
  @qr_code
end

#tokenObject

Returns the value of attribute token


3
4
5
# File 'lib/authful/user.rb', line 3

def token
  @token
end

Class Method Details

.[](token) ⇒ Object


5
6
7
# File 'lib/authful/user.rb', line 5

def self.[](token)
  self.new(token: token)
end

.create!(args) ⇒ Object


34
35
36
# File 'lib/authful/user.rb', line 34

def self.create!(args)
  self.new(args).save
end

Instance Method Details

#destroyObject


38
39
40
# File 'lib/authful/user.rb', line 38

def destroy
  delete("/api/users/#{token}")["ok"] == 1
end

#saveObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/authful/user.rb', line 15

def save
  r = post("/api/users", {email: email, phone: phone})
  self.qr_code = r["qr_code"]
  self.token = r["token"]
rescue
  if Authful::Errors::IncorrectApiToken === $!
    raise Authful::Errors::IncorrectApiToken
  elsif $!.http_code == 409 # Catch duplicate user -- already enrolled
    r = JSON.parse($!.response)
    self.token = r["token"]
  elsif $!.http_code == 400
    r = JSON.parse($!.response)
    self.error = r["error"]
    false
  else
    raise $!
  end
end

#send_smsObject


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/authful/user.rb', line 42

def send_sms
  get("/api/users/#{token}/send_sms")["ok"] == 1
rescue
  if Authful::Errors::IncorrectApiToken === $!
    raise Authful::Errors::IncorrectApiToken
  elsif $!.http_code == 400
    r = JSON.parse($!.response)
    self.error = r["error"]
    false
  else
    raise $!
  end
end

#valid?(otp_token) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/authful/user.rb', line 56

def valid?(otp_token)
  get("/api/users/#{token}/validate?token=#{otp_token}")["ok"] == 1
rescue
  if Authful::Errors::IncorrectApiToken === $!
    raise Authful::Errors::IncorrectApiToken
  elsif $!.http_code == 403
    r = JSON.parse($!.response)
    self.error = r["error"]
    false
  else
    raise $!
  end
end