Class: AhoyEmail::RedisSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy_email/redis_subscriber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis: nil, prefix: "ahoy_email") ⇒ RedisSubscriber

Returns a new instance of RedisSubscriber.



5
6
7
8
# File 'lib/ahoy_email/redis_subscriber.rb', line 5

def initialize(redis: nil, prefix: "ahoy_email")
  @redis = redis || Redis.new
  @prefix = prefix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



3
4
5
# File 'lib/ahoy_email/redis_subscriber.rb', line 3

def prefix
  @prefix
end

#redisObject (readonly)

Returns the value of attribute redis.



3
4
5
# File 'lib/ahoy_email/redis_subscriber.rb', line 3

def redis
  @redis
end

Instance Method Details

#campaign_exists?(campaign) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ahoy_email/redis_subscriber.rb', line 44

def campaign_exists?(campaign)
  redis.sismember(campaigns_key, campaign)
end

#campaignsObject



40
41
42
# File 'lib/ahoy_email/redis_subscriber.rb', line 40

def campaigns
  redis.smembers(campaigns_key)
end

#stats(campaign = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ahoy_email/redis_subscriber.rb', line 26

def stats(campaign = nil)
  if campaign
    # return nil instead of zeros if not a campaign
    if campaign_exists?(campaign)
      campaign_stats(campaign)
    end
  else
    campaigns.inject({}) do |memo, campaign|
      memo[campaign] = campaign_stats(campaign)
      memo
    end
  end
end

#track_click(event) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/ahoy_email/redis_subscriber.rb', line 18

def track_click(event)
  campaign_prefix = campaign_key(event[:campaign])
  pipelined do |pipeline|
    pipeline.incr("#{campaign_prefix}:clicks")
    pipeline.pfadd("#{campaign_prefix}:unique_clicks", event[:token])
  end
end

#track_send(event) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/ahoy_email/redis_subscriber.rb', line 10

def track_send(event)
  campaign_prefix = campaign_key(event[:campaign])
  pipelined do |pipeline|
    pipeline.incr("#{campaign_prefix}:sends")
    pipeline.sadd(campaigns_key, event[:campaign])
  end
end