Class: URI::Redis

Inherits:
Generic
  • Object
show all
Defined in:
lib/uri/redis.rb,
lib/uri/redis/version.rb

Overview

Redis URI

This is a subclass of URI::Generic and supports the following URI formats:

redis://host:port/dbindex

Examples:

uri = URI::Redis.build(host: "localhost", port: 6379, db: 2, key: "v1:arbitrary:key")
uri.to_s #=> "redis://localhost:6379/2/v1:arbitrary:key"

uri = URI::Redis.build(host: "localhost", port: 6379, db: 2)
uri.to_s #=> "redis://localhost:6379/2"

Constant Summary collapse

DEFAULT_PORT =
6379
DEFAULT_DB =
0
VERSION =
"1.3.0"
SUMMARY =
"A Ruby library for parsing, building and normalizing redis URLs"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(args) ⇒ Object



23
24
25
26
# File 'lib/uri/redis.rb', line 23

def self.build(args)
  tmp = Util.make_components_hash(self, args)
  super(tmp)
end

Instance Method Details

#confObject

Returns a hash suitable for sending to Redis.new. The hash is generated from the host, port, db and password from the URI as well as any query vars.

e.g.

uri = URI.parse "redis://127.0.0.1/6/?timeout=5"
uri.conf
  # => {:db=>6, :timeout=>"5", :host=>"127.0.0.1", :port=>6379}


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/uri/redis.rb', line 65

def conf
  hsh = {
    host: host,
    port: port,
    db: db,
    ssl: scheme == 'rediss'
  }.merge(parse_query(query))
  hsh[:password] = password if password
  hsh[:timeout] = hsh[:timeout].to_i if hsh.key?(:timeout)
  hsh
end

#dbObject



43
44
45
46
# File 'lib/uri/redis.rb', line 43

def db
  self.path ||= "/#{DEFAULT_DB}"
  (self.path.split("/")[1] || DEFAULT_DB).to_i
end

#db=(val) ⇒ Object



48
49
50
51
52
53
# File 'lib/uri/redis.rb', line 48

def db=(val)
  current_key = key
  self.path = "/#{val}"
  self.path << "/#{current_key}"
  self.path
end

#keyObject



32
33
34
35
36
37
# File 'lib/uri/redis.rb', line 32

def key
  return if path.nil?

  self.path ||= "/#{DEFAULT_DB}"
  (self.path.split("/")[2..] || []).join("/")
end

#key=(val) ⇒ Object



39
40
41
# File 'lib/uri/redis.rb', line 39

def key=(val)
  self.path = "/" << [db, val].join("/")
end

#request_uriObject



28
29
30
# File 'lib/uri/redis.rb', line 28

def request_uri
  path_query
end

#serveridObject



77
78
79
# File 'lib/uri/redis.rb', line 77

def serverid
  format('%s://%s:%s/%s', scheme, host, port, db)
end