Class: URIConfig::Config
- Inherits:
-
Object
- Object
- URIConfig::Config
- Defined in:
- lib/uri_config/config.rb
Direct Known Subclasses
Class Method Summary collapse
- .config(*keys) ⇒ Object
- .configure_from(env_var) {|new(value)| ... } ⇒ Object
- .configure_from!(env_var) {|new(value)| ... } ⇒ 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
- #username ⇒ Object
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
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
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, = {}) alias_method key, .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_uri ⇒ Object
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 |
#host ⇒ Object
51 52 53 |
# File 'lib/uri_config/config.rb', line 51 def host uri.host end |
#password ⇒ Object
47 48 49 |
# File 'lib/uri_config/config.rb', line 47 def password CGI.unescape uri.password if uri.password end |
#path ⇒ Object
59 60 61 |
# File 'lib/uri_config/config.rb', line 59 def path uri.path end |
#port ⇒ Object
55 56 57 |
# File 'lib/uri_config/config.rb', line 55 def port uri.port end |
#username ⇒ Object
43 44 45 |
# File 'lib/uri_config/config.rb', line 43 def username CGI.unescape uri.user if uri.user end |