Class: Redis::Sentinel::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/sentinel/url.rb

Constant Summary collapse

DEFAULT_SENTINEL_PORT =
26379
DEFAULT_SENTINEL_SERVICE =
'mymaster'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ URL

Returns a new instance of URL.



16
17
18
# File 'lib/redis/sentinel/url.rb', line 16

def initialize(url)
  @uri = URI(url)
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



10
11
12
# File 'lib/redis/sentinel/url.rb', line 10

def uri
  @uri
end

Class Method Details

.parse(url) ⇒ Object



12
13
14
# File 'lib/redis/sentinel/url.rb', line 12

def self.parse(url)
  new(url).parse
end

Instance Method Details

#parseObject



37
38
39
40
41
42
43
44
45
# File 'lib/redis/sentinel/url.rb', line 37

def parse
  uri_options  = path_options.merge(query_options)
  service_name = uri_options.delete(:service_name) || DEFAULT_SENTINEL_SERVICE

  uri_options[:url]       = "redis://#{service_name}"
  uri_options[:sentinels] = [{ host: uri.host, port: uri.port || DEFAULT_SENTINEL_PORT }]

  uri_options
end

#path_optionsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redis/sentinel/url.rb', line 21

def path_options
  path_parts = uri.path.split('/')
  case path_parts.length
  when 3
    { service_name: path_parts[1], db: path_parts[2] }
  when 2
    { service_name: path_parts[1] }
  else
    {}
  end
end

#query_optionsObject



33
34
35
# File 'lib/redis/sentinel/url.rb', line 33

def query_options
  CGI::parse(uri.query.to_s).transform_keys(&:to_sym)
end