def renew_authorization(wait = nil)
      wait ||= (state == :authorized) ? ((@expires_at - Time.now).to_i / RENEW_FACTOR) : 0

      if @renew_timer && wait == 0
        @renew_timer.cancel
        @renew_timer = nil
      end

      unless @renew_timer
        @renew_timer = EM_S::Timer.new(wait) do
          @renew_timer = nil
          previous_state = state
          begin
            get_authorized
            renew_authorization
          rescue Exceptions::ConnectivityFailure => e
            if wait > 0
              renew_authorization([(wait * RENEW_FACTOR), MAX_RENEW_TIME].min)
            else
              renew_authorization(MIN_RENEW_TIME)
            end
          rescue BalancedHttpClient::NotResponding => e
            if (expires_in = (@expires_at - Time.now).to_i) > MIN_RENEW_TIME
              renew_authorization(expires_in / RENEW_FACTOR)
            else
              self.state = :expired
              reconnect
            end
          rescue Exceptions::Unauthorized => e
            if previous_state == :unauthorized && wait > 0
              renew_authorization([(wait * RENEW_FACTOR), MAX_UNAUTHORIZED_RENEW_INTERVAL].min)
            else
              renew_authorization(UNAUTHORIZED_RENEW_INTERVAL)
            end
          rescue CommunicationModeSwitch => e
            ErrorTracker.log(self, "Failed authorization renewal", e, nil, :no_trace)
            self.state = :failed
          rescue Exception => e
            ErrorTracker.log(self, "Failed authorization renewal", e)
            self.state = :failed
          end
        end
      end
      true
    end
def renew_authorization(wait = nil)
      wait ||= (state == :authorized) ? ((@expires_at - Time.now).to_i / RENEW_FACTOR) : 0

      if @renew_timer && wait == 0
        @renew_timer.cancel
        @renew_timer = nil
      end

      unless @renew_timer
        @renew_timer = EM_S::Timer.new(wait) do
          @renew_timer = nil
          previous_state = state
          begin
            get_authorized
            renew_authorization
          rescue Exceptions::ConnectivityFailure => e
            if wait > 0
              renew_authorization([(wait * RENEW_FACTOR), MAX_RENEW_TIME].min)
            else
              renew_authorization(MIN_RENEW_TIME)
            end
          rescue BalancedHttpClient::NotResponding => e
            if (expires_in = (@expires_at - Time.now).to_i) > MIN_RENEW_TIME
              renew_authorization(expires_in / RENEW_FACTOR)
            else
              self.state = :expired
              reconnect
            end
          rescue Exceptions::Unauthorized => e
            if previous_state == :unauthorized && wait > 0
              renew_authorization([(wait * RENEW_FACTOR), MAX_UNAUTHORIZED_RENEW_INTERVAL].min)
            else
              renew_authorization(UNAUTHORIZED_RENEW_INTERVAL)
            end
          rescue CommunicationModeSwitch => e
            ErrorTracker.log(self, "Failed authorization renewal", e, nil, :no_trace)
            self.state = :failed
          rescue Exception => e
            ErrorTracker.log(self, "Failed authorization renewal", e)
            self.state = :failed
          end
        end
      end
      true
    end
Get authorized and then continuously renew authorization before it expires

 @param [Integer, NilClass] wait time before attempt to renew; defaults to
   have the expiry time if authorized, otherwise 0

 @return [TrueClass] always true