Class: Ayadn::Authorize

Inherits:
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
9
# File 'lib/ayadn/authorize.rb', line 5

def initialize
  @thor = Thor::Shell::Color.new # local statuses
  @status = Status.new # global statuses + utils
  @baseURL = "https://api.app.net" # may be overriden
end

Instance Method Details

#authorize(options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ayadn/authorize.rb', line 11

def authorize(options)
  puts "\n"
  if File.exist?(Dir.home + "/ayadn/accounts.db")
    @status.deprecated_ayadn
    exit
  end
  api_file = Dir.home + "/ayadn/.api.yml"
  # overrides the default value
  if File.exist?(api_file)
    @baseURL = YAML.load(File.read(api_file))[:root]
  end
  # overrides the config file
  if options["api"]
    @baseURL = options["api"]
  end
  puts "\e[H\e[2J"
  show_link
  token = if options["token"]
    options["token"][0]
  else
    get_token
  end
  # token = get_token
  check_token(token)
  puts "\e[H\e[2J"
  @status.say_yellow :connexion, "downloading user info"
  user = create_user_data(token, Dir.home + "/ayadn")
  prepare(user)
  @status.say_yellow :create, "configuration"
  Settings.load_config
  Logs.create_logger
  install
  @status.say_green :done, "user #{user.handle} is authorized"
  Errors.info "#{user.handle} authorized."
  @status.say { @status.say_green :end, "Thank you for using Ayadn. Enjoy!" }
  Switch.new.list
end

#unauthorize(user, options) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ayadn/authorize.rb', line 49

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? [y/N]\n\n> ", :red)
    else
      sure = @thor.yes?("Are you sure you want to unauthorize user @#{user} ? [y/N]\n\n> ", :red)
    end
    unless sure == true
      Status.new.canceled
      exit
    end
    puts "\e[H\e[2J"
    @status.say_yellow :delete, "database entry for @#{user}"

    db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")

    Databases.remove_from_accounts(db, user)
    if options[:delete]
      @status.say_yellow :delete, "@#{user} user folders"
      FileUtils.remove_dir(Dir.home + "/ayadn/#{user}")
    end
    @status.say_green :done, "user @#{user} has been unauthorized"
    remaining = Databases.all_accounts(db)
    if remaining.flatten.empty?
      @status.say_info "accounts database is now empty"
    else
      username = remaining[0][0]
      Databases.(db, username)
      @status.say_info "user @#{username} is now the active user"
    end
    puts "\n"
  rescue Amalgalite::SQLite3::Error => e
      @status.not_authorized
      exit
  rescue Interrupt
    Status.new.canceled
    exit
  end
end