Class: Fleck::Configuration
- Inherits:
-
Object
- Object
- Fleck::Configuration
- Defined in:
- lib/fleck/configuration.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
Returns the value of attribute app_name.
-
#default_host ⇒ Object
Returns the value of attribute default_host.
-
#default_pass ⇒ Object
Returns the value of attribute default_pass.
-
#default_port ⇒ Object
Returns the value of attribute default_port.
-
#default_queue ⇒ Object
Returns the value of attribute default_queue.
-
#default_user ⇒ Object
Returns the value of attribute default_user.
-
#default_vhost ⇒ Object
Returns the value of attribute default_vhost.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#hosts ⇒ Object
Returns the value of attribute hosts.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#loglevel ⇒ Object
Returns the value of attribute loglevel.
-
#progname ⇒ Object
Returns the value of attribute progname.
Instance Method Summary collapse
- #add_host(data) ⇒ Object
- #default_options ⇒ Object
- #formatter ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #logger ⇒ Object
- #logger=(new_logger) ⇒ Object
- #reset_logger ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fleck/configuration.rb', line 9 def initialize @logfile = STDOUT @loglevel = ::Logger::INFO @progname = "Fleck" @app_name = $0 @default_host = "127.0.0.1" @default_port = 5672 @default_user = nil @default_pass = nil @default_vhost = "/" @default_queue = "default" @filters = ["password", "secret", "token"] @hosts = [] @credentials = {} end |
Instance Attribute Details
#app_name ⇒ Object
Returns the value of attribute app_name.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def app_name @app_name end |
#default_host ⇒ Object
Returns the value of attribute default_host.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def default_host @default_host end |
#default_pass ⇒ Object
Returns the value of attribute default_pass.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def default_pass @default_pass end |
#default_port ⇒ Object
Returns the value of attribute default_port.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def default_port @default_port end |
#default_queue ⇒ Object
Returns the value of attribute default_queue.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def default_queue @default_queue end |
#default_user ⇒ Object
Returns the value of attribute default_user.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def default_user @default_user end |
#default_vhost ⇒ Object
Returns the value of attribute default_vhost.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def default_vhost @default_vhost end |
#filters ⇒ Object
Returns the value of attribute filters.
6 7 8 |
# File 'lib/fleck/configuration.rb', line 6 def filters @filters end |
#hosts ⇒ Object
Returns the value of attribute hosts.
5 6 7 |
# File 'lib/fleck/configuration.rb', line 5 def hosts @hosts end |
#logfile ⇒ Object
Returns the value of attribute logfile.
5 6 7 |
# File 'lib/fleck/configuration.rb', line 5 def logfile @logfile end |
#loglevel ⇒ Object
Returns the value of attribute loglevel.
5 6 7 |
# File 'lib/fleck/configuration.rb', line 5 def loglevel @loglevel end |
#progname ⇒ Object
Returns the value of attribute progname.
5 6 7 |
# File 'lib/fleck/configuration.rb', line 5 def progname @progname end |
Instance Method Details
#add_host(data) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fleck/configuration.rb', line 32 def add_host(data) if data.is_a?(String) host, port = data.split(":") port = port ? port.to_i : 5672 @hosts << Fleck::HostRating.new(host: host, port: port) @credentials["#{host}:#{port}"] ||= { user: @default_user, pass: @default_pass } elsif data.is_a?(Hash) data = data.to_hash_with_indifferent_access host = data[:host] || @default_host port = data[:port] || @default_port @hosts << Fleck::HostRating.new(host: data[:host] || @default_host, port: data[:port] || @default_port) @credentials["#{host}:#{port}"] ||= { user: data[:user] || @default_user, pass: data[:pass] || @default_pass } else raise ArgumentError.new("Invalid host type #{data.inspect}: String or Hash expected") end end |
#default_options ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fleck/configuration.rb', line 49 def best = @hosts.sort.first opts = {} host = best ? best.host : @default_host port = best ? best.port : @default_port credentials = @credentials["#{host}:#{port}"] || {user: @default_user, pass: @default_pass} opts[:host] = host opts[:port] = port opts[:user] = credentials[:user] || @default_user opts[:pass] = credentials[:pass] || @default_pass opts[:vhost] = @default_vhost opts[:queue] = @default_queue return opts end |
#formatter ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fleck/configuration.rb', line 120 def formatter return @formatter if @formatter @formatter = proc do |severity, datetime, progname, msg| color = :blue case severity when 'DEBUG' color = "#512DA8" when 'INFO' color = "#33691E" when 'WARN' color = "#E65100" when 'ERROR', 'FATAL' color = "#B71C1C" else color = "#00BCD4" end "[#{datetime.strftime('%F %T.%L')}]".color(:cyan) + "(#{$$})".color(:blue) + "|#{severity}|".color(color) + (progname ? "<#{progname}>".color(:yellow) : "") + " #{msg}\n" end return @formatter end |
#logger ⇒ Object
90 91 92 |
# File 'lib/fleck/configuration.rb', line 90 def logger return @logger || reset_logger end |
#logger=(new_logger) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fleck/configuration.rb', line 94 def logger=(new_logger) if new_logger.nil? @logger.close if @logger @logger = ::Logger.new(nil) else @logger.close if @logger @logger = new_logger.clone @logger.formatter = formatter @logger.progname = @progname @logger.level = @loglevel end return @logger end |
#reset_logger ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fleck/configuration.rb', line 109 def reset_logger new_logger = ::Logger.new(@logfile) new_logger.formatter = formatter new_logger.progname = @progname new_logger.level = @loglevel @logger.close if @logger @logger = new_logger return @logger end |