Class: URIConfig::Config
- Inherits:
-
Object
- Object
- URIConfig::Config
- Defined in:
- lib/uri_config/config.rb
Direct Known Subclasses
Constant Summary collapse
- FETCH_ERROR =
(RUBY_VERSION < "1.9" ? ::IndexError : ::KeyError)
Class Method Summary collapse
- .config(*keys) ⇒ Object
- .configure_from(env_var, &block) ⇒ Object
- .configure_from!(env_var) ⇒ Object
- .map(key, options = {}) ⇒ Object
- .parameter(method, query_parameter = method) ⇒ Object
- .values_from(env_var, *params) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #base_uri ⇒ Object
- #host ⇒ Object
-
#initialize(url) ⇒ Config
constructor
A new instance of Config.
- #password ⇒ Object
- #path ⇒ Object
- #port ⇒ Object
- #scheme ⇒ Object
- #username ⇒ Object
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, = {}) alias_method key, .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_uri ⇒ Object
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 |
#host ⇒ Object
58 59 60 |
# File 'lib/uri_config/config.rb', line 58 def host uri.host end |
#password ⇒ Object
54 55 56 |
# File 'lib/uri_config/config.rb', line 54 def password CGI.unescape uri.password if uri.password end |
#path ⇒ Object
66 67 68 |
# File 'lib/uri_config/config.rb', line 66 def path uri.path end |
#port ⇒ Object
62 63 64 |
# File 'lib/uri_config/config.rb', line 62 def port uri.port end |
#scheme ⇒ Object
70 71 72 |
# File 'lib/uri_config/config.rb', line 70 def scheme uri.scheme end |
#username ⇒ Object
50 51 52 |
# File 'lib/uri_config/config.rb', line 50 def username CGI.unescape uri.user if uri.user end |