Class: URIConfig::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/uri_config/config.rb

Direct Known Subclasses

ActionMailerConfig

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Config

Returns a new instance of Config.



6
7
8
# File 'lib/uri_config/config.rb', line 6

def initialize(url)
  @url = url
end

Class Method Details

.config(*keys) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/uri_config/config.rb', line 70

def self.config(*keys)
  define_method :config do
    keys
      .each_with_object({}) { |key, hash| hash[key] = send(key) }
      .reject { |_, value| value.nil? }
  end
end

.configure_from(env_var) {|new(value)| ... } ⇒ Object

Yields:

  • (new(value))


20
21
22
23
24
# File 'lib/uri_config/config.rb', line 20

def self.configure_from(env_var)
  return unless (value = ENV[env_var])

  yield new(value)
end

.configure_from!(env_var) {|new(value)| ... } ⇒ Object

Yields:

  • (new(value))


26
27
28
29
30
# File 'lib/uri_config/config.rb', line 26

def self.configure_from!(env_var)
  return unless (value = ENV.fetch(env_var))

  yield new(value)
end

.map(key, options = {}) ⇒ Object



10
11
12
# File 'lib/uri_config/config.rb', line 10

def self.map(key, options = {})
  alias_method key, options.fetch(:from)
end

.parameter(method, query_parameter = method) ⇒ Object



14
15
16
17
18
# File 'lib/uri_config/config.rb', line 14

def self.parameter(method, query_parameter = method)
  define_method(method) do
    query[query_parameter.to_s].first
  end
end

.values_from(env_var, *params) ⇒ Object



32
33
34
35
# File 'lib/uri_config/config.rb', line 32

def self.values_from(env_var, *params)
  config = new(ENV.fetch(env_var))
  params.map { |param| config.public_send(param) }
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
40
41
# File 'lib/uri_config/config.rb', line 37

def ==(other)
  return false unless other.is_a?(self.class)

  url == other.url
end

#base_uriObject



63
64
65
66
67
68
# File 'lib/uri_config/config.rb', line 63

def base_uri
  uri.dup.tap do |uri|
    uri.user = nil
    uri.password = nil
  end.to_s
end

#hostObject



51
52
53
# File 'lib/uri_config/config.rb', line 51

def host
  uri.host
end

#passwordObject



47
48
49
# File 'lib/uri_config/config.rb', line 47

def password
  CGI.unescape uri.password if uri.password
end

#pathObject



59
60
61
# File 'lib/uri_config/config.rb', line 59

def path
  uri.path
end

#portObject



55
56
57
# File 'lib/uri_config/config.rb', line 55

def port
  uri.port
end

#usernameObject



43
44
45
# File 'lib/uri_config/config.rb', line 43

def username
  CGI.unescape uri.user if uri.user
end