Class: ConfigCat::ConfigEntry

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

Constant Summary collapse

EMPTY =
ConfigEntry.new(etag: 'empty')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, etag = '', config_json_string = '{}', fetch_time = Utils::DISTANT_PAST) ⇒ ConfigEntry

Returns a new instance of ConfigEntry.



7
8
9
10
11
12
# File 'lib/configcat/configentry.rb', line 7

def initialize(config = {}, etag = '', config_json_string = '{}', fetch_time = Utils::DISTANT_PAST)
  @config = config
  @etag = etag
  @config_json_string = config_json_string
  @fetch_time = fetch_time
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/configcat/configentry.rb', line 5

def config
  @config
end

#config_json_stringObject

Returns the value of attribute config_json_string.



5
6
7
# File 'lib/configcat/configentry.rb', line 5

def config_json_string
  @config_json_string
end

#etagObject

Returns the value of attribute etag.



5
6
7
# File 'lib/configcat/configentry.rb', line 5

def etag
  @etag
end

#fetch_timeObject

Returns the value of attribute fetch_time.



5
6
7
# File 'lib/configcat/configentry.rb', line 5

def fetch_time
  @fetch_time
end

Class Method Details

.create_from_string(string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/configcat/configentry.rb', line 22

def self.create_from_string(string)
  return ConfigEntry.empty if string.nil? || string.empty?

  fetch_time_index = string.index("\n")
  etag_index = string.index("\n", fetch_time_index + 1)
  if fetch_time_index.nil? || etag_index.nil?
    raise 'Number of values is fewer than expected.'
  end

  begin
    fetch_time = Float(string[0...fetch_time_index])
  rescue ArgumentError
    raise "Invalid fetch time: #{string[0...fetch_time_index]}"
  end

  etag = string[fetch_time_index + 1...etag_index]
  if etag.nil? || etag.empty?
    raise 'Empty eTag value'
  end
  begin
    config_json = string[etag_index + 1..-1]
    config = JSON.parse(config_json)
    Config.fixup_config_salt_and_segments(config)
  rescue => e
    raise "Invalid config JSON: #{config_json}. #{e.message}"
  end

  ConfigEntry.new(config, etag, config_json, fetch_time / 1000.0)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/configcat/configentry.rb', line 14

def empty?
  self == ConfigEntry::EMPTY
end

#serializeObject



18
19
20
# File 'lib/configcat/configentry.rb', line 18

def serialize
  "#{(fetch_time * 1000).floor}\n#{etag}\n#{config_json_string}"
end