Class: Rox::Core::ConfigurationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/configuration/configuration_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(signature_verifier, error_reporter, configuration_fetched_invoker, rox_options = nil) ⇒ ConfigurationParser

Returns a new instance of ConfigurationParser.



13
14
15
16
17
18
# File 'lib/rox/core/configuration/configuration_parser.rb', line 13

def initialize(signature_verifier, error_reporter, configuration_fetched_invoker, rox_options = nil)
  @signature_verifier = signature_verifier
  @error_reporter = error_reporter
  @configuration_fetched_invoker = configuration_fetched_invoker
  @rox_options = rox_options
end

Instance Method Details

#parse(fetch_result, sdk_settings) ⇒ Object



20
21
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
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rox/core/configuration/configuration_parser.rb', line 20

def parse(fetch_result, sdk_settings)
  if fetch_result.nil? || fetch_result.data.nil? || fetch_result.data.empty?
    @configuration_fetched_invoker.invoke_error(FetcherError::EMPTY_JSON)
    @error_reporter.report('Failed to parse JSON configuration - Null Or Empty', ArgumentError.new('data'))
    return nil
  end

  begin
    json_obj = JSON.parse(fetch_result.data)
  rescue StandardError => e
    @configuration_fetched_invoker.invoke_error(FetcherError::CORRUPTED_JSON)
    @error_reporter.report('Failed to parse JSON configuration', e)
    return nil
  end

  if signature_verification_enabled? && fetch_result.source != ConfigurationSource::ROXY && !@signature_verifier.verify(json_obj['data'],
                                                                                                                        json_obj['signature_v0'])
    @configuration_fetched_invoker.invoke_error(FetcherError::SIGNATURE_VERIFICATION_ERROR)
    @error_reporter.report('Failed to validate signature',
                           StandardError.new("Data : #{json_obj['data']} Signature : #{json_obj['signature_v0']}"))
    return nil
  end

  signature_date = DateTime.rfc3339(json_obj['signed_date']).to_time
  internal_data_string = json_obj['data']
  internal_data_object = JSON.parse(internal_data_string)

  if fetch_result.source != ConfigurationSource::ROXY && internal_data_object['application'] != sdk_settings.api_key
    @configuration_fetched_invoker.invoke_error(FetcherError::MISMATCH_APP_KEY)
    @error_reporter.report('Failed to parse JSON configuration - ',
                           StandardError.new("Internal Data: #{internal_data_object['application']} SdkSettings: #{sdk_settings.api_key}"))
    return nil
  end

  experiments = parse_experiments(internal_data_object)
  target_groups = parse_target_groups(internal_data_object)

  Configuration.new(experiments, target_groups, signature_date)
rescue StandardError => e
  Logging.logger.error('Failed to parse configurations', e)
  @configuration_fetched_invoker.invoke_error(FetcherError::UNKNOWN)
  nil
end

#parse_experiment(data) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rox/core/configuration/configuration_parser.rb', line 68

def parse_experiment(data)
  condition = data['deploymentConfiguration']['condition']
  is_archived = data['archived']
  name = data['name']
  id = data['_id']
  labels = data['labels'] || []
  flags = data['featureFlags'].map { |f| f['name'] }
  ExperimentModel.new(id, name, condition, is_archived, flags, labels)
end

#parse_experiments(data) ⇒ Object



64
65
66
# File 'lib/rox/core/configuration/configuration_parser.rb', line 64

def parse_experiments(data)
  data['experiments'].map { |e| parse_experiment(e) }
end

#parse_target_group(data) ⇒ Object



82
83
84
# File 'lib/rox/core/configuration/configuration_parser.rb', line 82

def parse_target_group(data)
  TargetGroupModel.new(data['_id'], data['condition'])
end

#parse_target_groups(data) ⇒ Object



78
79
80
# File 'lib/rox/core/configuration/configuration_parser.rb', line 78

def parse_target_groups(data)
  data['targetGroups'].map { |g| parse_target_group(g) }
end