Class: Ayadn::Authorize

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/authorize.rb

Instance Method Summary collapse

Constructor Details

#initializeAuthorize

Returns a new instance of Authorize.



5
6
7
8
# File 'lib/ayadn/authorize.rb', line 5

def initialize
  @thor = Thor::Shell::Color.new # local statuses
  @status = Status.new # global statuses + utils
end

Instance Method Details

#authorizeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ayadn/authorize.rb', line 10

def authorize
  puts "\n"
  if File.exist?(Dir.home + "/ayadn/accounts.db")
    @status.has_to_migrate
    exit
  end
  puts "\e[H\e[2J"
  show_link
  token = get_token
  check_token(token)
  puts "\e[H\e[2J"
  @thor.say_status :connexion, "downloading user info", :yellow
  user = create_user_data(token, Dir.home + "/ayadn")
  prepare(user)
  @thor.say_status :create, "configuration", :yellow
  Settings.load_config
  Logs.create_logger
  install
  @thor.say_status :done, "user #{user.handle} is authorized", :green
  Errors.info "#{user.handle} authorized."
  @status.say { @thor.say_status :end, "Thank you for using Ayadn. Enjoy!", :green }
  Switch.new.list
end

#unauthorize(user, options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ayadn/authorize.rb', line 34

def unauthorize(user, options)
  begin
    @workers = Workers.new
    if user.size > 1
      @status.one_username
      exit
    end
    user = @workers.remove_arobase_if_present(user)[0]
    puts "\e[H\e[2J"
    if options[:delete]
      sure = @thor.yes?("Are you sure you want to unauthorize user @#{user} and delete its folders?\n\n> ", :red)
    else
      sure = @thor.yes?("Are you sure you want to unauthorize user @#{user} ?\n\n> ", :red)
    end
    unless sure == true
      Status.new.canceled
      exit
    end
    puts "\e[H\e[2J"
    @thor.say_status :delete, "database entry for @#{user}", :yellow
    db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
    Databases.remove_from_accounts(db, user)
    if options[:delete]
      @thor.say_status :delete, "@#{user} user folders", :yellow
      FileUtils.remove_dir(Dir.home + "/ayadn/#{user}")
    end
    @thor.say_status :done, "user @#{user} has been unauthorized", :green
    remaining = Databases.all_accounts(db)
    if remaining.flatten.empty?
      @thor.say_status :info, "accounts database is now empty", :cyan
    else
      username = remaining[0][0]
      Databases.(db, username)
      @thor.say_status :info, "user @#{username} is now the active user", :cyan
    end
    puts "\n"
  rescue Interrupt
    Status.new.canceled
    exit
  end
end