Class: RedisClient::URLConfig

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

Constant Summary collapse

DEFAULT_SCHEMA =
"redis"
SSL_SCHEMA =
"rediss"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ URLConfig

Returns a new instance of URLConfig.



12
13
14
15
16
17
18
# File 'lib/redis_client/url_config.rb', line 12

def initialize(url)
  @url = url
  @uri = URI(url)
  unless uri.scheme == DEFAULT_SCHEMA || uri.scheme == SSL_SCHEMA
    raise ArgumentError, "Invalid URL: #{url.inspect}"
  end
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



10
11
12
# File 'lib/redis_client/url_config.rb', line 10

def uri
  @uri
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/redis_client/url_config.rb', line 10

def url
  @url
end

Instance Method Details

#dbObject



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

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

#hostObject



41
42
43
44
45
# File 'lib/redis_client/url_config.rb', line 41

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

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

#passwordObject



33
34
35
36
37
38
39
# File 'lib/redis_client/url_config.rb', line 33

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

#portObject



47
48
49
50
51
# File 'lib/redis_client/url_config.rb', line 47

def port
  return unless uri.port

  Integer(uri.port)
end

#ssl?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/redis_client/url_config.rb', line 20

def ssl?
  @uri.scheme == SSL_SCHEMA
end

#usernameObject



29
30
31
# File 'lib/redis_client/url_config.rb', line 29

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