Sha256: 3f9b3c8fe07e23e91fd3da3a838adbd52cd89bb44184f085514dad9b00ff317c

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'connection_pool'
require 'em-synchrony/connection_pool'

module Faye
  module Publisher
    class << self
      def instance
        @default_instance ||= RedisPublisher.new(configuration.redis)
      end

      def configure
        yield(configuration)
      end

      def configured?
        !@config.nil?
      end

      private
      def configuration
        @config ||= OpenStruct.new(redis: {url: 'redis://localhost:6379', size: 10, namespace: 'faye'})
      end
    end

    class RedisPublisher
      def initialize(config)
        case config
          when
          ::ConnectionPool,
              EventMachine::Synchrony::ConnectionPool
            @redis = config
          else
            @config = config
        end
      end

      def publish(channel, payload)
        redis.rpush(
            'faye.messages',
            MultiJson.dump(
                channel: channel,
                payload: payload
            )
        )
      end

      private
      def redis
        @redis ||= ConnectionPool::Wrapper.new(size: @config[:size] || 10) do
          connection = Redis.new(url: @config[:url])
          connection = Redis::Namespace.new(@config[:namespace], redis: connection) if @config[:namespace]
          connection
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-gen-0.0.6 template/lib/faye/publisher.rb