Class: Ayadn::Switch

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

Instance Method Summary collapse

Instance Method Details

#listObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ayadn/switch.rb', line 5

def list
  home_path = Dir.home + "/ayadn"
  if File.exist?("#{home_path}/accounts.db")
    accounts_db = Databases.init("#{home_path}/accounts.db")
    active = accounts_db['ACTIVE']
    begin
      puts "\nCurrently authorized accounts:\n".color(:cyan)
      accounts_db.each do |acc|
        next if acc[0] == 'ACTIVE'
        if acc[1][:username] == active
          puts "#{acc[1][:handle]}".color(:red)
        else
          puts "#{acc[1][:handle]}".color(:green)
        end
      end
      puts "\n"
    ensure
      close_db(accounts_db)
    end
  else
    please
  end
end

#switch(user) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ayadn/switch.rb', line 29

def switch(user)
  if user.empty? || user.nil?
    puts "\n\nOops, something went wrong, I couldn't get your username. Please try again.\n\n".color(:red)
    exit
  end
  #puts "\e[H\e[2J"
  username = Workers.new.remove_arobase_if_present([user.first])[0]
  home_path = Dir.home + "/ayadn"
  if File.exist?("#{home_path}/accounts.db")
    accounts_db = Databases.init("#{home_path}/accounts.db")
    active = accounts_db['ACTIVE']
    if username == accounts_db[active][:username]
      puts "\nYou're already authorized with username '#{accounts_db[active][:handle]}'.\n".color(:red)
      cancel(accounts_db)
    end
    if accounts_db[username]
      puts "\nSwitching to account @#{username}...".color(:green)
      accounts_db['ACTIVE'] = username
      close_db(accounts_db)
      puts Status.done
      exit
    else
      puts "\nThis account isn't in the database. Please run 'ayadn authorize'.\n".color(:red)
      cancel(accounts_db)
    end
  else
    please
  end
end