Sha256: e78e80fc65ede56150607c50933c0cc3e08af21473d589936372d369256c8e2b

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

require 'logstasher/device'
require 'redis'

module LogStasher
  module Device
    class Redis
      include ::LogStasher::Device

      attr_reader :options, :redis

      def initialize(options = {})
        @options = default_options.merge(stringify_keys(options))

        validate_options
        configure_redis
      end

      def data_type
        options['data_type']
      end

      def key
        options['key']
      end

      def redis_options
        unless @redis_options
          default_keys = default_options.keys
          @redis_options = options.select { |k, v| !default_keys.include?(k) }
        end

        @redis_options
      end

      def write(log)
        case data_type
        when 'list'
          redis.rpush(key, log)
        when 'channel'
          redis.publish(key, log)
        else
          fail "Unknown data type #{data_type}"
        end
      end

      def close
        redis.quit
      end

      private

      def configure_redis
        @redis = ::Redis.new(redis_options)
      end

      def default_options
        { 'key' => 'logstash', 'data_type' => 'list' }
      end

      def validate_options
        unless ['list', 'channel'].include?(options['data_type'])
          fail RuntimeError, 'Expected data_type to be either "list" or "channel"'
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
md-logstasher-1.8.0 lib/logstasher/device/redis.rb
md-logstasher-1.7.0 lib/logstasher/device/redis.rb
md-logstasher-1.6.0 lib/logstasher/device/redis.rb
md-logstasher-1.5.0 lib/logstasher/device/redis.rb
md-logstasher-1.4.1 lib/logstasher/device/redis.rb
md-logstasher-1.4.0 lib/logstasher/device/redis.rb
md-logstasher-1.3.0 lib/logstasher/device/redis.rb
md-logstasher-1.2.0 lib/logstasher/device/redis.rb
md-logstasher-1.1.0 lib/logstasher/device/redis.rb