Class: OptOut::Adapters::RedisAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/opt_out/adapters/redis_adapter.rb

Overview

Adapter that persists data in a Redis set.

Options

:redis - redis connection object OR
:url   - redis connection url OR
:host
:port
:password
:key_format - format string for redis key. list_id is interpolated into this option.
              Default is "opt_out:%s"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#initialize, #subscribed?

Constructor Details

This class inherits a constructor from OptOut::Adapters::AbstractAdapter

Instance Attribute Details

#key_formatObject


37
38
39
# File 'lib/opt_out/adapters/redis_adapter.rb', line 37

def key_format
  @key_format || @options[:key_format] || "opt_out:%s"
end

Instance Method Details

#redisObject

Private: returns redis client for this adapter


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opt_out/adapters/redis_adapter.rb', line 43

def redis
  return @redis if @redis

  @redis = if @options[:redis]
    @options[:redis]
  elsif @options[:url]
    uri = URI.parse(@options[:url])
    Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
  else
    Redis.new(:host => @options[:host], :port => @options[:port], :password => @options[:password])
  end
end

#resetObject


33
34
35
# File 'lib/opt_out/adapters/redis_adapter.rb', line 33

def reset
  redis.flushdb
end

#subscribe(list_id, user_id) ⇒ Object


17
18
19
# File 'lib/opt_out/adapters/redis_adapter.rb', line 17

def subscribe(list_id, user_id)
  redis.srem(key(list_id), user_id) and return
end

#unsubscribe(list_id, user_id) ⇒ Object


21
22
23
# File 'lib/opt_out/adapters/redis_adapter.rb', line 21

def unsubscribe(list_id, user_id)
  redis.sadd(key(list_id), user_id) and return
end

#unsubscribed?(list_id, user_id) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/opt_out/adapters/redis_adapter.rb', line 25

def unsubscribed?(list_id, user_id)
  redis.sismember(key(list_id), user_id)
end

#unsubscribers(list_id) ⇒ Object


29
30
31
# File 'lib/opt_out/adapters/redis_adapter.rb', line 29

def unsubscribers(list_id)
  redis.smembers(key(list_id))
end