lib/ayadn/authorize.rb in ayadn-1.8.2 vs lib/ayadn/authorize.rb in ayadn-2.0
- old
+ new
@@ -1,121 +1,156 @@
# encoding: utf-8
module Ayadn
class Authorize
+ def initialize
+ @thor = Thor::Shell::Color.new # local statuses
+ @status = Status.new # global statuses + utils
+ end
+
def authorize
+ puts "\n"
+ if File.exist?(Dir.home + "/ayadn/accounts.db")
+ @status.has_to_migrate
+ exit
+ end
puts "\e[H\e[2J"
- try_remove_old_ayadn
show_link
token = get_token
check_token(token)
- puts "\n\nThanks! Contacting App.net...\n".color(:green)
+ puts "\e[H\e[2J"
+ @thor.say_status :connexion, "downloading user info", :yellow
user = create_user_data(token, Dir.home + "/ayadn")
prepare(user)
- puts "Creating configuration...\n".color(:green)
+ @thor.say_status :create, "configuration", :yellow
Settings.load_config
Logs.create_logger
install
- puts Status.done
- Errors.info "Done!"
- puts "\nThank you for using Ayadn. Enjoy!\n\n".color(:yellow)
+ @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
+ 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
+ puts "\n"
+ rescue Interrupt
+ Status.new.canceled
+ exit
+ end
+ end
+
private
def prepare(user)
- puts "Ok! Creating Ayadn folders...\n".color(:green)
+ @thor.say_status :create, "user folders", :yellow
create_config_folders(user)
- puts "Saving user token...\n".color(:green)
+ @thor.say_status :save, "user token", :yellow
create_token_file(user)
- puts "Creating user account for #{user.handle}...\n".color(:green)
- accounts_db = Databases.init("#{user.home_path}/accounts.db")
- create_account(user, accounts_db)
+ @thor.say_status :create, "Ayadn account", :yellow
+ acc_db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
+ user_db = Amalgalite::Database.new("#{user.user_path}/db/ayadn.sqlite")
+ if user_db.schema.tables.empty?
+ Databases.create_tables(user)
+ end
+ if acc_db.schema.tables.empty?
+ Databases.create_account_table(acc_db)
+ end
+ Databases.create_account(acc_db, user)
end
def install
- puts "Creating api and config files...\n".color(:green)
- Errors.info "Creating api, version and config files..."
+ @thor.say_status :create, "api and config files", :yellow
+ Errors.info "Creating api and config files..."
Errors.info "Creating version file..."
Settings.init_config
end
- def create_account(user, accounts_db)
- accounts_db[user.username] = {username: user.username, id: user.id, handle: user.handle, path: user.user_path}
- accounts_db['ACTIVE'] = user.username
- accounts_db.flush
- accounts_db.close
- end
-
def create_token_file(user)
File.write("#{user.user_path}/auth/token", user.token)
end
def create_config_folders(user)
begin
FileUtils.mkdir_p(user.user_path)
- %w{log db pagination config auth downloads backup posts messages lists}.each do |target|
+ %w{log db config auth downloads posts messages lists}.each do |target|
Dir.mkdir("#{user.user_path}/#{target}") unless Dir.exist?("#{user.user_path}/#{target}")
end
rescue => e
- puts "\nError creating Ayadn #{user.handle} account folders.\n\n"
- puts "Error: #{e}"
+ @status.say do
+ @thor.say_status :error, "can't create #{user.handle} account folders", :red
+ end
+ @status.say { puts "\nError: #{e}" }
exit
end
end
def show_link
- puts "\nPlease click this URL, or open a browser then copy/paste it:\n".color(:cyan)
- puts Endpoints.new.authorize_url
- puts "\n"
- puts "On this page, log in with your App.net account to authorize Ayadn.\n".color(:cyan)
- puts "You will then be redirected to a page showing a 'user token' (your secret code).\n".color(:cyan)
- puts "Copy it then paste it here:\n".color(:yellow)
- print "> "
+ @status.say do
+ @thor.say_status :please, "click or copy/paste this URL in a browser", :yellow
+ puts "\n"
+ puts "\t#{Endpoints.new.authorize_url}"
+ puts "\n"
+ @thor.say_status :next, "log in to authorize Ayadn", :cyan
+ @thor.say_status nil, "you will be redirected to your 'user token'"
+ @thor.say_status :please, "copy/paste the token here:", :yellow
+ end
+ print "\t> "
end
def get_user(token)
- JSON.parse(RestClient.get("https://api.app.net/users/me?access_token=#{token}", :verify_ssl => OpenSSL::SSL::VERIFY_NONE) {|response, request, result| response })
+ begin
+ JSON.parse(RestClient.get("https://api.app.net/users/me?access_token=#{token}", :verify_ssl => OpenSSL::SSL::VERIFY_NONE) {|response, request, result| response })
+ rescue Exception => e
+ @status.say do
+ @thor.say_status :error, "connection problem", :red
+ end
+ puts "#{e}"
+ end
end
def get_token
begin
STDIN.gets.chomp()
rescue Interrupt
- puts Status.canceled
+ @status.canceled
exit
end
end
def check_token(token)
if token.empty? || token.nil?
- puts "\n\nOops, something went wrong, I couldn't get the token. Please try again.\n\n".color(:red)
+ @status.say do
+ @thor.say_status :error, "couldn't get the token", :red
+ end
exit
end
- end
-
- def try_remove_old_ayadn
- if FileOps.old_ayadn?
- answer = ask_del_old_ayadn
- unless answer.downcase == "y"
- puts Status.canceled
- exit
- end
- puts "\nDeleting old version...\n".color(:green)
- begin
- old_dir = Dir.home + "/ayadn"
- FileUtils.remove_dir(old_dir)
- rescue => e
- puts "Unable to remove folder: #{old_dir}\n\n".color(:red)
- raise e
- end
- end
- end
-
- def ask_del_old_ayadn
- puts "\nAn obsolete version of Ayadn has been detected and will be deleted. Install and authorize the new version? [y/N]\n".color(:red)
- print "> "
- STDIN.getch
end
def create_user_data(token, home_path)
resp = get_user(token)
model = Struct.new(:resp, :username, :id, :handle, :home_path, :user_path, :token)