Class: WePredict::CLI::User

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/wepredict/cli/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



101
102
103
# File 'lib/wepredict/cli/user.rb', line 101

def self.banner(task, namespace = true, subcommand = false)
  "#{basename} #{self.name.split('::').last.downcase} #{task.formatted_usage(self, false, subcommand)}"
end

Instance Method Details

#change_email(email, new_email) ⇒ Object



72
73
74
75
76
77
# File 'lib/wepredict/cli/user.rb', line 72

def change_email(email, new_email)
  password = authenticate(email)
  WePredict::User.change_email(:email => email, :password => password, :new_email => email)
  
  say "Sucessfully changed your email address", :green
end

#change_password(email) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/wepredict/cli/user.rb', line 63

def change_password(email)
  password = authenticate(email)
  new_password = capture_password(true)
  WePredict::User.change_password(:email => email, :password => password, :new_password => new_password)

  say "Sucessfully changed your password", :green
end

#delete(email) ⇒ Object



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

def delete(email)
  confirm = yes?("Are you sure you want to close your account? This is non-reversable (y/n):")

  if !confirm
    say "Phew, almost lost you there!" 
    exit
  end

  begin
    password = capture_password
    WePredict::User.delete(:email => email, :password => password)
  rescue WePredict::Errors::AccessDenied => e
    say e.message, :red
    retry
  end
  
  say "Sucessfully closed your WePredict account", :green
end

#forgotten_password(email) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wepredict/cli/user.rb', line 80

def forgotten_password(email)
  begin
    WePredict::User.forgotten_password(:email => email)
  rescue WePredict::Errors::BadRequest
    say "We can't find you! Maybe you want to signup instead?", :red
    exit
  end
  
  say "We have sent you a super secret code to your email, go check your email and then enter the code here!"
  begin
    code = ask "Super secret code:"
    password = capture_password(true)
    WePredict::User.reset_forgotten_password(:email => email, :forgotten_password_code => code, :new_password => password)
  rescue WePredict::Errors::AccessDenied
    say "That's not the right super secret code! Try again", :red
    retry
  end

  say "Sucessfully reset your password", :green
end

#login(email) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wepredict/cli/user.rb', line 30

def (email)
  begin
    password = capture_password
    u = WePredict::User.get(:email => email, :password => password)
  rescue WePredict::Errors::AccessDenied => e
    say e.message, :red
    retry
  end

  write_api_key(u[:api_key])
end

#signup(email) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wepredict/cli/user.rb', line 11

def (email)
  begin
    password = capture_password(true)
    u = WePredict::User.create(:email => email, :password => password)
  rescue WePredict::Errors::BadRequest => e
    if(e.body[:error] == "user_exists")
      say "User already exists, maybe try logging in?", :red
      exit
    end

    say e.message, :red
    retry
  end

  say "\nThankyou for signing up for WePredict", :green
  write_api_key(u[:api_key])
end