Class: Infrataster::Contexts::RedisContext

Inherits:
BaseContext
  • Object
show all
Defined in:
lib/infrataster/contexts/redis_context.rb

Instance Method Summary collapse

Instance Method Details

#resultObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/infrataster/contexts/redis_context.rb', line 7

def result 
  options = {host: 'localhost', port: 6379, db: 15}
  if server.options[:redis]
    options = options.merge(server.options[:redis])
  end

  # Query  parse 
  query, arguments = resource.query.split(' ', 2) 
  # For set and get query parse and get arguments
  if query == "set" or query == "get"
    arguments = arguments.scan(/'[^']*'/).map{|n| eval n}  
  end 

  # Initialize the redis obj
  if options.has_key?(:url)
    # In case of URL pass
    resource.redis = Redis.new(:url => "#{options[:url]}")
  elsif options.has_key?(:password)
    # In case of password protected redis
    resource.redis = Redis.new(:host=>"#{options[:host]}",
                               :port=>"#{options[:port]}",
                               :db=>"#{options[:db]}",
                               :password=> "#{options[:password]}")
  else
    # default connection options
    resource.redis = Redis.new(:host=>"#{options[:host]}",
                               :port=>"#{options[:port]}",
                               :db=>"#{options[:db]}")
  end

  if resource.redis.respond_to?(query)
    # Run query 
    resource.redis.method(query).call(*arguments)
  end  
end