Class: Supso::User

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

Constant Summary collapse

@@current_user =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, name, id, auth_token = nil) ⇒ User

Returns a new instance of User.



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

def initialize(email, name, id, auth_token = nil)
  @email = email
  @name = name
  @id = id
  @auth_token = auth_token
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



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

def auth_token
  @auth_token
end

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.attach_to_email!(email) ⇒ Object



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

def self.attach_to_email!(email)
  data = {
      email: email,
  }

  response = Util.http_post("#{ Supso.supso_api_root }users/attach", data)

  if response['success']
    User.set_current_user!(response['user']['email'], response['user']['name'],
        response['user']['id'])
  else
    puts response['reason']
    # Anything here needed?
  end
end

.current_userObject



62
63
64
# File 'lib/supso/user.rb', line 62

def self.current_user
  @@current_user ||= User.current_user_from_file
end

.current_user_filenameObject



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

def self.current_user_filename
  "#{ Supso.user_supso_config_root }/current_user.json"
end

.current_user_from_fileObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/supso/user.rb', line 42

def self.current_user_from_file
  if !File.exist?(User.current_user_filename)
    return nil
  end

  user_data = {}
  begin
    user_data = JSON.parse(File.read(User.current_user_filename))
    user_data = {} if !user_data.is_a?(Object)
  rescue JSON::ParserError => err
    user_data = {}
  end

  if user_data['email'] || user_data['auth_token']
    User.new(user_data['email'], user_data['name'], user_data['id'], user_data['auth_token'])
  else
    nil
  end
end

.log_in_with_confirmation_token!(email, confirmation_token) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/supso/user.rb', line 107

def self.(email, confirmation_token)
  data = {
      email: email,
      confirmation_token: confirmation_token,
  }

  response = Util.http_post("#{ Supso.supso_api_root }users/confirm", data)

  if response['success']
    User.set_current_user!(response['user']['email'], response['user']['name'],
        response['user']['id'], response['auth_token'])
    if Organization.current_organization.nil?
      Organization.set_current_organization!(response['organization']['name'], response['organization']['id'])
    end
    [true, nil]
  else
    [false, response['reason']]
  end
end

.log_in_with_password!(email, password) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/supso/user.rb', line 87

def self.(email, password)
  data = {
      email: email,
      password: password,
  }

  response = Util.http_post("#{ Supso.supso_api_root }sign_in", data)

  if response['success']
    User.set_current_user!(response['user']['email'], response['user']['name'],
        response['user']['id'], response['auth_token'])
    if Organization.current_organization.nil?
      Organization.set_current_organization!(response['organization']['name'], response['organization']['id'])
    end
    [true, nil]
  else
    [false, response['reason']]
  end
end

.log_out!Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/supso/user.rb', line 127

def self.log_out!
  if File.exists?(User.current_user_filename)
    user = User.current_user
    if user && user.auth_token
      data = {
          version: Supso::VERSION,
          auth_token: user.auth_token,
          user_id: user.id,
      }

      response = Util.http_post("#{ Supso.supso_api_root }sign_out", data)

      if response['success']
        # No need to say anything like 'logout succeeded'
      else
        puts response['reason']
      end
    end

    File.delete(User.current_user_filename)
    @@current_user = nil
  end
end

.readme_filenameObject



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

def self.readme_filename
  "#{ Supso.user_supso_config_root }/README.txt"
end

.save_user_supso_readme!Object



151
152
153
154
155
156
157
158
159
# File 'lib/supso/user.rb', line 151

def self.save_user_supso_readme!
  if !File.exists?(User.readme_filename)
    readme_contents = File.open("#{ Supso.gem_root }/lib/templates/user_dir_readme.txt", 'r').read
    Util.ensure_path_exists!(User.readme_filename)
    file = File.open(User.readme_filename, 'w')
    file << readme_contents
    file.close
  end
end

.set_current_user!(email, name, id, auth_token = nil) ⇒ Object



66
67
68
69
# File 'lib/supso/user.rb', line 66

def self.set_current_user!(email, name, id, auth_token = nil)
  @@current_user = User.new(email, name, id, auth_token)
  @@current_user.save_to_file!
end

Instance Method Details

#save_to_file!Object



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

def save_to_file!
  Util.ensure_path_exists!(User.current_user_filename)
  file = File.open(User.current_user_filename, 'w')
  file << self.saved_data.to_json
  file.close
  User.save_user_supso_readme!
end

#saved_dataObject



24
25
26
27
28
29
30
31
32
# File 'lib/supso/user.rb', line 24

def saved_data
  data = {
      'email' => self.email
  }
  data['name'] = self.name if self.name
  data['id'] = self.id if self.id
  data['auth_token'] = self.auth_token if self.auth_token
  data
end