Sha256: 97a4db16be344fa0d7ca54cb2e82e65f40ccac0b11e868ec29dccd0d314a76b7

Contents?: true

Size: 1.22 KB

Versions: 24

Compression:

Stored size: 1.22 KB

Contents

require 'redis'

module LogStasher
  module Device
    class Redis

      attr_reader :options, :redis

      def initialize(options = {})
        @options = default_options.merge(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 'Expected :data_type to be either "list" or "channel"'
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
logstasher-1.4.0 lib/logstasher/device/redis.rb
logstasher-1.3.0 lib/logstasher/device/redis.rb
logstasher-1.2.2 lib/logstasher/device/redis.rb
logstasher-1.2.1 lib/logstasher/device/redis.rb
logstasher-1.2.0 lib/logstasher/device/redis.rb
logstasher-1.1.1 lib/logstasher/device/redis.rb
logstasher-1.1.0 lib/logstasher/device/redis.rb
logstasher-1.0.1 lib/logstasher/device/redis.rb
logstasher-1.0.0 lib/logstasher/device/redis.rb
logstasher-0.9.0 lib/logstasher/device/redis.rb
logstasher-0.8.6 lib/logstasher/device/redis.rb
logstasher-0.8.5 lib/logstasher/device/redis.rb
logstasher-0.8.2 lib/logstasher/device/redis.rb
logstasher-0.8.0 lib/logstasher/device/redis.rb
logstasher-0.7.1 lib/logstasher/device/redis.rb
logstasher-0.7.0 lib/logstasher/device/redis.rb
logstasher-0.6.5 lib/logstasher/device/redis.rb
logstasher-0.6.2 lib/logstasher/device/redis.rb
logstasher-0.6.1 lib/logstasher/device/redis.rb
logstasher-0.6.0 lib/logstasher/device/redis.rb