Sha256: 866b9fe07871897ca4ac80ebe7be7414f922ef0cf4606c3ddb24bafd14982f7c

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Lita
  # A namespace to hold all subclasses of {Handler}.
  module Handlers
    # Provides information about the currently running robot.
    class Info < Handler
      route(/^info$/i, :chat, command: true, help: {
        "info" => t("help.info_value")
      })

      http.get "/lita/info", :web

      # Replies with the current version of Lita, the current version of Redis,
      # and Redis memory usage.
      # @param response [Lita::Response] The response object.
      # @return [void]
      # @since 3.0.0
      def chat(response)
        response.reply(
          %(Lita #{Lita::VERSION} - https://www.lita.io/),
          %(Redis #{redis_version} - Memory used: #{redis_memory_usage})
        )
      end

      # Returns JSON with basic information about the robot.
      # @param _request [Rack::Request] The HTTP request.
      # @param response [Rack::Response] The HTTP response.
      # @return [void]
      def web(_request, response)
        response.headers["Content-Type"] = "application/json"
        json = MultiJson.dump(
          adapter: robot.config.robot.adapter,
          lita_version: Lita::VERSION,
          redis_memory_usage: redis_memory_usage,
          redis_version: redis_version,
          robot_mention_name: robot.mention_name,
          robot_name: robot.name
        )
        response.write(json)
      end

      # A hash of information about Redis.
      def redis_info
        @redis_info ||= begin
          r = redis.redis
          r = r.redis while r.respond_to?(:redis)
          r.info
        end
      end

      # The current version of Redis.
      def redis_version
        redis_info["redis_version"]
      end

      # The amount of memory Redis is using.
      def redis_memory_usage
        redis_info["used_memory_human"]
      end
    end

    Lita.register_handler(Info)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lita-4.8.0 lib/lita/handlers/info.rb
lita-4.8.0.beta1 lib/lita/handlers/info.rb