Class: JsonCrudApi::AuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/json-crud-api/auth_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AuthClient

Returns a new instance of AuthClient.



8
9
10
11
12
# File 'lib/json-crud-api/auth_client.rb', line 8

def initialize(options)
  @redis = options[:redis_client]
  @session_ttl = options[:session_ttl]
  @prefix = options[:key_prefix]
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



6
7
8
# File 'lib/json-crud-api/auth_client.rb', line 6

def prefix
  @prefix
end

#redisObject

Returns the value of attribute redis.



6
7
8
# File 'lib/json-crud-api/auth_client.rb', line 6

def redis
  @redis
end

#session_ttlObject

Returns the value of attribute session_ttl.



6
7
8
# File 'lib/json-crud-api/auth_client.rb', line 6

def session_ttl
  @session_ttl
end

Instance Method Details

#delete(key) ⇒ Object



22
23
24
25
26
27
# File 'lib/json-crud-api/auth_client.rb', line 22

def delete(key)
  key = get_redis_key(key)
  return false unless @redis.exists(key)
  @redis.del(key)
  true
end

#get(key) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/json-crud-api/auth_client.rb', line 14

def get(key)
  key = get_redis_key(key)
  data = @redis.get(key)
  return nil if data.nil?
  touch(key)
  JSON.parse(data, :symbolize_names => true)
end

#get_redis_key(key) ⇒ Object



36
37
38
39
# File 'lib/json-crud-api/auth_client.rb', line 36

def get_redis_key(key)
  return key.to_s if @prefix.nil?
  @prefix.to_s+key.to_s
end

#touch(key) ⇒ Object



29
30
31
32
33
34
# File 'lib/json-crud-api/auth_client.rb', line 29

def touch(key)
  key = get_redis_key(key)
  return false unless @redis.exists(key)
  @redis.expire(key, @session_ttl)
  true
end