Class: ConfigCat::ConfigFetcher

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

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, log, mode, base_url: nil, proxy_address: nil, proxy_port: nil, proxy_user: nil, proxy_pass: nil, open_timeout: 10, read_timeout: 30, data_governance: DataGovernance::GLOBAL) ⇒ ConfigFetcher

Returns a new instance of ConfigFetcher.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/configcat/configfetcher.rb', line 70

def initialize(sdk_key, log, mode, base_url: nil, proxy_address: nil, proxy_port: nil, proxy_user: nil, proxy_pass: nil,
               open_timeout: 10, read_timeout: 30,
               data_governance: DataGovernance::GLOBAL)
  @_sdk_key = sdk_key
  @log = log
  @_proxy_address = proxy_address
  @_proxy_port = proxy_port
  @_proxy_user = proxy_user
  @_proxy_pass = proxy_pass
  @_open_timeout = open_timeout
  @_read_timeout = read_timeout
  @_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



156
157
158
159
160
# File 'lib/configcat/configfetcher.rb', line 156

def close
  if @_http
    @_http = nil
  end
end

#get_configuration(etag = "", retries = 0) ⇒ Object

Returns the FetchResponse object contains configuration entry



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/configcat/configfetcher.rb', line 104

def get_configuration(etag = "", retries = 0)
  fetch_response = _fetch(etag)

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

  preferences = fetch_response.entry.config.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 has 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
    @log.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
    @log.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(etag, retries + 1)
end

#get_open_timeoutObject



95
96
97
# File 'lib/configcat/configfetcher.rb', line 95

def get_open_timeout
  return @_open_timeout
end

#get_read_timeoutObject



99
100
101
# File 'lib/configcat/configfetcher.rb', line 99

def get_read_timeout
  return @_read_timeout
end