Class: URIConfig::Config

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

Direct Known Subclasses

ActionMailerConfig

Constant Summary collapse

FETCH_ERROR =
(RUBY_VERSION < "1.9" ? ::IndexError : ::KeyError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Config

Returns a new instance of Config.



8
9
10
# File 'lib/uri_config/config.rb', line 8

def initialize(url)
  @url = url
end

Class Method Details

.config(*keys) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/uri_config/config.rb', line 81

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

.configure_from(env_var, &block) ⇒ Object



22
23
24
25
26
# File 'lib/uri_config/config.rb', line 22

def self.configure_from(env_var, &block)
  configure_from!(env_var, &block)
rescue FETCH_ERROR
  nil
end

.configure_from!(env_var) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/uri_config/config.rb', line 28

def self.configure_from!(env_var)
  config = new ENV.fetch(env_var)

  if block_given?
    yield config
  else
    config
  end
end

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



12
13
14
# File 'lib/uri_config/config.rb', line 12

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

.parameter(method, query_parameter = method) ⇒ Object



16
17
18
19
20
# File 'lib/uri_config/config.rb', line 16

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



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

def self.values_from(env_var, *params)
  config = configure_from!(env_var)

  params.map { |param| config.send(param) }
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
47
48
# File 'lib/uri_config/config.rb', line 44

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

  url == other.url
end

#base_uriObject



74
75
76
77
78
79
# File 'lib/uri_config/config.rb', line 74

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

#hostObject



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

def host
  uri.host
end

#passwordObject



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

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

#pathObject



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

def path
  uri.path
end

#portObject



62
63
64
# File 'lib/uri_config/config.rb', line 62

def port
  uri.port
end

#schemeObject



70
71
72
# File 'lib/uri_config/config.rb', line 70

def scheme
  uri.scheme
end

#usernameObject



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

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