Module: Prisma

Defined in:
lib/prisma.rb,
lib/prisma/group.rb,
lib/prisma/filter.rb,
lib/prisma/server.rb,
lib/prisma/railtie.rb,
lib/prisma/null_logger.rb

Overview

Used for configuration, typically in a Rails initializer.

Defined Under Namespace

Modules: Filter Classes: Group, NullLogger, Railtie, Server

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.group(name, options = {}, &block) ⇒ Object

Configures a group. The instance of the current ActionController is being passed as an argument into the block. As an example, tracking daily active users could be as simple as:

Prisma.setup do |config|
  config.group :logged_in { |controller| controller.current_user.id }
end

Parameters:

  • name (Symbol/String)

    for identifying the group, it is used as part of the redis key.

  • options (Hash) (defaults to: {})

    where :type is either :counter (default) or :bitmap, and where :description is an optional description (used in the admin UI)

  • block (Block)

    returning a String (or a meaningful .to_s output) which is used for identifying a counter inside a group, Prisma doesn’t count a request if block returns nil or false



65
66
67
# File 'lib/prisma.rb', line 65

def self.group(name, options={}, &block)
  @@groups[name] = Group.new(options.merge(name: name, block: block))
end

.redisRedis::Namespace

Returns a default or configured Redis instance wrapped in a Redis::Namespace

Returns:

  • (Redis::Namespace)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prisma.rb', line 71

def self.redis
  @@namespaced_redis ||= lambda do
    case @@redis
    when String
      if @@redis =~ /redis\:\/\//
        redis = Redis.connect(url: @@redis, thread_safe: true)
      else
        host, port, db = @@redis.split(':')
        redis = Redis.new(host: host, port: port, thread_safe: true, db: db)
      end
      Redis::Namespace.new(redis_namespace, redis: redis)
    else
      Redis::Namespace.new(redis_namespace, redis: @@redis)
    end
  end.call
end

.setup {|_self| ... } ⇒ Object

Configure prisma. Example usage:

Prisma.setup do |config|
  config.group :active_api_clients { |controller| controller.current_client.id }
  config.redis = Redis.new(db: 1)
  config.redis_namespace = 'stats'
  config.redis_expiration_duration = 2.days
  config.logger = Rails.logger
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Prisma)

    the object that the method was called on



51
52
53
54
# File 'lib/prisma.rb', line 51

def self.setup
  yield self
  store_configuration
end

Instance Method Details

#loggerObject

Logger instance responding to debug, warn and error.



35
# File 'lib/prisma.rb', line 35

mattr_accessor :logger

#redisObject

Set your own Redis instance, it will be wrapped into a Redis::Namespace object with the configured namespace. Useful for when Redis is not available on the standard IP and port. Allows:

  • hostname:port

  • hostname:port:db

  • redis://hostname:port/db

  • Redis instance



23
# File 'lib/prisma.rb', line 23

mattr_accessor :redis

#redis_expiration_durationObject

Duration in seconds for expiring redis keys (easy to use with Rails duration helpers 1.day)



41
# File 'lib/prisma.rb', line 41

mattr_accessor :redis_expiration_duration

#redis_namespaceObject

String for redis namespace, defaults to prisma



29
# File 'lib/prisma.rb', line 29

mattr_accessor :redis_namespace