Class: EventSourceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_sdk/event_source_client.rb

Overview

class for event source client instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ EventSourceClient

Returns a new instance of EventSourceClient.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_sdk/event_source_client.rb', line 11

def initialize(config)
  @config = config
  @features = {}
  @analytics_collectors = []
  @has_data = false
  options = {
     headers: { Authorization: @config[:sdk_key]}
   }

# this needs to make use of the constant based uri, not hardcoded
sse_client = SSE::Client.new(@config[:server_address], headers: options[:headers])

@api_client = sse_client
end

Instance Attribute Details

#has_dataObject (readonly)

Returns the value of attribute has_data.



9
10
11
# File 'lib/ruby_sdk/event_source_client.rb', line 9

def has_data
  @has_data
end

Instance Method Details

#add_google_analytics_collector(config_object) ⇒ Object



81
82
83
84
85
# File 'lib/ruby_sdk/event_source_client.rb', line 81

def add_google_analytics_collector(config_object)
  analytics_collector = AnalyticsCollector.new(config_object)
  @analytics_collectors.push(analytics_collector)
  return analytics_collector
end

#get_feature(key, default_value = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/ruby_sdk/event_source_client.rb', line 66

def get_feature(key, default_value = nil)
  feature_state = get_feature_state(key)
  if feature_state.nil?
    HandleUndefinedFeature.handle(key, default_value)
    return
  end

  feature_state.value
end

#get_feature_state(key) ⇒ Object



77
78
79
# File 'lib/ruby_sdk/event_source_client.rb', line 77

def get_feature_state(key) 
  return @features[key]
end

#handle_all_features(all_features) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_sdk/event_source_client.rb', line 49

def handle_all_features(all_features)
  all_features = JSON.parse(all_features)
  feature_states = {}

  all_features.each do |feature|
    modified_feature_state_params = {
      title: feature['title'],
      value: feature['is_active'],
      strategy: {percentage: feature['rollout']}
    }
    feature_states[feature['title']] = FeatureState.new(modified_feature_state_params)
  end

  @features = feature_states
  @has_data = true
end

#handle_errorsObject



42
43
44
45
46
# File 'lib/ruby_sdk/event_source_client.rb', line 42

def handle_errors()
  @api_client.on_error do |error|
    puts "Event Source Failed: ", error
  end
end

#handle_eventsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_sdk/event_source_client.rb', line 31

def handle_events
  @api_client.on_event do |event| 
    data = JSON.parse(event[:data]) 
    event_type = data['eventType']
    if event_type == "ALL_FEATURES"
      payload = data['payload']
      handle_all_features(payload)
    end
  end
end

#log_event(event_object) ⇒ Object



87
88
89
90
91
# File 'lib/ruby_sdk/event_source_client.rb', line 87

def log_event(event_object)
  @analytics_collectors.each do |analytics_collector|
    analytics_collector.log_event(event_object)
  end
end

#startObject



26
27
28
29
# File 'lib/ruby_sdk/event_source_client.rb', line 26

def start
  handle_events()
  handle_errors()
end