class InfluxDB::Config

InfluxDB client configuration

Constants

ATTR_ACCESSOR
ATTR_READER
AUTH_METHODS

Valid values for the “auth_method” option.

OPTIONS_FROM_PARAMS

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity

Public Class Methods

new(url: nil, **opts) click to toggle source

Creates a new instance. See `DEFAULT_CONFIG_OPTIONS` for available config options and their default values.

If you provide a “url” option, either as String (hint: ENV) or as URI instance, you can override the defaults. The precedence for a config value is as follows (first found wins):

  • values given in the options hash

  • values found in URL (if given)

  • default values

# File lib/influxdb/config.rb, line 75
def initialize(url: nil, **opts)
  opts = opts_from_url(url).merge(opts) if url

  DEFAULT_CONFIG_OPTIONS.each do |name, value|
    set_ivar! name, opts.fetch(name, value)
  end

  configure_hosts! opts[:hosts] || opts[:host] || "localhost".freeze
end

Public Instance Methods

async?() click to toggle source
# File lib/influxdb/config.rb, line 89
def async?
  async != false
end
hosts() click to toggle source
# File lib/influxdb/config.rb, line 99
def hosts
  Array.new(@hosts_queue.length) do
    host = @hosts_queue.pop
    @hosts_queue.push(host)
    host
  end
end
next_host() click to toggle source
# File lib/influxdb/config.rb, line 93
def next_host
  host = @hosts_queue.pop
  @hosts_queue.push(host)
  host
end
udp?() click to toggle source
# File lib/influxdb/config.rb, line 85
def udp?
  udp != false
end

Private Instance Methods

coerce(name, value) click to toggle source
# File lib/influxdb/config.rb, line 178
def coerce(name, value)
  case name
  when :open_timeout, :read_timeout, :max_delay, :retry, :chunk_size
    value.to_i
  when :initial_delay
    value.to_f
  when :verify_ssl, :denormalize, :async, :discard_write_errors
    %w[true 1 yes on].include?(value.downcase)
  else
    value
  end
end
configure_hosts!(hosts) click to toggle source

load the hosts into a Queue for thread safety

# File lib/influxdb/config.rb, line 129
def configure_hosts!(hosts)
  @hosts_queue = Queue.new
  Array(hosts).each do |host|
    @hosts_queue.push(host)
  end
end
normalize_retry_option(value) click to toggle source
# File lib/influxdb/config.rb, line 120
def normalize_retry_option(value)
  case value
  when Integer   then value
  when true, nil then -1
  when false     then 0
  end
end
opts_from_non_params(url) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity

# File lib/influxdb/config.rb, line 147
def opts_from_non_params(url)
  {}.tap do |o|
    o[:host]     = url.host                                    if url.host
    o[:port]     = url.port                                    if url.port
    o[:username] = URI.decode_www_form_component(url.user)     if url.user
    o[:password] = URI.decode_www_form_component(url.password) if url.password
    o[:database] = url.path[1..-1]                             if url.path.length > 1
    o[:use_ssl]  = url.scheme == "https".freeze

    o[:udp] = { host: o[:host], port: o[:port] } if url.scheme == "udp"
  end
end
opts_from_params(query) click to toggle source
# File lib/influxdb/config.rb, line 168
def opts_from_params(query)
  params = CGI.parse(query || "").tap { |h| h.default = [] }

  OPTIONS_FROM_PARAMS.each_with_object({}) do |k, opts|
    next unless params[k.to_s].size == 1

    opts[k] = coerce(k, params[k.to_s].first)
  end
end
opts_from_url(url) click to toggle source

merges URI options into opts

# File lib/influxdb/config.rb, line 137
def opts_from_url(url)
  url = URI.parse(url) unless url.is_a?(URI)
  opts_from_non_params(url).merge opts_from_params(url.query)
rescue URI::InvalidURIError
  {}
end
set_ivar!(name, value) click to toggle source
# File lib/influxdb/config.rb, line 109
def set_ivar!(name, value)
  case name
  when :auth_method
    value = "params".freeze unless AUTH_METHODS.include?(value)
  when :retry
    value = normalize_retry_option(value)
  end

  instance_variable_set "@#{name}", value
end