Class: ConfigCat::CacheControlConfigFetcher

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, mode, base_url = nil) ⇒ CacheControlConfigFetcher

Returns a new instance of CacheControlConfigFetcher.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/configcat/configfetcher.rb', line 13

def initialize(api_key, mode, base_url=nil)
  @_api_key = api_key
  @_etag = nil
  @_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 = base_url.chomp("/")
  else
    @_base_url = BASE_URL
  end
  uri = URI.parse(@_base_url)
  @_http = Net::HTTP.new(uri.host, uri.port)
  @_http.use_ssl = true if uri.scheme == 'https'
  @_http.open_timeout = 10 # in seconds
  @_http.read_timeout = 30 # in seconds
end

Instance Method Details

#closeObject



41
42
43
44
45
# File 'lib/configcat/configfetcher.rb', line 41

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

#get_configuration_jsonObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/configcat/configfetcher.rb', line 29

def get_configuration_json()
  ConfigCat.logger.debug "Fetching configuration from ConfigCat"
  uri = URI.parse((((@_base_url + ("/")) + BASE_PATH) + @_api_key) + BASE_EXTENSION)
  @_headers["If-None-Match"] = @_etag unless @_etag.nil?
  request = Net::HTTP::Get.new(uri.request_uri, @_headers)
  response = @_http.request(request)
  json = JSON.parse(response.body)
  @_etag = response["ETag"]
  ConfigCat.logger.debug "ConfigCat configuration json fetch response code:#{response.code} Cached:#{response['ETag']}"
  return json
end