class ConfigCat::CacheControlConfigFetcher

Public Class Methods

new(sdk_key, mode, base_url=nil, proxy_address=nil, proxy_port=nil, proxy_user=nil, proxy_pass=nil, data_governance=DataGovernance::GLOBAL) click to toggle source
# File lib/configcat/configfetcher.rb, line 44
def initialize(sdk_key, mode, base_url=nil, proxy_address=nil, proxy_port=nil, proxy_user=nil, proxy_pass=nil,
               data_governance=DataGovernance::GLOBAL)
  @_sdk_key = sdk_key
  @_proxy_address = proxy_address
  @_proxy_port = proxy_port
  @_proxy_user = proxy_user
  @_proxy_pass = proxy_pass
  @_etag = ""
  @_headers = {"User-Agent" => ((("ConfigCat-Ruby/") + mode) + ("-")) + VERSION, "X-ConfigCat-UserAgent" => ((("ConfigCat-Ruby/") + mode) + ("-")) + VERSION, "Content-Type" => "application/json"}
  if !base_url.equal?(nil)
    @_base_url_overridden = true
    @_base_url = base_url.chomp("/")
  else
    @_base_url_overridden = false
    if data_governance == DataGovernance::EU_ONLY
      @_base_url = BASE_URL_EU_ONLY
    else
      @_base_url = BASE_URL_GLOBAL
    end
  end
end

Public Instance Methods

close() click to toggle source
# File lib/configcat/configfetcher.rb, line 129
def close()
  if @_http
    @_http = nil
  end
end
get_configuration_json(retries=0) click to toggle source

Returns the FetchResponse object contains configuration json Dictionary

# File lib/configcat/configfetcher.rb, line 67
def get_configuration_json(retries=0)
  ConfigCat.logger.debug "Fetching configuration from ConfigCat"
  uri = URI.parse((((@_base_url + ("/")) + BASE_PATH) + @_sdk_key) + BASE_EXTENSION)
  headers = @_headers
  headers["If-None-Match"] = @_etag unless @_etag.empty?
  _create_http()
  request = Net::HTTP::Get.new(uri.request_uri, headers)
  response = @_http.request(request)
  etag = response["ETag"]
  @_etag = etag unless etag.nil? || etag.empty?
  ConfigCat.logger.debug "ConfigCat configuration json fetch response code:#{response.code} Cached:#{response['ETag']}"
  fetch_response = FetchResponse.new(response)

  # If there wasn't a config change, we return the response.
  if !fetch_response.is_fetched()
    return fetch_response
  end

  preferences = fetch_response.json().fetch(PREFERENCES, nil)
  if preferences === nil
    return fetch_response
  end

  base_url = preferences.fetch(BASE_URL, nil)

  # If the base_url is the same as the last called one, just return the response.
  if base_url.equal?(nil) || @_base_url == base_url
    return fetch_response
  end

  redirect = preferences.fetch(REDIRECT, nil)
  # If the base_url is overridden, and the redirect parameter is not 2 (force),
  # the SDK should not redirect the calls and it just have to return the response.
  if @_base_url_overridden && redirect != RedirectMode::FORCE_REDIRECT
    return fetch_response
  end

  # The next call should use the base_url provided in the config json
  @_base_url = base_url

  # If the redirect property == 0 (redirect not needed), return the response
  if redirect == RedirectMode::NO_REDIRECT
    # Return the response
    return fetch_response
  end

  # Try to download again with the new url

  if redirect == RedirectMode::SHOULD_REDIRECT
    ConfigCat.logger.warn("Your data_governance parameter at ConfigCatClient initialization is not in sync with your preferences on the ConfigCat Dashboard: https://app.configcat.com/organization/data-governance. Only Organization Admins can set this preference.")
  end

  # To prevent loops we check if we retried at least 3 times with the new base_url
  if retries >= 2
    ConfigCat.logger.error("Redirect loop during config.json fetch. Please contact support@configcat.com.")
    return fetch_response
  end

  # Retry the config download with the new base_url
  return get_configuration_json(retries + 1)
end

Private Instance Methods

_create_http() click to toggle source
# File lib/configcat/configfetcher.rb, line 137
def _create_http()
  uri = URI.parse(@_base_url)
  use_ssl = true if uri.scheme == 'https'
  if @_http.equal?(nil) || @_http.address != uri.host || @_http.port != uri.port || @_http.use_ssl? != use_ssl
    close()
    @_http = Net::HTTP.new(uri.host, uri.port, @_proxy_address, @_proxy_port, @_proxy_user, @_proxy_pass)
    @_http.use_ssl = use_ssl
    @_http.open_timeout = 10 # in seconds
    @_http.read_timeout = 30 # in seconds
  end
end