Class: Turnstile::Adapter

Inherits:
Object
  • Object
show all
Includes:
Timeout
Defined in:
lib/turnstile/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdapter

Returns a new instance of Adapter.



9
10
11
12
13
14
15
# File 'lib/turnstile/adapter.rb', line 9

def initialize
  self.redis = config.redis.url ?
                 ::Redis.new(url: config.redis.url) :
                 ::Redis.new(host: config.redis.host,
                             port: config.redis.port,
                             db:   config.redis.db)
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



6
7
8
# File 'lib/turnstile/adapter.rb', line 6

def redis
  @redis
end

Instance Method Details

#add(uid, platform, ip) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/turnstile/adapter.rb', line 17

def add(uid, platform, ip)
  key = compose_key(uid, platform, ip)
  timeout(config.redis.timeout) do
    redis.setex(key, config.activity_interval, 1)
  end
rescue StandardError => e
  Turnstile::Logger.log "exception while writing to redis: #{e.inspect}"
end

#aggregateObject



37
38
39
40
41
# File 'lib/turnstile/adapter.rb', line 37

def aggregate
  redis.keys('t:*').inject({}) { |hash, key| increment_platform(hash, key) }.tap do |h|
    h['total'] = h.values.inject(&:+) || 0
  end
end

#fetchObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/turnstile/adapter.rb', line 26

def fetch
  redis.keys('t:*').map do |key|
    fields = key.split(':')
    {
      uid:      fields[1],
      platform: fields[2],
      ip:       fields[3],
    }
  end
end