class Ayadn::Authorize
Public Class Methods
new()
click to toggle source
# File lib/ayadn/authorize.rb, line 5 def initialize @status = Status.new # global statuses + utils @baseURL = "https://api.app.net" # may be overriden end
Public Instance Methods
Private Instance Methods
check_token(token)
click to toggle source
# File lib/ayadn/authorize.rb, line 183 def check_token(token) if token.empty? || token.nil? @status.say do @status.say_error "couldn't get the token" end exit end end
create_config_folders(user)
click to toggle source
# File lib/ayadn/authorize.rb, line 135 def create_config_folders(user) begin FileUtils.mkdir_p(user.user_path) %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 @status.say do @status.say_error "can't create #{user.handle} account folders" end @status.say { puts "\nError: #{e}" } exit end end
create_token_file(user)
click to toggle source
# File lib/ayadn/authorize.rb, line 131 def create_token_file(user) File.write("#{user.user_path}/auth/token", user.token) end
create_user_data(token, home_path)
click to toggle source
# File lib/ayadn/authorize.rb, line 192 def create_user_data(token, home_path) resp = get_user(token) model = Struct.new(:resp, :username, :id, :handle, :home_path, :user_path, :token) username = resp['data']['username'] model.new(resp, username, resp['data']['id'], "@" + username, home_path, home_path + "/#{username}", token) end
get_token()
click to toggle source
# File lib/ayadn/authorize.rb, line 174 def get_token begin STDIN.gets.chomp() rescue Interrupt @status.canceled exit end end
get_user(token)
click to toggle source
# File lib/ayadn/authorize.rb, line 163 def get_user(token) begin JSON.parse(RestClient.get("#{@baseURL}/users/me?access_token=#{token}", :verify_ssl => OpenSSL::SSL::VERIFY_NONE) {|response, request, result| response }) rescue Exception => e @status.say do @status.say_error "connection problem" end puts "#{e}" end end
install()
click to toggle source
# File lib/ayadn/authorize.rb, line 124 def install @status.say_yellow :create, "api and config files" Errors.info "Creating api and config files..." Errors.info "Creating version file..." Settings.init_config end
prepare(user)
click to toggle source
# File lib/ayadn/authorize.rb, line 107 def prepare(user) @status.say_yellow :create, "user folders" create_config_folders(user) @status.say_yellow :save, "user token" create_token_file(user) @status.say_yellow :create, "Ayadn account" 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
show_link()
click to toggle source
# File lib/ayadn/authorize.rb, line 150 def show_link @status.say do @status.say_yellow :please, "click or copy/paste this URL in a browser" puts "\n" puts "\t#{Endpoints.new.authorize_url}" puts "\n" @status.say_cyan :next, "log in to authorize Ayadn" @status.say_center "you will be redirected to your 'user token'" @status.say_yellow :please, "copy/paste the token here:" end print "\t> " end