Module: LaunchDarkly::Util

Defined in:
lib/ldclient-rb/util.rb

Class Method Summary collapse

Class Method Details

.add_payload_filter_key(uri, config) ⇒ String

Append the payload filter key query parameter to the provided URI.

Parameters:

  • uri (String)
  • config (Config)

Returns:

  • (String)


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ldclient-rb/util.rb', line 77

def self.add_payload_filter_key(uri, config)
  return uri if config.payload_filter_key.nil?

  begin
    parsed = URI.parse(uri)
    new_query_params = URI.decode_www_form(String(parsed.query)) << ["filter", config.payload_filter_key]
    parsed.query = URI.encode_www_form(new_query_params)
    parsed.to_s
  rescue URI::InvalidURIError
    config.logger.warn { "[LDClient] URI could not be parsed. No filtering will be applied." }
    uri
  end
end

.http_error_message(status, context, recoverable_message) ⇒ Object



126
127
128
129
130
# File 'lib/ldclient-rb/util.rb', line 126

def self.http_error_message(status, context, recoverable_message)
  desc = (status == 401 || status == 403) ? " (invalid SDK key)" : ""
  message = Util.http_error_recoverable?(status) ? recoverable_message : "giving up permanently"
  "HTTP error #{status}#{desc} for #{context} - #{message}"
end

.http_error_recoverable?(status) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
# File 'lib/ldclient-rb/util.rb', line 118

def self.http_error_recoverable?(status)
  if status >= 400 && status < 500
    status == 400 || status == 408 || status == 429
  else
    true
  end
end

.log_exception(logger, message, exc) ⇒ Object



113
114
115
116
# File 'lib/ldclient-rb/util.rb', line 113

def self.log_exception(logger, message, exc)
  logger.error { "[LDClient] #{message}: #{exc.inspect}" }
  logger.debug { "[LDClient] Exception trace: #{exc.backtrace}" }
end

.new_http_client(uri_s, config) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ldclient-rb/util.rb', line 91

def self.new_http_client(uri_s, config)
  http_client_options = {}
  if config.socket_factory
    http_client_options["socket_class"] = config.socket_factory
  end
  proxy = URI.parse(uri_s).find_proxy
  unless proxy.nil?
    http_client_options["proxy"] = {
      proxy_address: proxy.host,
      proxy_port: proxy.port,
      proxy_username: proxy.user,
      proxy_password: proxy.password,
    }
  end
  HTTP::Client.new(http_client_options)
    .timeout({
      read: config.read_timeout,
      connect: config.connect_timeout,
    })
    .persistent(uri_s)
end