Class: FaaStRuby::User

Inherits:
BaseObject show all
Defined in:
lib/faastruby/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseObject

#assign_attributes, #attributes=, #call_api, #initialize, #mass_assign

Constructor Details

This class inherits a constructor from FaaStRuby::BaseObject

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def api_secret
  @api_secret
end

#confirmation_tokenObject

Returns the value of attribute confirmation_token.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def confirmation_token
  @confirmation_token
end

#emailObject

Returns the value of attribute email.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def email
  @email
end

#errorsObject

Returns the value of attribute errors.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def errors
  @errors
end

#passwordObject

Returns the value of attribute password.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def password
  @password
end

#status_codeObject

Returns the value of attribute status_code.



23
24
25
# File 'lib/faastruby/user.rb', line 23

def status_code
  @status_code
end

Class Method Details

.create(email:, password:) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/faastruby/user.rb', line 3

def self.create(email:, password:)
  api = API.new
  user = User.new(email: email)
  response = api.(email: email, password: password)
  user.status_code = response.code
  if response.errors.any?
    user.errors += response.errors
    return user
  end
  case response.code
  when 422
    user.errors += ['(422) Unprocessable Entity', response.body]
  when 200, 201
    true
  else
    user.errors << "(#{response.code}) Error"
  end
  return user
end

Instance Method Details

#assign_credentials(response) ⇒ Object



31
32
33
34
35
36
# File 'lib/faastruby/user.rb', line 31

def assign_credentials(response)
  return if response.body['credentials'].nil?

  @api_key = response.body['credentials']['api_key']
  @api_secret = response.body['credentials']['api_secret']
end

#confirm_account!Object



49
50
51
52
53
54
# File 'lib/faastruby/user.rb', line 49

def confirm_account!
  response = call_api { @api.(@confirmation_token) }
  assign_credentials(response) unless response.errors.any?

  self
end

#has_credentials?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/faastruby/user.rb', line 45

def has_credentials?
  @api_key && @api_secret
end

#loginObject



38
39
40
41
42
43
# File 'lib/faastruby/user.rb', line 38

def 
  response = call_api { @api.(email: @email, password: @password) }
  assign_credentials(response) unless response.errors.any?

  self
end

#logout(all: false) ⇒ Object



25
26
27
28
29
# File 'lib/faastruby/user.rb', line 25

def logout(all: false)
  response = call_api { @api.logout(api_key: @api_key, api_secret: @api_secret, all: all) }

  self
end

#save_credentialsObject



62
63
64
65
66
# File 'lib/faastruby/user.rb', line 62

def save_credentials

  credentials_file = NewCredentials::CredentialsFile.new
  credentials_file.save(email: @email, api_key: @api_key, api_secret: @api_secret)
end

#send_confirmation_codeObject



56
57
58
59
60
# File 'lib/faastruby/user.rb', line 56

def send_confirmation_code
  response = call_api { @api.send_confirmation_code(@email) }

  self
end