Class: ConfigCat::CacheControlConfigFetcher

Inherits:
ConfigFetcher show all
Defined in:
lib/configcat/configfetcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, mode, base_url = nil, proxy_address = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil, data_governance = DataGovernance::GLOBAL) ⇒ CacheControlConfigFetcher

Returns a new instance of CacheControlConfigFetcher.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Method Details

#closeObject



129
130
131
132
133
# File 'lib/configcat/configfetcher.rb', line 129

def close()
  if @_http
    @_http = nil
  end
end

#get_configuration_json(retries = 0) ⇒ Object

Returns the FetchResponse object contains configuration json Dictionary



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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 [email protected].")
    return fetch_response
  end

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