Class: RedisClient::URLConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/url_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ URLConfig

Returns a new instance of URLConfig.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/redis_client/url_config.rb', line 9

def initialize(url)
  @url = url
  @uri = URI(url)
  @unix = false
  @ssl = false
  case uri.scheme
  when "redis"
    # expected
  when "rediss"
    @ssl = true
  when "unix", nil
    @unix = true
  else
    raise ArgumentError, "Unknown URL scheme: #{url.inspect}"
  end
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/redis_client/url_config.rb', line 7

def uri
  @uri
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/redis_client/url_config.rb', line 7

def url
  @url
end

Instance Method Details

#dbObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redis_client/url_config.rb', line 30

def db
  unless @unix
    db_path = uri.path&.delete_prefix("/")
    return Integer(db_path) if db_path && !db_path.empty?
  end

  unless uri.query.nil? || uri.query.empty?
    _, db_query = URI.decode_www_form(uri.query).find do |key, _|
      key == "db"
    end
    return Integer(db_query) if db_query && !db_query.empty?
  end
end

#hostObject



56
57
58
59
60
# File 'lib/redis_client/url_config.rb', line 56

def host
  return if uri.host.nil? || uri.host.empty?

  uri.host.sub(/\A\[(.*)\]\z/, '\1')
end

#passwordObject



48
49
50
51
52
53
54
# File 'lib/redis_client/url_config.rb', line 48

def password
  if uri.user && !uri.password
    URI.decode_www_form_component(uri.user)
  elsif uri.user && uri.password
    URI.decode_www_form_component(uri.password)
  end
end

#pathObject



62
63
64
65
66
# File 'lib/redis_client/url_config.rb', line 62

def path
  if @unix
    File.join(*[uri.host, uri.path].compact)
  end
end

#portObject



68
69
70
71
72
# File 'lib/redis_client/url_config.rb', line 68

def port
  return unless uri.port

  Integer(uri.port)
end

#ssl?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/redis_client/url_config.rb', line 26

def ssl?
  @ssl
end

#usernameObject



44
45
46
# File 'lib/redis_client/url_config.rb', line 44

def username
  uri.user if uri.password && !uri.user.empty?
end