module UserControl

  def self.logged_in?
    Cfg.key?(:default_id)
  end

  def self.log_in
    loop do
      permalink = HighLine.new.ask('Please enter your permalink (last word in your profile url): ')
      url = "https://soundcloud.com/#{permalink}"
      if user_exist?(permalink)
        user = Client.get('/resolve', url: url)
        Cfg[:default_id] = user.id
        Cfg[:permalink] = permalink
        puts Paint['Successfully logged in!', :green]
        break
      else
        puts Paint['Invalid permalink. Please enter correct permalink', :red]
      end
    end
  end

  def self.temp_user=(permalink)
    if user_exist?(permalink)
      user = Client.get('/resolve', url: "https://soundcloud.com/#{permalink}")
      @temp_user = User.new(user.id)
    else
      puts Paint['Invalid permalink. Please enter correct permalink', :red]
      exit
    end
  end

  def self.user
    @temp_user || default_user
  end

  module_function

  def default_user
    if UserControl.logged_in?
      User.new(Cfg[:default_id])
    else
      puts Paint["You didn't logged in", :red]
      puts "Login from #{Paint['nehm configure', :yellow]} or use #{Paint['[from PERMALINK]', :yellow]} option"
      exit
    end
  end

  def user_exist?(permalink)
    Client.get('/resolve', url: "https://soundcloud.com/#{permalink}")

    rescue SoundCloud::ResponseError => e
      if e.message =~ /404/
        false
      else
        raise e
      end
    else
      true
  end
end

module UserControl
  def self.user
    @temp_user || default_user
  end

  def self.logged_in?
    Cfg.key?(:default_id)
  end

  def self.log_in
    loop do
      permalink = HighLine.new.ask('Please enter your permalink (last word in your profile url): ')
      user = get_user(permalink)
      if user
        Cfg[:default_id] = user.id
        Cfg[:permalink] = permalink
        puts Paint['Successfully logged in!', :green]
        break
      else
        puts Paint['Invalid permalink. Please enter correct permalink', :red]
      end
    end
  end

  def self.temp_user=(permalink)
    user = get_user(permalink)
    if user
      @temp_user = User.new(user.id)
    else
      puts Paint['Invalid permalink. Please enter correct permalink', :red]
      exit
    end
  end

  module_function

  def default_user
    if UserControl.logged_in?
      User.new(Cfg[:default_id])
    else
      puts Paint["You didn't logged in", :red]
      puts "Login from #{Paint['nehm configure', :yellow]} or use #{Paint['[from PERMALINK]', :yellow]} option"
      exit
    end
  end

  def get_user(permalink)
    begin
      user = Client.get('/resolve', url: "https://soundcloud.com/#{permalink}")
    rescue SoundCloud::ResponseError => e
      if e.message =~ /404/
        user = nil
      else
        raise e
      end
    end
    user
  end
end

Improve performance for get user from SC
Improve performance for get user from SC
