Module: LaunchDarkly::Impl::Util Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0

Class Method Summary collapse

Class Method Details

.add_payload_filter_key(uri, config) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • uri (String)
  • config (Config)

Returns:

  • (String)

Since:

  • 5.5.0



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ldclient-rb/impl/util.rb', line 107

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

.application_header_value(application) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate an HTTP Header value containing the application meta information (@see #application).

Returns:

  • (String)

Since:

  • 5.5.0



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ldclient-rb/impl/util.rb', line 36

def self.application_header_value(application)
  parts = []
  unless  application[:id].empty?
    parts << "application-id/#{application[:id]}"
  end

  unless  application[:version].empty?
    parts << "application-version/#{application[:version]}"
  end

  parts.join(" ")
end

.bool?(aObject) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 5.5.0



7
8
9
# File 'lib/ldclient-rb/impl/util.rb', line 7

def self.bool?(aObject)
   [true,false].include? aObject
end

.current_time_millisObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



11
12
13
# File 'lib/ldclient-rb/impl/util.rb', line 11

def self.current_time_millis
  (Time.now.to_f * 1000).to_i
end

.default_http_headers(sdk_key, config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ldclient-rb/impl/util.rb', line 15

def self.default_http_headers(sdk_key, config)
  ret = { "Authorization" => sdk_key, "User-Agent" => "RubyClient/" + LaunchDarkly::VERSION }

  ret["X-LaunchDarkly-Instance-Id"] = config.instance_id unless config.instance_id.nil?

  if config.wrapper_name
    ret["X-LaunchDarkly-Wrapper"] = config.wrapper_name +
      (config.wrapper_version ? "/" + config.wrapper_version : "")
  end

  app_value = application_header_value config.application
  ret["X-LaunchDarkly-Tags"] = app_value unless app_value.nil? || app_value.empty?

  ret
end

.http_error_message(status, context, recoverable_message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



156
157
158
159
160
# File 'lib/ldclient-rb/impl/util.rb', line 156

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

.http_error_recoverable?(status) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 5.5.0



148
149
150
151
152
153
154
# File 'lib/ldclient-rb/impl/util.rb', line 148

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



143
144
145
146
# File 'lib/ldclient-rb/impl/util.rb', line 143

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ldclient-rb/impl/util.rb', line 121

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

.validate_application_info(app, logger) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • app (Hash)
  • logger (Logger)

Returns:

  • (Hash)

Since:

  • 5.5.0



78
79
80
81
82
83
# File 'lib/ldclient-rb/impl/util.rb', line 78

def self.validate_application_info(app, logger)
  {
    id: validate_application_value(app[:id], :id, logger),
    version: validate_application_value(app[:version], :version, logger),
  }
end

.validate_application_value(value, name, logger) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (String)
  • name (Symbol)
  • logger (Logger)

Returns:

  • (String)

Since:

  • 5.5.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ldclient-rb/impl/util.rb', line 55

def self.validate_application_value(value, name, logger)
  value = value.to_s

  return "" if value.empty?

  if value.length > 64
    logger.warn { "Value of application[#{name}] was longer than 64 characters and was discarded" }
    return ""
  end

  if /[^a-zA-Z0-9._-]/.match?(value)
    logger.warn { "Value of application[#{name}] contained invalid characters and was discarded" }
    return ""
  end

  value
end

.validate_payload_filter_key(value, logger) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (String, nil)
  • logger (Logger)

Returns:

  • (String, nil)

Since:

  • 5.5.0



90
91
92
93
94
95
96
97
98
# File 'lib/ldclient-rb/impl/util.rb', line 90

def self.validate_payload_filter_key(value, logger)
  return nil if value.nil?
  return value if value.is_a?(String) && /^[a-zA-Z0-9][._\-a-zA-Z0-9]*$/.match?(value)

  logger.warn {
    "Invalid payload filter configured, full environment will be fetched. Ensure the filter key is not empty and was copied correctly from LaunchDarkly settings."
  }
  nil
end